UpdatesManagerTests.coffee 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 are out of order", ->
  113. beforeEach ->
  114. @rawUpdates = [{ v: 13, op: "mock-op-13" }, { v: 12, op: "mock-op-12" }]
  115. @UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
  116. it "should call the callback with an error", ->
  117. @callback
  118. .calledWith(new Error)
  119. .should.equal true
  120. it "should not insert any update into mongo", ->
  121. @PackManager.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, @temporary, @callback
  160. it "should get the oldest updates", ->
  161. @RedisManager.getOldestDocUpdates
  162. .calledWith(@doc_id, @UpdatesManager.REDIS_READ_BATCH_SIZE)
  163. .should.equal true
  164. it "should compress and save the updates", ->
  165. @UpdatesManager.compressAndSaveRawUpdates
  166. .calledWith(@project_id, @doc_id, @updates, @temporary)
  167. .should.equal true
  168. it "should delete the batch of uncompressed updates that was just processed", ->
  169. @RedisManager.deleteAppliedDocUpdates
  170. .calledWith(@project_id, @doc_id, @updates)
  171. .should.equal true
  172. it "should call the callback", ->
  173. @callback.called.should.equal true
  174. describe "when there are multiple batches to send", ->
  175. beforeEach (done) ->
  176. @UpdatesManager.REDIS_READ_BATCH_SIZE = 2
  177. @updates = ["mock-update-0", "mock-update-1", "mock-update-2", "mock-update-3", "mock-update-4"]
  178. @redisArray = @updates.slice()
  179. @RedisManager.getOldestDocUpdates = (doc_id, batchSize, callback = (error, updates) ->) =>
  180. updates = @redisArray.slice(0, batchSize)
  181. @redisArray = @redisArray.slice(batchSize)
  182. callback null, updates
  183. sinon.spy @RedisManager, "getOldestDocUpdates"
  184. @RedisManager.expandDocUpdates = (jsonUpdates, callback) =>
  185. callback null, jsonUpdates
  186. sinon.spy @RedisManager, "expandDocUpdates"
  187. @UpdatesManager.processUncompressedUpdates @project_id, @doc_id, @temporary, (args...) =>
  188. @callback(args...)
  189. done()
  190. it "should get the oldest updates in three batches ", ->
  191. @RedisManager.getOldestDocUpdates.callCount.should.equal 3
  192. it "should compress and save the updates in batches", ->
  193. @UpdatesManager.compressAndSaveRawUpdates
  194. .calledWith(@project_id, @doc_id, @updates.slice(0,2), @temporary)
  195. .should.equal true
  196. @UpdatesManager.compressAndSaveRawUpdates
  197. .calledWith(@project_id, @doc_id, @updates.slice(2,4), @temporary)
  198. .should.equal true
  199. @UpdatesManager.compressAndSaveRawUpdates
  200. .calledWith(@project_id, @doc_id, @updates.slice(4,5), @temporary)
  201. .should.equal true
  202. it "should delete the batches of uncompressed updates", ->
  203. @RedisManager.deleteAppliedDocUpdates.callCount.should.equal 3
  204. it "should call the callback", ->
  205. @callback.called.should.equal true
  206. describe "processCompressedUpdatesWithLock", ->
  207. beforeEach ->
  208. @UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, @temporary = "temp mock")
  209. @MongoManager.backportProjectId = sinon.stub().callsArg(2)
  210. @UpdatesManager._processUncompressedUpdates = sinon.stub().callsArg(3)
  211. @LockManager.runWithLock = sinon.stub().callsArg(2)
  212. @UpdatesManager.processUncompressedUpdatesWithLock @project_id, @doc_id, @callback
  213. it "should check if the updates are temporary", ->
  214. @UpdateTrimmer.shouldTrimUpdates
  215. .calledWith(@project_id)
  216. .should.equal true
  217. it "should backport the project id", ->
  218. @MongoManager.backportProjectId
  219. .calledWith(@project_id, @doc_id)
  220. .should.equal true
  221. it "should run processUncompressedUpdates with the lock", ->
  222. @LockManager.runWithLock
  223. .calledWith(
  224. "HistoryLock:#{@doc_id}"
  225. )
  226. .should.equal true
  227. it "should call the callback", ->
  228. @callback.called.should.equal true
  229. describe "getDocUpdates", ->
  230. beforeEach ->
  231. @updates = ["mock-updates"]
  232. @options = { to: "mock-to", limit: "mock-limit" }
  233. @PackManager.getOpsByVersionRange = sinon.stub().callsArgWith(4, null, @updates)
  234. @UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2)
  235. @UpdatesManager.getDocUpdates @project_id, @doc_id, @options, @callback
  236. it "should process outstanding updates", ->
  237. @UpdatesManager.processUncompressedUpdatesWithLock
  238. .calledWith(@project_id, @doc_id)
  239. .should.equal true
  240. it "should get the updates from the database", ->
  241. @PackManager.getOpsByVersionRange
  242. .calledWith(@project_id, @doc_id, @options.from, @options.to)
  243. .should.equal true
  244. it "should return the updates", ->
  245. @callback
  246. .calledWith(null, @updates)
  247. .should.equal true
  248. describe "getDocUpdatesWithUserInfo", ->
  249. beforeEach ->
  250. @updates = ["mock-updates"]
  251. @options = { to: "mock-to", limit: "mock-limit" }
  252. @updatesWithUserInfo = ["updates-with-user-info"]
  253. @UpdatesManager.getDocUpdates = sinon.stub().callsArgWith(3, null, @updates)
  254. @UpdatesManager.fillUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  255. @UpdatesManager.getDocUpdatesWithUserInfo @project_id, @doc_id, @options, @callback
  256. it "should get the updates", ->
  257. @UpdatesManager.getDocUpdates
  258. .calledWith(@project_id, @doc_id, @options)
  259. .should.equal true
  260. it "should file the updates with the user info", ->
  261. @UpdatesManager.fillUserInfo
  262. .calledWith(@updates)
  263. .should.equal true
  264. it "should return the updates with the filled details", ->
  265. @callback.calledWith(null, @updatesWithUserInfo).should.equal true
  266. describe "processUncompressedUpdatesForProject", ->
  267. beforeEach (done) ->
  268. @doc_ids = ["mock-id-1", "mock-id-2"]
  269. @UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, @temporary = "temp mock")
  270. @MongoManager.backportProjectId = sinon.stub().callsArg(2)
  271. @UpdatesManager._processUncompressedUpdatesForDocWithLock = sinon.stub().callsArg(3)
  272. @RedisManager.getDocIdsWithHistoryOps = sinon.stub().callsArgWith(1, null, @doc_ids)
  273. @UpdatesManager.processUncompressedUpdatesForProject @project_id, () =>
  274. @callback()
  275. done()
  276. it "should get all the docs with history ops", ->
  277. @RedisManager.getDocIdsWithHistoryOps
  278. .calledWith(@project_id)
  279. .should.equal true
  280. it "should process the doc ops for the each doc_id", ->
  281. for doc_id in @doc_ids
  282. @UpdatesManager._processUncompressedUpdatesForDocWithLock
  283. .calledWith(@project_id, doc_id, @temporary)
  284. .should.equal true
  285. it "should call the callback", ->
  286. @callback.called.should.equal true
  287. describe "getSummarizedProjectUpdates", ->
  288. beforeEach ->
  289. @updates = [{doc_id: 123, v:456, op: "mock-updates", meta: {user_id: 123, start_ts: 1233, end_ts:1234}}]
  290. @options = { before: "mock-before", limit: "mock-limit" }
  291. @summarizedUpdates = [
  292. {meta: {user_ids: [123], start_ts: 1233, end_ts:1234},docs:{"123":{fromV:456,toV:456}}}
  293. ]
  294. @updatesWithUserInfo = ["updates-with-user-info"]
  295. @done_state = false
  296. @iterator =
  297. next: (cb) =>
  298. @done_state = true
  299. cb(null, @updates)
  300. done: () =>
  301. @done_state
  302. @PackManager.makeProjectIterator = sinon.stub().callsArgWith(2, null, @iterator)
  303. @UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
  304. @UpdatesManager.fillSummarizedUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  305. @UpdatesManager.getSummarizedProjectUpdates @project_id, @options, @callback
  306. it "should process any outstanding updates", ->
  307. @UpdatesManager.processUncompressedUpdatesForProject
  308. .calledWith(@project_id)
  309. .should.equal true
  310. it "should get the updates", ->
  311. @PackManager.makeProjectIterator
  312. .calledWith(@project_id, @options.before)
  313. .should.equal true
  314. it "should fill the updates with the user info", ->
  315. @UpdatesManager.fillSummarizedUserInfo
  316. .calledWith(@summarizedUpdates)
  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_id: @user_1.id
  496. start_ts: @now + 20
  497. end_ts: @now + 30
  498. v: 5
  499. }, {
  500. doc_id: "doc-id-1"
  501. meta:
  502. user_id: @user_2.id
  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. user_ids: [@user_1.id, @user_2.id]
  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_id: @user_2.id
  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_id: @user_1.id
  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. user_ids: [@user_2.id]
  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. user_ids: [@user_1.id]
  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_id: @user_1.id
  558. start_ts: @now + 20
  559. end_ts: @now + 30
  560. v: 5
  561. }, {
  562. doc_id: "doc-id-2"
  563. meta:
  564. user_id: @user_2.id
  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. user_ids: [@user_1.id]
  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. user_ids: [@user_1.id, @user_2.id]
  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_id: @user_1.id
  596. start_ts: @now + 20
  597. end_ts: @now + 30
  598. v: 5
  599. }, {
  600. doc_id: "doc-id-1"
  601. meta:
  602. user_id: 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. user_ids: [@user_1.id, 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_id: null
  622. start_ts: @now
  623. end_ts: @now + 10
  624. v: 4
  625. }, {
  626. doc_id: "doc-id-1"
  627. meta:
  628. user_id: @user_1.id
  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. user_ids: [null, @user_1.id]
  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_id: @user_1.id
  648. start_ts: @now + 20
  649. end_ts: @now + 30
  650. v: 5
  651. }, {
  652. doc_id: "doc-id-1"
  653. meta:
  654. user_id: null
  655. start_ts: @now
  656. end_ts: @now + 10
  657. v: 4
  658. }, {
  659. doc_id: "doc-id-1"
  660. meta:
  661. user_id: 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. user_ids: [@user_1.id, null]
  673. start_ts: @now
  674. end_ts: @now + 30
  675. }]
  676. it "should split updates before a big delete", ->
  677. result = @UpdatesManager._summarizeUpdates [{
  678. doc_id: "doc-id-1"
  679. op: [{ d: "this is a long long long long long delete", p: 34 }]
  680. meta:
  681. user_id: @user_1.id
  682. start_ts: @now + 20
  683. end_ts: @now + 30
  684. v: 5
  685. }, {
  686. doc_id: "doc-id-1"
  687. meta:
  688. user_id: @user_2.id
  689. start_ts: @now
  690. end_ts: @now + 10
  691. v: 4
  692. }]
  693. expect(result).to.deep.equal [{
  694. docs:
  695. "doc-id-1":
  696. fromV: 5
  697. toV: 5
  698. meta:
  699. user_ids: [@user_1.id]
  700. start_ts: @now + 20
  701. end_ts: @now + 30
  702. }, {
  703. docs:
  704. "doc-id-1":
  705. fromV: 4
  706. toV: 4
  707. meta:
  708. user_ids: [@user_2.id]
  709. start_ts: @now
  710. end_ts: @now + 10
  711. }]