UpdatesManagerTests.coffee 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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, @callback
  150. it "should check if the updates are temporary", ->
  151. @UpdateTrimmer.shouldTrimUpdates
  152. .calledWith(@project_id)
  153. .should.equal true
  154. it "should backport the project id", ->
  155. @MongoManager.backportProjectId
  156. .calledWith(@project_id, @doc_id)
  157. .should.equal true
  158. it "should get the oldest updates", ->
  159. @RedisManager.getOldestDocUpdates
  160. .calledWith(@doc_id, @UpdatesManager.REDIS_READ_BATCH_SIZE)
  161. .should.equal true
  162. it "should compress and save the updates", ->
  163. @UpdatesManager.compressAndSaveRawUpdates
  164. .calledWith(@project_id, @doc_id, @updates, @temporary)
  165. .should.equal true
  166. it "should delete the batch of uncompressed updates that was just processed", ->
  167. @RedisManager.deleteAppliedDocUpdates
  168. .calledWith(@project_id, @doc_id, @updates)
  169. .should.equal true
  170. it "should call the callback", ->
  171. @callback.called.should.equal true
  172. describe "when there are multiple batches to send", ->
  173. beforeEach (done) ->
  174. @UpdatesManager.REDIS_READ_BATCH_SIZE = 2
  175. @updates = ["mock-update-0", "mock-update-1", "mock-update-2", "mock-update-3", "mock-update-4"]
  176. @redisArray = @updates.slice()
  177. @RedisManager.getOldestDocUpdates = (doc_id, batchSize, callback = (error, updates) ->) =>
  178. updates = @redisArray.slice(0, batchSize)
  179. @redisArray = @redisArray.slice(batchSize)
  180. callback null, updates
  181. sinon.spy @RedisManager, "getOldestDocUpdates"
  182. @RedisManager.expandDocUpdates = (jsonUpdates, callback) =>
  183. callback null, jsonUpdates
  184. sinon.spy @RedisManager, "expandDocUpdates"
  185. @UpdatesManager.processUncompressedUpdates @project_id, @doc_id, (args...) =>
  186. @callback(args...)
  187. done()
  188. it "should get the oldest updates in three batches ", ->
  189. @RedisManager.getOldestDocUpdates.callCount.should.equal 3
  190. it "should compress and save the updates in batches", ->
  191. @UpdatesManager.compressAndSaveRawUpdates
  192. .calledWith(@project_id, @doc_id, @updates.slice(0,2), @temporary)
  193. .should.equal true
  194. @UpdatesManager.compressAndSaveRawUpdates
  195. .calledWith(@project_id, @doc_id, @updates.slice(2,4), @temporary)
  196. .should.equal true
  197. @UpdatesManager.compressAndSaveRawUpdates
  198. .calledWith(@project_id, @doc_id, @updates.slice(4,5), @temporary)
  199. .should.equal true
  200. it "should delete the batches of uncompressed updates", ->
  201. @RedisManager.deleteAppliedDocUpdates.callCount.should.equal 3
  202. it "should call the callback", ->
  203. @callback.called.should.equal true
  204. describe "processCompressedUpdatesWithLock", ->
  205. beforeEach ->
  206. @UpdatesManager.processUncompressedUpdates = sinon.stub().callsArg(2)
  207. @LockManager.runWithLock = sinon.stub().callsArg(2)
  208. @UpdatesManager.processUncompressedUpdatesWithLock @project_id, @doc_id, @callback
  209. it "should run processUncompressedUpdates with the lock", ->
  210. @LockManager.runWithLock
  211. .calledWith(
  212. "HistoryLock:#{@doc_id}"
  213. )
  214. .should.equal true
  215. it "should call the callback", ->
  216. @callback.called.should.equal true
  217. describe "getDocUpdates", ->
  218. beforeEach ->
  219. @updates = ["mock-updates"]
  220. @options = { to: "mock-to", limit: "mock-limit" }
  221. @PackManager.getOpsByVersionRange = sinon.stub().callsArgWith(4, null, @updates)
  222. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
  223. @UpdatesManager.getDocUpdates @project_id, @doc_id, @options, @callback
  224. it "should process outstanding updates", ->
  225. @UpdatesManager.processUncompressedUpdatesWithLock
  226. .calledWith(@project_id, @doc_id)
  227. .should.equal true
  228. it "should get the updates from the database", ->
  229. @PackManager.getOpsByVersionRange
  230. .calledWith(@project_id, @doc_id, @options.from, @options.to)
  231. .should.equal true
  232. it "should return the updates", ->
  233. @callback
  234. .calledWith(null, @updates)
  235. .should.equal true
  236. describe "getDocUpdatesWithUserInfo", ->
  237. beforeEach ->
  238. @updates = ["mock-updates"]
  239. @options = { to: "mock-to", limit: "mock-limit" }
  240. @updatesWithUserInfo = ["updates-with-user-info"]
  241. @UpdatesManager.getDocUpdates = sinon.stub().callsArgWith(3, null, @updates)
  242. @UpdatesManager.fillUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  243. @UpdatesManager.getDocUpdatesWithUserInfo @project_id, @doc_id, @options, @callback
  244. it "should get the updates", ->
  245. @UpdatesManager.getDocUpdates
  246. .calledWith(@project_id, @doc_id, @options)
  247. .should.equal true
  248. it "should file the updates with the user info", ->
  249. @UpdatesManager.fillUserInfo
  250. .calledWith(@updates)
  251. .should.equal true
  252. it "should return the updates with the filled details", ->
  253. @callback.calledWith(null, @updatesWithUserInfo).should.equal true
  254. describe "processUncompressedUpdatesForProject", ->
  255. beforeEach (done) ->
  256. @doc_ids = ["mock-id-1", "mock-id-2"]
  257. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
  258. @RedisManager.getDocIdsWithHistoryOps = sinon.stub().callsArgWith(1, null, @doc_ids)
  259. @UpdatesManager.processUncompressedUpdatesForProject @project_id, () =>
  260. @callback()
  261. done()
  262. it "should get all the docs with history ops", ->
  263. @RedisManager.getDocIdsWithHistoryOps
  264. .calledWith(@project_id)
  265. .should.equal true
  266. it "should process the doc ops for the each doc_id", ->
  267. for doc_id in @doc_ids
  268. @UpdatesManager.processUncompressedUpdatesWithLock
  269. .calledWith(@project_id, doc_id)
  270. .should.equal true
  271. it "should call the callback", ->
  272. @callback.called.should.equal true
  273. describe "getSummarizedProjectUpdates", ->
  274. beforeEach ->
  275. @updates = [{doc_id: 123, v:456, op: "mock-updates", meta: {user_id: 123, start_ts: 1233, end_ts:1234}}]
  276. @options = { before: "mock-before", limit: "mock-limit" }
  277. @summarizedUpdates = [
  278. {meta: {user_ids: [123], start_ts: 1233, end_ts:1234},docs:{"123":{fromV:456,toV:456}}}
  279. ]
  280. @updatesWithUserInfo = ["updates-with-user-info"]
  281. @done_state = false
  282. @iterator =
  283. next: (cb) =>
  284. @done_state = true
  285. cb(null, @updates)
  286. done: () =>
  287. @done_state
  288. @PackManager.makeProjectIterator = sinon.stub().callsArgWith(2, null, @iterator)
  289. @UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
  290. @UpdatesManager.fillSummarizedUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  291. @UpdatesManager.getSummarizedProjectUpdates @project_id, @options, @callback
  292. it "should process any outstanding updates", ->
  293. @UpdatesManager.processUncompressedUpdatesForProject
  294. .calledWith(@project_id)
  295. .should.equal true
  296. it "should get the updates", ->
  297. @PackManager.makeProjectIterator
  298. .calledWith(@project_id, @options.before)
  299. .should.equal true
  300. it "should fill the updates with the user info", ->
  301. @UpdatesManager.fillSummarizedUserInfo
  302. .calledWith(@summarizedUpdates)
  303. .should.equal true
  304. it "should return the updates with the filled details", ->
  305. @callback.calledWith(null, @updatesWithUserInfo).should.equal true
  306. # describe "_extendBatchOfSummarizedUpdates", ->
  307. # beforeEach ->
  308. # @before = Date.now()
  309. # @min_count = 2
  310. # @existingSummarizedUpdates = ["summarized-updates-3"]
  311. # @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  312. # describe "when there are updates to get", ->
  313. # beforeEach ->
  314. # @updates = [
  315. # {op: "mock-op-1", meta: end_ts: @before - 10},
  316. # {op: "mock-op-1", meta: end_ts: @nextBeforeTimestamp = @before - 20}
  317. # ]
  318. # @existingSummarizedUpdates = ["summarized-updates-3"]
  319. # @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  320. # @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  321. # @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  322. # @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  323. # it "should get the updates", ->
  324. # @UpdatesManager.getProjectUpdatesWithUserInfo
  325. # .calledWith(@project_id, { before: @before, limit: 3 * @min_count })
  326. # .should.equal true
  327. # it "should summarize the updates", ->
  328. # @UpdatesManager._summarizeUpdates
  329. # .calledWith(@updates, @existingSummarizedUpdates)
  330. # .should.equal true
  331. # it "should call the callback with the summarized updates and the next before timestamp", ->
  332. # @callback.calledWith(null, @summarizedUpdates, @nextBeforeTimestamp).should.equal true
  333. # describe "when there are no more updates", ->
  334. # beforeEach ->
  335. # @updates = []
  336. # @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  337. # @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  338. # @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  339. # it "should call the callback with the summarized updates and null for nextBeforeTimestamp", ->
  340. # @callback.calledWith(null, @summarizedUpdates, null).should.equal true
  341. # describe "getSummarizedProjectUpdates", ->
  342. # describe "when one batch of updates is enough to meet the limit", ->
  343. # beforeEach ->
  344. # @before = Date.now()
  345. # @min_count = 2
  346. # @updates = ["summarized-updates-3", "summarized-updates-2"]
  347. # @nextBeforeTimestamp = @before - 100
  348. # @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, @nextBeforeTimestamp)
  349. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  350. # it "should get the batch of summarized updates", ->
  351. # @UpdatesManager._extendBatchOfSummarizedUpdates
  352. # .calledWith(@project_id, [], @before, @min_count)
  353. # .should.equal true
  354. # it "should call the callback with the updates", ->
  355. # @callback.calledWith(null, @updates, @nextBeforeTimestamp).should.equal true
  356. # describe "when multiple batches are needed to meet the limit", ->
  357. # beforeEach ->
  358. # @before = Date.now()
  359. # @min_count = 4
  360. # @firstBatch = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  361. # @nextBeforeTimestamp = @before - 100
  362. # @secondBatch = [{ toV: 4, fromV: 4 }, { toV: 3, fromV: 3 }]
  363. # @nextNextBeforeTimestamp = @before - 200
  364. # @UpdatesManager._extendBatchOfSummarizedUpdates = (project_id, existingUpdates, before, desiredLength, callback) =>
  365. # if existingUpdates.length == 0
  366. # callback null, @firstBatch, @nextBeforeTimestamp
  367. # else
  368. # callback null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp
  369. # sinon.spy @UpdatesManager, "_extendBatchOfSummarizedUpdates"
  370. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  371. # it "should get the first batch of summarized updates", ->
  372. # @UpdatesManager._extendBatchOfSummarizedUpdates
  373. # .calledWith(@project_id, [], @before, @min_count)
  374. # .should.equal true
  375. # it "should get the second batch of summarized updates", ->
  376. # @UpdatesManager._extendBatchOfSummarizedUpdates
  377. # .calledWith(@project_id, @firstBatch, @nextBeforeTimestamp, @min_count)
  378. # .should.equal true
  379. # it "should call the callback with all the updates", ->
  380. # @callback.calledWith(null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp).should.equal true
  381. # describe "when the end of the database is hit", ->
  382. # beforeEach ->
  383. # @before = Date.now()
  384. # @min_count = 4
  385. # @updates = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  386. # @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, null)
  387. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  388. # it "should get the batch of summarized updates", ->
  389. # @UpdatesManager._extendBatchOfSummarizedUpdates
  390. # .calledWith(@project_id, [], @before, @min_count)
  391. # .should.equal true
  392. # it "should call the callback with the updates", ->
  393. # @callback.calledWith(null, @updates, null).should.equal true
  394. describe "fillUserInfo", ->
  395. describe "with valid users", ->
  396. beforeEach (done) ->
  397. {ObjectId} = require "mongojs"
  398. @user_id_1 = ObjectId().toString()
  399. @user_id_2 = ObjectId().toString()
  400. @updates = [{
  401. meta:
  402. user_id: @user_id_1
  403. op: "mock-op-1"
  404. }, {
  405. meta:
  406. user_id: @user_id_1
  407. op: "mock-op-2"
  408. }, {
  409. meta:
  410. user_id: @user_id_2
  411. op: "mock-op-3"
  412. }]
  413. @user_info = {}
  414. @user_info[@user_id_1] = email: "user1@sharelatex.com"
  415. @user_info[@user_id_2] = email: "user2@sharelatex.com"
  416. @WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
  417. callback null, @user_info[user_id]
  418. sinon.spy @WebApiManager, "getUserInfo"
  419. @UpdatesManager.fillUserInfo @updates, (error, @results) =>
  420. done()
  421. it "should only call getUserInfo once for each user_id", ->
  422. @WebApiManager.getUserInfo.calledTwice.should.equal true
  423. @WebApiManager.getUserInfo
  424. .calledWith(@user_id_1)
  425. .should.equal true
  426. @WebApiManager.getUserInfo
  427. .calledWith(@user_id_2)
  428. .should.equal true
  429. it "should return the updates with the user info filled", ->
  430. expect(@results).to.deep.equal [{
  431. meta:
  432. user:
  433. email: "user1@sharelatex.com"
  434. op: "mock-op-1"
  435. }, {
  436. meta:
  437. user:
  438. email: "user1@sharelatex.com"
  439. op: "mock-op-2"
  440. }, {
  441. meta:
  442. user:
  443. email: "user2@sharelatex.com"
  444. op: "mock-op-3"
  445. }]
  446. describe "with invalid user ids", ->
  447. beforeEach (done) ->
  448. @updates = [{
  449. meta:
  450. user_id: null
  451. op: "mock-op-1"
  452. }, {
  453. meta:
  454. user_id: "anonymous-user"
  455. op: "mock-op-2"
  456. }]
  457. @WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
  458. callback null, @user_info[user_id]
  459. sinon.spy @WebApiManager, "getUserInfo"
  460. @UpdatesManager.fillUserInfo @updates, (error, @results) =>
  461. done()
  462. it "should not call getUserInfo", ->
  463. @WebApiManager.getUserInfo.called.should.equal false
  464. it "should return the updates without the user info filled", ->
  465. expect(@results).to.deep.equal [{
  466. meta: {}
  467. op: "mock-op-1"
  468. }, {
  469. meta: {}
  470. op: "mock-op-2"
  471. }]
  472. describe "_summarizeUpdates", ->
  473. beforeEach ->
  474. @now = Date.now()
  475. @user_1 = { id: "mock-user-1" }
  476. @user_2 = { id: "mock-user-2" }
  477. it "should concat updates that are close in time", ->
  478. result = @UpdatesManager._summarizeUpdates [{
  479. doc_id: "doc-id-1"
  480. meta:
  481. user_id: @user_1.id
  482. start_ts: @now + 20
  483. end_ts: @now + 30
  484. v: 5
  485. }, {
  486. doc_id: "doc-id-1"
  487. meta:
  488. user_id: @user_2.id
  489. start_ts: @now
  490. end_ts: @now + 10
  491. v: 4
  492. }]
  493. expect(result).to.deep.equal [{
  494. docs:
  495. "doc-id-1":
  496. fromV: 4
  497. toV: 5
  498. meta:
  499. user_ids: [@user_1.id, @user_2.id]
  500. start_ts: @now
  501. end_ts: @now + 30
  502. }]
  503. it "should leave updates that are far apart in time", ->
  504. oneDay = 1000 * 60 * 60 * 24
  505. result = @UpdatesManager._summarizeUpdates [{
  506. doc_id: "doc-id-1"
  507. meta:
  508. user_id: @user_2.id
  509. start_ts: @now + oneDay
  510. end_ts: @now + oneDay + 10
  511. v: 5
  512. }, {
  513. doc_id: "doc-id-1"
  514. meta:
  515. user_id: @user_1.id
  516. start_ts: @now
  517. end_ts: @now + 10
  518. v: 4
  519. }]
  520. expect(result).to.deep.equal [{
  521. docs:
  522. "doc-id-1":
  523. fromV: 5
  524. toV: 5
  525. meta:
  526. user_ids: [@user_2.id]
  527. start_ts: @now + oneDay
  528. end_ts: @now + oneDay + 10
  529. }, {
  530. docs:
  531. "doc-id-1":
  532. fromV: 4
  533. toV: 4
  534. meta:
  535. user_ids: [@user_1.id]
  536. start_ts: @now
  537. end_ts: @now + 10
  538. }]
  539. it "should concat onto existing summarized updates", ->
  540. result = @UpdatesManager._summarizeUpdates [{
  541. doc_id: "doc-id-2"
  542. meta:
  543. user_id: @user_1.id
  544. start_ts: @now + 20
  545. end_ts: @now + 30
  546. v: 5
  547. }, {
  548. doc_id: "doc-id-2"
  549. meta:
  550. user_id: @user_2.id
  551. start_ts: @now
  552. end_ts: @now + 10
  553. v: 4
  554. }], [{
  555. docs:
  556. "doc-id-1":
  557. fromV: 6
  558. toV: 8
  559. meta:
  560. user_ids: [@user_1.id]
  561. start_ts: @now + 40
  562. end_ts: @now + 50
  563. }]
  564. expect(result).to.deep.equal [{
  565. docs:
  566. "doc-id-1":
  567. toV: 8
  568. fromV: 6
  569. "doc-id-2":
  570. toV: 5
  571. fromV: 4
  572. meta:
  573. user_ids: [@user_1.id, @user_2.id]
  574. start_ts: @now
  575. end_ts: @now + 50
  576. }]
  577. it "should include null user values", ->
  578. result = @UpdatesManager._summarizeUpdates [{
  579. doc_id: "doc-id-1"
  580. meta:
  581. user_id: @user_1.id
  582. start_ts: @now + 20
  583. end_ts: @now + 30
  584. v: 5
  585. }, {
  586. doc_id: "doc-id-1"
  587. meta:
  588. user_id: null
  589. start_ts: @now
  590. end_ts: @now + 10
  591. v: 4
  592. }]
  593. expect(result).to.deep.equal [{
  594. docs:
  595. "doc-id-1":
  596. fromV: 4
  597. toV: 5
  598. meta:
  599. user_ids: [@user_1.id, null]
  600. start_ts: @now
  601. end_ts: @now + 30
  602. }]
  603. it "should include null user values, when the null is earlier in the updates list", ->
  604. result = @UpdatesManager._summarizeUpdates [{
  605. doc_id: "doc-id-1"
  606. meta:
  607. user_id: null
  608. start_ts: @now
  609. end_ts: @now + 10
  610. v: 4
  611. }, {
  612. doc_id: "doc-id-1"
  613. meta:
  614. user_id: @user_1.id
  615. start_ts: @now + 20
  616. end_ts: @now + 30
  617. v: 5
  618. }]
  619. expect(result).to.deep.equal [{
  620. docs:
  621. "doc-id-1":
  622. fromV: 4
  623. toV: 5
  624. meta:
  625. user_ids: [null, @user_1.id]
  626. start_ts: @now
  627. end_ts: @now + 30
  628. }]
  629. it "should roll several null user values into one", ->
  630. result = @UpdatesManager._summarizeUpdates [{
  631. doc_id: "doc-id-1"
  632. meta:
  633. user_id: @user_1.id
  634. start_ts: @now + 20
  635. end_ts: @now + 30
  636. v: 5
  637. }, {
  638. doc_id: "doc-id-1"
  639. meta:
  640. user_id: null
  641. start_ts: @now
  642. end_ts: @now + 10
  643. v: 4
  644. }, {
  645. doc_id: "doc-id-1"
  646. meta:
  647. user_id: null
  648. start_ts: @now + 2
  649. end_ts: @now + 4
  650. v: 4
  651. }]
  652. expect(result).to.deep.equal [{
  653. docs:
  654. "doc-id-1":
  655. fromV: 4
  656. toV: 5
  657. meta:
  658. user_ids: [@user_1.id, null]
  659. start_ts: @now
  660. end_ts: @now + 30
  661. }]