DiffManagerTests.coffee 9.0 KB

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