DiffManagerTests.coffee 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/DiffManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "DiffManager", ->
  8. beforeEach ->
  9. @DiffManager = SandboxedModule.require modulePath, requires:
  10. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  11. "./UpdatesManager": @UpdatesManager = {}
  12. "./DocumentUpdaterManager": @DocumentUpdaterManager = {}
  13. "./DiffGenerator": @DiffGenerator = {}
  14. @callback = sinon.stub()
  15. @from = new Date()
  16. @to = new Date(Date.now() + 10000)
  17. @project_id = "mock-project-id"
  18. @doc_id = "mock-doc-id"
  19. describe "getLatestDocAndUpdates", ->
  20. beforeEach ->
  21. @content = "hello world"
  22. @version = 42
  23. @updates = [ "mock-update-1", "mock-update-2" ]
  24. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(2, null, @content, @version)
  25. @UpdatesManager.getDocUpdatesWithUserInfo = sinon.stub().callsArgWith(3, null, @updates)
  26. @DiffManager.getLatestDocAndUpdates @project_id, @doc_id, @from, @to, @callback
  27. it "should get the latest version of the doc", ->
  28. @DocumentUpdaterManager.getDocument
  29. .calledWith(@project_id, @doc_id)
  30. .should.equal true
  31. it "should get the latest updates", ->
  32. @UpdatesManager.getDocUpdatesWithUserInfo
  33. .calledWith(@project_id, @doc_id, from: @from, to: @to)
  34. .should.equal true
  35. it "should call the callback with the content, version and updates", ->
  36. @callback.calledWith(null, @content, @version, @updates).should.equal true
  37. describe "getDiff", ->
  38. beforeEach ->
  39. @content = "hello world"
  40. # Op versions are the version they were applied to, so doc is always one version
  41. # ahead.s
  42. @version = 43
  43. @updates = [
  44. { op: "mock-4", v: 42, meta: { start_ts: new Date(@to.getTime() + 20)} }
  45. { op: "mock-3", v: 41, meta: { start_ts: new Date(@to.getTime() + 10)} }
  46. { op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
  47. { op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
  48. ]
  49. @fromVersion = 39
  50. @toVersion = 40
  51. @diffed_updates = @updates.slice(2)
  52. @rewound_content = "rewound-content"
  53. @diff = [ u: "mock-diff" ]
  54. describe "with matching versions", ->
  55. beforeEach ->
  56. @DiffManager.getDocumentBeforeVersion = sinon.stub().callsArgWith(3, null, @rewound_content, @updates)
  57. @DiffGenerator.buildDiff = sinon.stub().returns(@diff)
  58. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  59. it "should get the latest doc and version with all recent updates", ->
  60. @DiffManager.getDocumentBeforeVersion
  61. .calledWith(@project_id, @doc_id, @fromVersion)
  62. .should.equal true
  63. it "should generate the diff", ->
  64. @DiffGenerator.buildDiff
  65. .calledWith(@rewound_content, @diffed_updates.slice().reverse())
  66. .should.equal true
  67. it "should call the callback with the diff", ->
  68. @callback.calledWith(null, @diff).should.equal true
  69. describe "when the updates are inconsistent", ->
  70. beforeEach ->
  71. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  72. @DiffGenerator.buildDiff = sinon.stub().throws(@error = new Error("inconsistent!"))
  73. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  74. it "should call the callback with an error", ->
  75. @callback
  76. .calledWith(@error)
  77. .should.equal true
  78. describe "getDocumentBeforeVersion", ->
  79. beforeEach ->
  80. @DiffManager._tryGetDocumentBeforeVersion = sinon.stub()
  81. @document = "mock-documents"
  82. @rewound_updates = "mock-rewound-updates"
  83. describe "succesfully", ->
  84. beforeEach ->
  85. @DiffManager._tryGetDocumentBeforeVersion.yields(null, @document, @rewound_updates)
  86. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @version, @callback
  87. it "should call _tryGetDocumentBeforeVersion", ->
  88. @DiffManager._tryGetDocumentBeforeVersion
  89. .calledWith(@project_id, @doc_id, @version)
  90. .should.equal true
  91. it "should call the callback with the response", ->
  92. @callback.calledWith(null, @document, @rewound_updates).should.equal true
  93. describe "with a retry needed", ->
  94. beforeEach ->
  95. retried = false
  96. @DiffManager._tryGetDocumentBeforeVersion = (project_id, doc_id, version, callback) =>
  97. if !retried
  98. retried = true
  99. error = new Error()
  100. error.retry = true
  101. callback error
  102. else
  103. callback(null, @document, @rewound_updates)
  104. sinon.spy @DiffManager, "_tryGetDocumentBeforeVersion"
  105. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @version, @callback
  106. it "should call _tryGetDocumentBeforeVersion twice", ->
  107. @DiffManager._tryGetDocumentBeforeVersion
  108. .calledTwice
  109. .should.equal true
  110. it "should call the callback with the response", ->
  111. @callback.calledWith(null, @document, @rewound_updates).should.equal true
  112. describe "with a non-retriable error", ->
  113. beforeEach ->
  114. @error = new Error("oops")
  115. @DiffManager._tryGetDocumentBeforeVersion.yields(@error)
  116. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @version, @callback
  117. it "should call _tryGetDocumentBeforeVersion once", ->
  118. @DiffManager._tryGetDocumentBeforeVersion
  119. .calledOnce
  120. .should.equal true
  121. it "should call the callback with the error", ->
  122. @callback.calledWith(@error).should.equal true
  123. describe "when retry limit is matched", ->
  124. beforeEach ->
  125. @error = new Error("oops")
  126. @error.retry = true
  127. @DiffManager._tryGetDocumentBeforeVersion.yields(@error)
  128. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @version, @callback
  129. it "should call _tryGetDocumentBeforeVersion three times (max retries)", ->
  130. @DiffManager._tryGetDocumentBeforeVersion
  131. .calledThrice
  132. .should.equal true
  133. it "should call the callback with the error", ->
  134. @callback.calledWith(@error).should.equal true
  135. describe "_tryGetDocumentBeforeVersion", ->
  136. beforeEach ->
  137. @content = "hello world"
  138. # Op versions are the version they were applied to, so doc is always one version
  139. # ahead.s
  140. @version = 43
  141. @updates = [
  142. { op: "mock-4", v: 42, meta: { start_ts: new Date(@to.getTime() + 20)} }
  143. { op: "mock-3", v: 41, meta: { start_ts: new Date(@to.getTime() + 10)} }
  144. { op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
  145. { op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
  146. ]
  147. @fromVersion = 39
  148. @rewound_content = "rewound-content"
  149. @diff = [ u: "mock-diff" ]
  150. describe "with matching versions", ->
  151. beforeEach ->
  152. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  153. @DiffGenerator.rewindUpdates = sinon.spy (content, updates) =>
  154. # the rewindUpdates method reverses the 'updates' array
  155. updates.reverse()
  156. return @rewound_content
  157. @rewindUpdatesWithArgs = @DiffGenerator.rewindUpdates.withArgs(@content, @updates.slice().reverse())
  158. @DiffManager._tryGetDocumentBeforeVersion @project_id, @doc_id, @fromVersion, @callback
  159. it "should get the latest doc and version with all recent updates", ->
  160. @DiffManager.getLatestDocAndUpdates
  161. .calledWith(@project_id, @doc_id, @fromVersion, null)
  162. .should.equal true
  163. it "should rewind the diff", ->
  164. sinon.assert.calledOnce(@rewindUpdatesWithArgs)
  165. it "should call the callback with the rewound document and updates", ->
  166. @callback.calledWith(null, @rewound_content, @updates).should.equal true
  167. describe "with mismatching versions", ->
  168. beforeEach ->
  169. @version = 50
  170. @updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
  171. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  172. @DiffManager._tryGetDocumentBeforeVersion @project_id, @doc_id, @fromVersion, @callback
  173. it "should call the callback with an error with retry = true set", ->
  174. @callback.calledOnce.should.equal true
  175. error = @callback.args[0][0]
  176. expect(error.retry).to.equal true
  177. describe "when the updates are inconsistent", ->
  178. beforeEach ->
  179. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  180. @DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
  181. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @fromVersion, @callback
  182. it "should call the callback with an error", ->
  183. @callback
  184. .calledWith(@error)
  185. .should.equal true