UpdatesManagerTests.coffee 26 KB

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