UpdatesManagerTests.coffee 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/UpdatesManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "UpdatesManager", ->
  8. beforeEach ->
  9. @UpdatesManager = SandboxedModule.require modulePath, requires:
  10. "./UpdateCompressor": @UpdateCompressor = {}
  11. "./MongoManager" : @MongoManager = {}
  12. "./PackManager" : @PackManager = {}
  13. "./RedisManager" : @RedisManager = {}
  14. "./LockManager" : @LockManager = {}
  15. "./WebApiManager": @WebApiManager = {}
  16. "./UpdateTrimmer": @UpdateTrimmer = {}
  17. "./DocArchiveManager": @DocArchiveManager = {}
  18. "logger-sharelatex": { log: sinon.stub(), error: sinon.stub() }
  19. @doc_id = "doc-id-123"
  20. @project_id = "project-id-123"
  21. @callback = sinon.stub()
  22. @temporary = "temp-mock"
  23. describe "compressAndSaveRawUpdates", ->
  24. describe "when there are no raw ops", ->
  25. beforeEach ->
  26. @MongoManager.peekLastCompressedUpdate = sinon.stub()
  27. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, [], @temporary, @callback
  28. it "should not need to access the database", ->
  29. @MongoManager.peekLastCompressedUpdate.called.should.equal false
  30. it "should call the callback", ->
  31. @callback.called.should.equal true
  32. describe "when there is no compressed history to begin with", ->
  33. beforeEach ->
  34. @rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  35. @compressedUpdates = [ { v: 13, op: "compressed-op-12" } ]
  36. @MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
  37. @PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
  38. @UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
  39. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  40. it "should look at the last compressed op", ->
  41. @MongoManager.peekLastCompressedUpdate
  42. .calledWith(@doc_id)
  43. .should.equal true
  44. it "should save the compressed ops as a pack", ->
  45. @PackManager.insertCompressedUpdates
  46. .calledWith(@project_id, @doc_id, null, @compressedUpdates, @temporary)
  47. .should.equal true
  48. it "should call the callback", ->
  49. @callback.called.should.equal true
  50. describe "when the raw ops need appending to existing history", ->
  51. beforeEach ->
  52. @lastCompressedUpdate = { v: 11, op: "compressed-op-11" }
  53. @compressedUpdates = [ { v: 12, op: "compressed-op-11+12" }, { v: 13, op: "compressed-op-12" } ]
  54. @MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v)
  55. @PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
  56. @UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
  57. describe "when the raw ops start where the existing history ends", ->
  58. beforeEach ->
  59. @rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  60. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  61. it "should look at the last compressed op", ->
  62. @MongoManager.peekLastCompressedUpdate
  63. .calledWith(@doc_id)
  64. .should.equal true
  65. it "should compress the raw ops", ->
  66. @UpdateCompressor.compressRawUpdates
  67. .calledWith(null, @rawUpdates)
  68. .should.equal true
  69. it "should save the new compressed ops into a pack", ->
  70. @PackManager.insertCompressedUpdates
  71. .calledWith(@project_id, @doc_id, @lastCompressedUpdate, @compressedUpdates, @temporary)
  72. .should.equal true
  73. it "should call the callback", ->
  74. @callback.called.should.equal true
  75. describe "when the raw ops start where the existing history ends and the history is in a pack", ->
  76. beforeEach ->
  77. @lastCompressedUpdate = {pack: [{ v: 11, op: "compressed-op-11" }], v:11}
  78. @rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  79. @MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate, @lastCompressedUpdate.v)
  80. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  81. it "should look at the last compressed op", ->
  82. @MongoManager.peekLastCompressedUpdate
  83. .calledWith(@doc_id)
  84. .should.equal true
  85. it "should defer the compression of raw ops until they are written in a new pack", ->
  86. @UpdateCompressor.compressRawUpdates
  87. .should.not.be.called
  88. it "should save the new compressed ops into a pack", ->
  89. @PackManager.insertCompressedUpdates
  90. .calledWith(@project_id, @doc_id, @lastCompressedUpdate, @compressedUpdates, @temporary)
  91. .should.equal true
  92. it "should call the callback", ->
  93. @callback.called.should.equal true
  94. describe "when some raw ops are passed that have already been compressed", ->
  95. beforeEach ->
  96. @rawUpdates = [{ v: 10, op: "mock-op-10" }, { v: 11, op: "mock-op-11"}, { v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  97. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  98. it "should only compress the more recent raw ops", ->
  99. @UpdateCompressor.compressRawUpdates
  100. .calledWith(null, @rawUpdates.slice(-2))
  101. .should.equal true
  102. describe "when the raw ops do not follow from the last compressed op version", ->
  103. beforeEach ->
  104. @rawUpdates = [{ v: 13, op: "mock-op-13" }]
  105. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  106. it "should call the callback with an error", ->
  107. @callback
  108. .calledWith(new Error("Tried to apply raw op at version 13 to last compressed update with version 11"))
  109. .should.equal true
  110. it "should not insert any update into mongo", ->
  111. @PackManager.insertCompressedUpdates.called.should.equal false
  112. describe "when the raw ops need appending to existing history which is in S3", ->
  113. beforeEach ->
  114. @lastCompressedUpdate = null
  115. @lastVersion = 11
  116. @compressedUpdates = [ { v: 13, op: "compressed-op-12" } ]
  117. @MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null, @lastVersion)
  118. @PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
  119. @UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
  120. describe "when the raw ops start where the existing history ends", ->
  121. beforeEach ->
  122. @rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  123. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  124. it "should try to look at the last compressed op", ->
  125. @MongoManager.peekLastCompressedUpdate
  126. .calledWith(@doc_id)
  127. .should.equal true
  128. it "should compress the last compressed op and the raw ops", ->
  129. @UpdateCompressor.compressRawUpdates
  130. .calledWith(@lastCompressedUpdate, @rawUpdates)
  131. .should.equal true
  132. it "should save the compressed ops", ->
  133. @PackManager.insertCompressedUpdates
  134. .calledWith(@project_id, @doc_id, null, @compressedUpdates, @temporary)
  135. .should.equal true
  136. it "should call the callback", ->
  137. @callback.called.should.equal true
  138. describe "processUncompressedUpdates", ->
  139. beforeEach ->
  140. @UpdatesManager.compressAndSaveRawUpdates = sinon.stub().callsArgWith(4)
  141. @RedisManager.deleteAppliedDocUpdates = sinon.stub().callsArg(3)
  142. @MongoManager.backportProjectId = sinon.stub().callsArg(2)
  143. @UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, @temporary = "temp mock")
  144. describe "when there is fewer than one batch to send", ->
  145. beforeEach ->
  146. @updates = ["mock-update"]
  147. @RedisManager.getOldestDocUpdates = sinon.stub().callsArgWith(2, null, @updates)
  148. @RedisManager.expandDocUpdates = sinon.stub().callsArgWith(1, null, @updates)
  149. @UpdatesManager.processUncompressedUpdates @project_id, @doc_id, @temporary, @callback
  150. it "should get the oldest updates", ->
  151. @RedisManager.getOldestDocUpdates
  152. .calledWith(@doc_id, @UpdatesManager.REDIS_READ_BATCH_SIZE)
  153. .should.equal true
  154. it "should compress and save the updates", ->
  155. @UpdatesManager.compressAndSaveRawUpdates
  156. .calledWith(@project_id, @doc_id, @updates, @temporary)
  157. .should.equal true
  158. it "should delete the batch of uncompressed updates that was just processed", ->
  159. @RedisManager.deleteAppliedDocUpdates
  160. .calledWith(@project_id, @doc_id, @updates)
  161. .should.equal true
  162. it "should call the callback", ->
  163. @callback.called.should.equal true
  164. describe "when there are multiple batches to send", ->
  165. beforeEach (done) ->
  166. @UpdatesManager.REDIS_READ_BATCH_SIZE = 2
  167. @updates = ["mock-update-0", "mock-update-1", "mock-update-2", "mock-update-3", "mock-update-4"]
  168. @redisArray = @updates.slice()
  169. @RedisManager.getOldestDocUpdates = (doc_id, batchSize, callback = (error, updates) ->) =>
  170. updates = @redisArray.slice(0, batchSize)
  171. @redisArray = @redisArray.slice(batchSize)
  172. callback null, updates
  173. sinon.spy @RedisManager, "getOldestDocUpdates"
  174. @RedisManager.expandDocUpdates = (jsonUpdates, callback) =>
  175. callback null, jsonUpdates
  176. sinon.spy @RedisManager, "expandDocUpdates"
  177. @UpdatesManager.processUncompressedUpdates @project_id, @doc_id, @temporary, (args...) =>
  178. @callback(args...)
  179. done()
  180. it "should get the oldest updates in three batches ", ->
  181. @RedisManager.getOldestDocUpdates.callCount.should.equal 3
  182. it "should compress and save the updates in batches", ->
  183. @UpdatesManager.compressAndSaveRawUpdates
  184. .calledWith(@project_id, @doc_id, @updates.slice(0,2), @temporary)
  185. .should.equal true
  186. @UpdatesManager.compressAndSaveRawUpdates
  187. .calledWith(@project_id, @doc_id, @updates.slice(2,4), @temporary)
  188. .should.equal true
  189. @UpdatesManager.compressAndSaveRawUpdates
  190. .calledWith(@project_id, @doc_id, @updates.slice(4,5), @temporary)
  191. .should.equal true
  192. it "should delete the batches of uncompressed updates", ->
  193. @RedisManager.deleteAppliedDocUpdates.callCount.should.equal 3
  194. it "should call the callback", ->
  195. @callback.called.should.equal true
  196. describe "processCompressedUpdatesWithLock", ->
  197. beforeEach ->
  198. @UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, @temporary = "temp mock")
  199. @MongoManager.backportProjectId = sinon.stub().callsArg(2)
  200. @UpdatesManager._processUncompressedUpdates = sinon.stub().callsArg(3)
  201. @LockManager.runWithLock = sinon.stub().callsArg(2)
  202. @UpdatesManager.processUncompressedUpdatesWithLock @project_id, @doc_id, @callback
  203. it "should check if the updates are temporary", ->
  204. @UpdateTrimmer.shouldTrimUpdates
  205. .calledWith(@project_id)
  206. .should.equal true
  207. it "should backport the project id", ->
  208. @MongoManager.backportProjectId
  209. .calledWith(@project_id, @doc_id)
  210. .should.equal true
  211. it "should run processUncompressedUpdates with the lock", ->
  212. @LockManager.runWithLock
  213. .calledWith(
  214. "HistoryLock:#{@doc_id}"
  215. )
  216. .should.equal true
  217. it "should call the callback", ->
  218. @callback.called.should.equal true
  219. describe "getDocUpdates", ->
  220. beforeEach ->
  221. @updates = ["mock-updates"]
  222. @options = { to: "mock-to", limit: "mock-limit" }
  223. @PackManager.getOpsByVersionRange = sinon.stub().callsArgWith(4, null, @updates)
  224. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
  225. @UpdatesManager.getDocUpdates @project_id, @doc_id, @options, @callback
  226. it "should process outstanding updates", ->
  227. @UpdatesManager.processUncompressedUpdatesWithLock
  228. .calledWith(@project_id, @doc_id)
  229. .should.equal true
  230. it "should get the updates from the database", ->
  231. @PackManager.getOpsByVersionRange
  232. .calledWith(@project_id, @doc_id, @options.from, @options.to)
  233. .should.equal true
  234. it "should return the updates", ->
  235. @callback
  236. .calledWith(null, @updates)
  237. .should.equal true
  238. describe "getDocUpdatesWithUserInfo", ->
  239. beforeEach ->
  240. @updates = ["mock-updates"]
  241. @options = { to: "mock-to", limit: "mock-limit" }
  242. @updatesWithUserInfo = ["updates-with-user-info"]
  243. @UpdatesManager.getDocUpdates = sinon.stub().callsArgWith(3, null, @updates)
  244. @UpdatesManager.fillUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  245. @UpdatesManager.getDocUpdatesWithUserInfo @project_id, @doc_id, @options, @callback
  246. it "should get the updates", ->
  247. @UpdatesManager.getDocUpdates
  248. .calledWith(@project_id, @doc_id, @options)
  249. .should.equal true
  250. it "should file the updates with the user info", ->
  251. @UpdatesManager.fillUserInfo
  252. .calledWith(@updates)
  253. .should.equal true
  254. it "should return the updates with the filled details", ->
  255. @callback.calledWith(null, @updatesWithUserInfo).should.equal true
  256. describe "processUncompressedUpdatesForProject", ->
  257. beforeEach (done) ->
  258. @doc_ids = ["mock-id-1", "mock-id-2"]
  259. @UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, @temporary = "temp mock")
  260. @MongoManager.backportProjectId = sinon.stub().callsArg(2)
  261. @UpdatesManager._processUncompressedUpdatesForDocWithLock = sinon.stub().callsArg(3)
  262. @RedisManager.getDocIdsWithHistoryOps = sinon.stub().callsArgWith(1, null, @doc_ids)
  263. @UpdatesManager.processUncompressedUpdatesForProject @project_id, () =>
  264. @callback()
  265. done()
  266. it "should get all the docs with history ops", ->
  267. @RedisManager.getDocIdsWithHistoryOps
  268. .calledWith(@project_id)
  269. .should.equal true
  270. it "should process the doc ops for the each doc_id", ->
  271. for doc_id in @doc_ids
  272. @UpdatesManager._processUncompressedUpdatesForDocWithLock
  273. .calledWith(@project_id, doc_id, @temporary)
  274. .should.equal true
  275. it "should call the callback", ->
  276. @callback.called.should.equal true
  277. describe "getSummarizedProjectUpdates", ->
  278. beforeEach ->
  279. @updates = [{doc_id: 123, v:456, op: "mock-updates", meta: {user_id: 123, start_ts: 1233, end_ts:1234}}]
  280. @options = { before: "mock-before", limit: "mock-limit" }
  281. @summarizedUpdates = [
  282. {meta: {user_ids: [123], start_ts: 1233, end_ts:1234},docs:{"123":{fromV:456,toV:456}}}
  283. ]
  284. @updatesWithUserInfo = ["updates-with-user-info"]
  285. @done_state = false
  286. @iterator =
  287. next: (cb) =>
  288. @done_state = true
  289. cb(null, @updates)
  290. done: () =>
  291. @done_state
  292. @PackManager.makeProjectIterator = sinon.stub().callsArgWith(2, null, @iterator)
  293. @UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
  294. @UpdatesManager.fillSummarizedUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  295. @UpdatesManager.getSummarizedProjectUpdates @project_id, @options, @callback
  296. it "should process any outstanding updates", ->
  297. @UpdatesManager.processUncompressedUpdatesForProject
  298. .calledWith(@project_id)
  299. .should.equal true
  300. it "should get the updates", ->
  301. @PackManager.makeProjectIterator
  302. .calledWith(@project_id, @options.before)
  303. .should.equal true
  304. it "should fill the updates with the user info", ->
  305. @UpdatesManager.fillSummarizedUserInfo
  306. .calledWith(@summarizedUpdates)
  307. .should.equal true
  308. it "should return the updates with the filled details", ->
  309. @callback.calledWith(null, @updatesWithUserInfo).should.equal true
  310. # describe "_extendBatchOfSummarizedUpdates", ->
  311. # beforeEach ->
  312. # @before = Date.now()
  313. # @min_count = 2
  314. # @existingSummarizedUpdates = ["summarized-updates-3"]
  315. # @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  316. # describe "when there are updates to get", ->
  317. # beforeEach ->
  318. # @updates = [
  319. # {op: "mock-op-1", meta: end_ts: @before - 10},
  320. # {op: "mock-op-1", meta: end_ts: @nextBeforeTimestamp = @before - 20}
  321. # ]
  322. # @existingSummarizedUpdates = ["summarized-updates-3"]
  323. # @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  324. # @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  325. # @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  326. # @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  327. # it "should get the updates", ->
  328. # @UpdatesManager.getProjectUpdatesWithUserInfo
  329. # .calledWith(@project_id, { before: @before, limit: 3 * @min_count })
  330. # .should.equal true
  331. # it "should summarize the updates", ->
  332. # @UpdatesManager._summarizeUpdates
  333. # .calledWith(@updates, @existingSummarizedUpdates)
  334. # .should.equal true
  335. # it "should call the callback with the summarized updates and the next before timestamp", ->
  336. # @callback.calledWith(null, @summarizedUpdates, @nextBeforeTimestamp).should.equal true
  337. # describe "when there are no more updates", ->
  338. # beforeEach ->
  339. # @updates = []
  340. # @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  341. # @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  342. # @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  343. # it "should call the callback with the summarized updates and null for nextBeforeTimestamp", ->
  344. # @callback.calledWith(null, @summarizedUpdates, null).should.equal true
  345. # describe "getSummarizedProjectUpdates", ->
  346. # describe "when one batch of updates is enough to meet the limit", ->
  347. # beforeEach ->
  348. # @before = Date.now()
  349. # @min_count = 2
  350. # @updates = ["summarized-updates-3", "summarized-updates-2"]
  351. # @nextBeforeTimestamp = @before - 100
  352. # @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, @nextBeforeTimestamp)
  353. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  354. # it "should get the batch of summarized updates", ->
  355. # @UpdatesManager._extendBatchOfSummarizedUpdates
  356. # .calledWith(@project_id, [], @before, @min_count)
  357. # .should.equal true
  358. # it "should call the callback with the updates", ->
  359. # @callback.calledWith(null, @updates, @nextBeforeTimestamp).should.equal true
  360. # describe "when multiple batches are needed to meet the limit", ->
  361. # beforeEach ->
  362. # @before = Date.now()
  363. # @min_count = 4
  364. # @firstBatch = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  365. # @nextBeforeTimestamp = @before - 100
  366. # @secondBatch = [{ toV: 4, fromV: 4 }, { toV: 3, fromV: 3 }]
  367. # @nextNextBeforeTimestamp = @before - 200
  368. # @UpdatesManager._extendBatchOfSummarizedUpdates = (project_id, existingUpdates, before, desiredLength, callback) =>
  369. # if existingUpdates.length == 0
  370. # callback null, @firstBatch, @nextBeforeTimestamp
  371. # else
  372. # callback null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp
  373. # sinon.spy @UpdatesManager, "_extendBatchOfSummarizedUpdates"
  374. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  375. # it "should get the first batch of summarized updates", ->
  376. # @UpdatesManager._extendBatchOfSummarizedUpdates
  377. # .calledWith(@project_id, [], @before, @min_count)
  378. # .should.equal true
  379. # it "should get the second batch of summarized updates", ->
  380. # @UpdatesManager._extendBatchOfSummarizedUpdates
  381. # .calledWith(@project_id, @firstBatch, @nextBeforeTimestamp, @min_count)
  382. # .should.equal true
  383. # it "should call the callback with all the updates", ->
  384. # @callback.calledWith(null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp).should.equal true
  385. # describe "when the end of the database is hit", ->
  386. # beforeEach ->
  387. # @before = Date.now()
  388. # @min_count = 4
  389. # @updates = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  390. # @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, null)
  391. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  392. # it "should get the batch of summarized updates", ->
  393. # @UpdatesManager._extendBatchOfSummarizedUpdates
  394. # .calledWith(@project_id, [], @before, @min_count)
  395. # .should.equal true
  396. # it "should call the callback with the updates", ->
  397. # @callback.calledWith(null, @updates, null).should.equal true
  398. describe "fillUserInfo", ->
  399. describe "with valid users", ->
  400. beforeEach (done) ->
  401. {ObjectId} = require "mongojs"
  402. @user_id_1 = ObjectId().toString()
  403. @user_id_2 = ObjectId().toString()
  404. @updates = [{
  405. meta:
  406. user_id: @user_id_1
  407. op: "mock-op-1"
  408. }, {
  409. meta:
  410. user_id: @user_id_1
  411. op: "mock-op-2"
  412. }, {
  413. meta:
  414. user_id: @user_id_2
  415. op: "mock-op-3"
  416. }]
  417. @user_info = {}
  418. @user_info[@user_id_1] = email: "user1@sharelatex.com"
  419. @user_info[@user_id_2] = email: "user2@sharelatex.com"
  420. @WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
  421. callback null, @user_info[user_id]
  422. sinon.spy @WebApiManager, "getUserInfo"
  423. @UpdatesManager.fillUserInfo @updates, (error, @results) =>
  424. done()
  425. it "should only call getUserInfo once for each user_id", ->
  426. @WebApiManager.getUserInfo.calledTwice.should.equal true
  427. @WebApiManager.getUserInfo
  428. .calledWith(@user_id_1)
  429. .should.equal true
  430. @WebApiManager.getUserInfo
  431. .calledWith(@user_id_2)
  432. .should.equal true
  433. it "should return the updates with the user info filled", ->
  434. expect(@results).to.deep.equal [{
  435. meta:
  436. user:
  437. email: "user1@sharelatex.com"
  438. op: "mock-op-1"
  439. }, {
  440. meta:
  441. user:
  442. email: "user1@sharelatex.com"
  443. op: "mock-op-2"
  444. }, {
  445. meta:
  446. user:
  447. email: "user2@sharelatex.com"
  448. op: "mock-op-3"
  449. }]
  450. describe "with invalid user ids", ->
  451. beforeEach (done) ->
  452. @updates = [{
  453. meta:
  454. user_id: null
  455. op: "mock-op-1"
  456. }, {
  457. meta:
  458. user_id: "anonymous-user"
  459. op: "mock-op-2"
  460. }]
  461. @WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
  462. callback null, @user_info[user_id]
  463. sinon.spy @WebApiManager, "getUserInfo"
  464. @UpdatesManager.fillUserInfo @updates, (error, @results) =>
  465. done()
  466. it "should not call getUserInfo", ->
  467. @WebApiManager.getUserInfo.called.should.equal false
  468. it "should return the updates without the user info filled", ->
  469. expect(@results).to.deep.equal [{
  470. meta: {}
  471. op: "mock-op-1"
  472. }, {
  473. meta: {}
  474. op: "mock-op-2"
  475. }]
  476. describe "_summarizeUpdates", ->
  477. beforeEach ->
  478. @now = Date.now()
  479. @user_1 = { id: "mock-user-1" }
  480. @user_2 = { id: "mock-user-2" }
  481. it "should concat updates that are close in time", ->
  482. result = @UpdatesManager._summarizeUpdates [{
  483. doc_id: "doc-id-1"
  484. meta:
  485. user_id: @user_1.id
  486. start_ts: @now + 20
  487. end_ts: @now + 30
  488. v: 5
  489. }, {
  490. doc_id: "doc-id-1"
  491. meta:
  492. user_id: @user_2.id
  493. start_ts: @now
  494. end_ts: @now + 10
  495. v: 4
  496. }]
  497. expect(result).to.deep.equal [{
  498. docs:
  499. "doc-id-1":
  500. fromV: 4
  501. toV: 5
  502. meta:
  503. user_ids: [@user_1.id, @user_2.id]
  504. start_ts: @now
  505. end_ts: @now + 30
  506. }]
  507. it "should leave updates that are far apart in time", ->
  508. oneDay = 1000 * 60 * 60 * 24
  509. result = @UpdatesManager._summarizeUpdates [{
  510. doc_id: "doc-id-1"
  511. meta:
  512. user_id: @user_2.id
  513. start_ts: @now + oneDay
  514. end_ts: @now + oneDay + 10
  515. v: 5
  516. }, {
  517. doc_id: "doc-id-1"
  518. meta:
  519. user_id: @user_1.id
  520. start_ts: @now
  521. end_ts: @now + 10
  522. v: 4
  523. }]
  524. expect(result).to.deep.equal [{
  525. docs:
  526. "doc-id-1":
  527. fromV: 5
  528. toV: 5
  529. meta:
  530. user_ids: [@user_2.id]
  531. start_ts: @now + oneDay
  532. end_ts: @now + oneDay + 10
  533. }, {
  534. docs:
  535. "doc-id-1":
  536. fromV: 4
  537. toV: 4
  538. meta:
  539. user_ids: [@user_1.id]
  540. start_ts: @now
  541. end_ts: @now + 10
  542. }]
  543. it "should concat onto existing summarized updates", ->
  544. result = @UpdatesManager._summarizeUpdates [{
  545. doc_id: "doc-id-2"
  546. meta:
  547. user_id: @user_1.id
  548. start_ts: @now + 20
  549. end_ts: @now + 30
  550. v: 5
  551. }, {
  552. doc_id: "doc-id-2"
  553. meta:
  554. user_id: @user_2.id
  555. start_ts: @now
  556. end_ts: @now + 10
  557. v: 4
  558. }], [{
  559. docs:
  560. "doc-id-1":
  561. fromV: 6
  562. toV: 8
  563. meta:
  564. user_ids: [@user_1.id]
  565. start_ts: @now + 40
  566. end_ts: @now + 50
  567. }]
  568. expect(result).to.deep.equal [{
  569. docs:
  570. "doc-id-1":
  571. toV: 8
  572. fromV: 6
  573. "doc-id-2":
  574. toV: 5
  575. fromV: 4
  576. meta:
  577. user_ids: [@user_1.id, @user_2.id]
  578. start_ts: @now
  579. end_ts: @now + 50
  580. }]
  581. it "should include null user values", ->
  582. result = @UpdatesManager._summarizeUpdates [{
  583. doc_id: "doc-id-1"
  584. meta:
  585. user_id: @user_1.id
  586. start_ts: @now + 20
  587. end_ts: @now + 30
  588. v: 5
  589. }, {
  590. doc_id: "doc-id-1"
  591. meta:
  592. user_id: null
  593. start_ts: @now
  594. end_ts: @now + 10
  595. v: 4
  596. }]
  597. expect(result).to.deep.equal [{
  598. docs:
  599. "doc-id-1":
  600. fromV: 4
  601. toV: 5
  602. meta:
  603. user_ids: [@user_1.id, null]
  604. start_ts: @now
  605. end_ts: @now + 30
  606. }]
  607. it "should include null user values, when the null is earlier in the updates list", ->
  608. result = @UpdatesManager._summarizeUpdates [{
  609. doc_id: "doc-id-1"
  610. meta:
  611. user_id: null
  612. start_ts: @now
  613. end_ts: @now + 10
  614. v: 4
  615. }, {
  616. doc_id: "doc-id-1"
  617. meta:
  618. user_id: @user_1.id
  619. start_ts: @now + 20
  620. end_ts: @now + 30
  621. v: 5
  622. }]
  623. expect(result).to.deep.equal [{
  624. docs:
  625. "doc-id-1":
  626. fromV: 4
  627. toV: 5
  628. meta:
  629. user_ids: [null, @user_1.id]
  630. start_ts: @now
  631. end_ts: @now + 30
  632. }]
  633. it "should roll several null user values into one", ->
  634. result = @UpdatesManager._summarizeUpdates [{
  635. doc_id: "doc-id-1"
  636. meta:
  637. user_id: @user_1.id
  638. start_ts: @now + 20
  639. end_ts: @now + 30
  640. v: 5
  641. }, {
  642. doc_id: "doc-id-1"
  643. meta:
  644. user_id: null
  645. start_ts: @now
  646. end_ts: @now + 10
  647. v: 4
  648. }, {
  649. doc_id: "doc-id-1"
  650. meta:
  651. user_id: null
  652. start_ts: @now + 2
  653. end_ts: @now + 4
  654. v: 4
  655. }]
  656. expect(result).to.deep.equal [{
  657. docs:
  658. "doc-id-1":
  659. fromV: 4
  660. toV: 5
  661. meta:
  662. user_ids: [@user_1.id, null]
  663. start_ts: @now
  664. end_ts: @now + 30
  665. }]
  666. it "should split updates before a big delete", ->
  667. result = @UpdatesManager._summarizeUpdates [{
  668. doc_id: "doc-id-1"
  669. op: [{ d: "this is a long long long long long delete", p: 34 }]
  670. meta:
  671. user_id: @user_1.id
  672. start_ts: @now + 20
  673. end_ts: @now + 30
  674. v: 5
  675. }, {
  676. doc_id: "doc-id-1"
  677. meta:
  678. user_id: @user_2.id
  679. start_ts: @now
  680. end_ts: @now + 10
  681. v: 4
  682. }]
  683. expect(result).to.deep.equal [{
  684. docs:
  685. "doc-id-1":
  686. fromV: 5
  687. toV: 5
  688. meta:
  689. user_ids: [@user_1.id]
  690. start_ts: @now + 20
  691. end_ts: @now + 30
  692. }, {
  693. docs:
  694. "doc-id-1":
  695. fromV: 4
  696. toV: 4
  697. meta:
  698. user_ids: [@user_2.id]
  699. start_ts: @now
  700. end_ts: @now + 10
  701. }]