FlushingDocsTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. async = require "async"
  6. MockWebApi = require "./helpers/MockWebApi"
  7. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  8. DocUpdaterApp = require "./helpers/DocUpdaterApp"
  9. describe "Flushing a doc to Mongo", ->
  10. before (done) ->
  11. @lines = ["one", "two", "three"]
  12. @version = 42
  13. @update =
  14. doc: @doc_id
  15. meta: { user_id: 'last-author-fake-id' }
  16. op: [{
  17. i: "one and a half\n"
  18. p: 4
  19. }]
  20. v: @version
  21. @result = ["one", "one and a half", "two", "three"]
  22. DocUpdaterApp.ensureRunning(done)
  23. describe "when the updated doc exists in the doc updater", ->
  24. before (done) ->
  25. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  26. sinon.spy MockWebApi, "setDocument"
  27. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  28. DocUpdaterClient.sendUpdates @project_id, @doc_id, [@update], (error) =>
  29. throw error if error?
  30. setTimeout () =>
  31. DocUpdaterClient.flushDoc @project_id, @doc_id, done
  32. , 200
  33. after ->
  34. MockWebApi.setDocument.restore()
  35. it "should flush the updated doc lines and version to the web api", ->
  36. MockWebApi.setDocument
  37. .calledWith(@project_id, @doc_id, @result, @version + 1)
  38. .should.equal true
  39. it "should flush the last update author and time to the web api", ->
  40. lastUpdatedAt = MockWebApi.setDocument.lastCall.args[5]
  41. parseInt(lastUpdatedAt).should.be.closeTo((new Date()).getTime(), 30000)
  42. lastUpdatedBy = MockWebApi.setDocument.lastCall.args[6]
  43. lastUpdatedBy.should.equal 'last-author-fake-id'
  44. describe "when the doc does not exist in the doc updater", ->
  45. before (done) ->
  46. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  47. MockWebApi.insertDoc @project_id, @doc_id, {
  48. lines: @lines
  49. }
  50. sinon.spy MockWebApi, "setDocument"
  51. DocUpdaterClient.flushDoc @project_id, @doc_id, done
  52. after ->
  53. MockWebApi.setDocument.restore()
  54. it "should not flush the doc to the web api", ->
  55. MockWebApi.setDocument.called.should.equal false
  56. describe "when the web api http request takes a long time on first request", ->
  57. before (done) ->
  58. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  59. MockWebApi.insertDoc @project_id, @doc_id, {
  60. lines: @lines
  61. version: @version
  62. }
  63. t = 30000
  64. sinon.stub MockWebApi, "setDocument", (project_id, doc_id, lines, version, ranges, lastUpdatedAt, lastUpdatedBy, callback = (error) ->) ->
  65. setTimeout callback, t
  66. t = 0
  67. DocUpdaterClient.preloadDoc @project_id, @doc_id, done
  68. after ->
  69. MockWebApi.setDocument.restore()
  70. it "should still work", (done) ->
  71. start = Date.now()
  72. DocUpdaterClient.flushDoc @project_id, @doc_id, (error, res, doc) =>
  73. res.statusCode.should.equal 204
  74. delta = Date.now() - start
  75. expect(delta).to.be.below 20000
  76. done()