UpdatesManagerTests.coffee 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 "getProjectUpdates", ->
  274. # beforeEach ->
  275. # @updates = ["mock-updates"]
  276. # @options = { before: "mock-before", limit: "mock-limit" }
  277. # @MongoManager.getProjectUpdates = sinon.stub().callsArgWith(2, null, @updates)
  278. # @UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1)
  279. # @UpdatesManager.getProjectUpdates @project_id, @options, @callback
  280. # it "should process any outstanding updates", ->
  281. # @UpdatesManager.processUncompressedUpdatesForProject
  282. # .calledWith(@project_id)
  283. # .should.equal true
  284. # it "should get the updates from the database", ->
  285. # @MongoManager.getProjectUpdates
  286. # .calledWith(@project_id, @options)
  287. # .should.equal true
  288. # it "should return the updates", ->
  289. # @callback
  290. # .calledWith(null, @updates)
  291. # .should.equal true
  292. # describe "getProjectUpdatesWithUserInfo", ->
  293. # beforeEach ->
  294. # @updates = ["mock-updates"]
  295. # @options = { before: "mock-before", limit: "mock-limit" }
  296. # @updatesWithUserInfo = ["updates-with-user-info"]
  297. # @UpdatesManager.getProjectUpdates = sinon.stub().callsArgWith(2, null, @updates)
  298. # @UpdatesManager.fillUserInfo = sinon.stub().callsArgWith(1, null, @updatesWithUserInfo)
  299. # @UpdatesManager.getProjectUpdatesWithUserInfo @project_id, @options, @callback
  300. # it "should get the updates", ->
  301. # @UpdatesManager.getProjectUpdates
  302. # .calledWith(@project_id, @options)
  303. # .should.equal true
  304. # it "should file the updates with the user info", ->
  305. # @UpdatesManager.fillUserInfo
  306. # .calledWith(@updates)
  307. # .should.equal true
  308. # it "should return the updates with the filled details", ->
  309. # @callback.calledWith(null, @updatesWithUserInfo).should.equal true
  310. # describe "_extendBatchOfSummarizedUpdates", ->
  311. # beforeEach ->
  312. # @before = Date.now()
  313. # @min_count = 2
  314. # @existingSummarizedUpdates = ["summarized-updates-3"]
  315. # @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  316. # describe "when there are updates to get", ->
  317. # beforeEach ->
  318. # @updates = [
  319. # {op: "mock-op-1", meta: end_ts: @before - 10},
  320. # {op: "mock-op-1", meta: end_ts: @nextBeforeTimestamp = @before - 20}
  321. # ]
  322. # @existingSummarizedUpdates = ["summarized-updates-3"]
  323. # @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  324. # @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  325. # @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  326. # @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  327. # it "should get the updates", ->
  328. # @UpdatesManager.getProjectUpdatesWithUserInfo
  329. # .calledWith(@project_id, { before: @before, limit: 3 * @min_count })
  330. # .should.equal true
  331. # it "should summarize the updates", ->
  332. # @UpdatesManager._summarizeUpdates
  333. # .calledWith(@updates, @existingSummarizedUpdates)
  334. # .should.equal true
  335. # it "should call the callback with the summarized updates and the next before timestamp", ->
  336. # @callback.calledWith(null, @summarizedUpdates, @nextBeforeTimestamp).should.equal true
  337. # describe "when there are no more updates", ->
  338. # beforeEach ->
  339. # @updates = []
  340. # @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  341. # @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  342. # @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  343. # it "should call the callback with the summarized updates and null for nextBeforeTimestamp", ->
  344. # @callback.calledWith(null, @summarizedUpdates, null).should.equal true
  345. # describe "getSummarizedProjectUpdates", ->
  346. # describe "when one batch of updates is enough to meet the limit", ->
  347. # beforeEach ->
  348. # @before = Date.now()
  349. # @min_count = 2
  350. # @updates = ["summarized-updates-3", "summarized-updates-2"]
  351. # @nextBeforeTimestamp = @before - 100
  352. # @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, @nextBeforeTimestamp)
  353. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  354. # it "should get the batch of summarized updates", ->
  355. # @UpdatesManager._extendBatchOfSummarizedUpdates
  356. # .calledWith(@project_id, [], @before, @min_count)
  357. # .should.equal true
  358. # it "should call the callback with the updates", ->
  359. # @callback.calledWith(null, @updates, @nextBeforeTimestamp).should.equal true
  360. # describe "when multiple batches are needed to meet the limit", ->
  361. # beforeEach ->
  362. # @before = Date.now()
  363. # @min_count = 4
  364. # @firstBatch = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  365. # @nextBeforeTimestamp = @before - 100
  366. # @secondBatch = [{ toV: 4, fromV: 4 }, { toV: 3, fromV: 3 }]
  367. # @nextNextBeforeTimestamp = @before - 200
  368. # @UpdatesManager._extendBatchOfSummarizedUpdates = (project_id, existingUpdates, before, desiredLength, callback) =>
  369. # if existingUpdates.length == 0
  370. # callback null, @firstBatch, @nextBeforeTimestamp
  371. # else
  372. # callback null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp
  373. # sinon.spy @UpdatesManager, "_extendBatchOfSummarizedUpdates"
  374. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  375. # it "should get the first batch of summarized updates", ->
  376. # @UpdatesManager._extendBatchOfSummarizedUpdates
  377. # .calledWith(@project_id, [], @before, @min_count)
  378. # .should.equal true
  379. # it "should get the second batch of summarized updates", ->
  380. # @UpdatesManager._extendBatchOfSummarizedUpdates
  381. # .calledWith(@project_id, @firstBatch, @nextBeforeTimestamp, @min_count)
  382. # .should.equal true
  383. # it "should call the callback with all the updates", ->
  384. # @callback.calledWith(null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp).should.equal true
  385. # describe "when the end of the database is hit", ->
  386. # beforeEach ->
  387. # @before = Date.now()
  388. # @min_count = 4
  389. # @updates = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  390. # @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, null)
  391. # @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  392. # it "should get the batch of summarized updates", ->
  393. # @UpdatesManager._extendBatchOfSummarizedUpdates
  394. # .calledWith(@project_id, [], @before, @min_count)
  395. # .should.equal true
  396. # it "should call the callback with the updates", ->
  397. # @callback.calledWith(null, @updates, null).should.equal true
  398. # describe "fillUserInfo", ->
  399. # describe "with valid users", ->
  400. # beforeEach (done) ->
  401. # {ObjectId} = require "mongojs"
  402. # @user_id_1 = ObjectId().toString()
  403. # @user_id_2 = ObjectId().toString()
  404. # @updates = [{
  405. # meta:
  406. # user_id: @user_id_1
  407. # op: "mock-op-1"
  408. # }, {
  409. # meta:
  410. # user_id: @user_id_1
  411. # op: "mock-op-2"
  412. # }, {
  413. # meta:
  414. # user_id: @user_id_2
  415. # op: "mock-op-3"
  416. # }]
  417. # @user_info = {}
  418. # @user_info[@user_id_1] = email: "user1@sharelatex.com"
  419. # @user_info[@user_id_2] = email: "user2@sharelatex.com"
  420. # @WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
  421. # callback null, @user_info[user_id]
  422. # sinon.spy @WebApiManager, "getUserInfo"
  423. # @UpdatesManager.fillUserInfo @updates, (error, @results) =>
  424. # done()
  425. # it "should only call getUserInfo once for each user_id", ->
  426. # @WebApiManager.getUserInfo.calledTwice.should.equal true
  427. # @WebApiManager.getUserInfo
  428. # .calledWith(@user_id_1)
  429. # .should.equal true
  430. # @WebApiManager.getUserInfo
  431. # .calledWith(@user_id_2)
  432. # .should.equal true
  433. # it "should return the updates with the user info filled", ->
  434. # expect(@results).to.deep.equal [{
  435. # meta:
  436. # user:
  437. # email: "user1@sharelatex.com"
  438. # op: "mock-op-1"
  439. # }, {
  440. # meta:
  441. # user:
  442. # email: "user1@sharelatex.com"
  443. # op: "mock-op-2"
  444. # }, {
  445. # meta:
  446. # user:
  447. # email: "user2@sharelatex.com"
  448. # op: "mock-op-3"
  449. # }]
  450. # describe "with invalid user ids", ->
  451. # beforeEach (done) ->
  452. # @updates = [{
  453. # meta:
  454. # user_id: null
  455. # op: "mock-op-1"
  456. # }, {
  457. # meta:
  458. # user_id: "anonymous-user"
  459. # op: "mock-op-2"
  460. # }]
  461. # @WebApiManager.getUserInfo = (user_id, callback = (error, userInfo) ->) =>
  462. # callback null, @user_info[user_id]
  463. # sinon.spy @WebApiManager, "getUserInfo"
  464. # @UpdatesManager.fillUserInfo @updates, (error, @results) =>
  465. # done()
  466. # it "should not call getUserInfo", ->
  467. # @WebApiManager.getUserInfo.called.should.equal false
  468. # it "should return the updates without the user info filled", ->
  469. # expect(@results).to.deep.equal [{
  470. # meta: {}
  471. # op: "mock-op-1"
  472. # }, {
  473. # meta: {}
  474. # op: "mock-op-2"
  475. # }]
  476. describe "_summarizeUpdates", ->
  477. beforeEach ->
  478. @now = Date.now()
  479. @user_1 = { id: "mock-user-1" }
  480. @user_2 = { id: "mock-user-2" }
  481. it "should concat updates that are close in time", ->
  482. result = @UpdatesManager._summarizeUpdates [{
  483. doc_id: "doc-id-1"
  484. meta:
  485. user_id: @user_1.id
  486. start_ts: @now + 20
  487. end_ts: @now + 30
  488. v: 5
  489. }, {
  490. doc_id: "doc-id-1"
  491. meta:
  492. user_id: @user_2.id
  493. start_ts: @now
  494. end_ts: @now + 10
  495. v: 4
  496. }]
  497. expect(result).to.deep.equal [{
  498. docs:
  499. "doc-id-1":
  500. fromV: 4
  501. toV: 5
  502. meta:
  503. user_ids: [@user_1.id, @user_2.id]
  504. start_ts: @now
  505. end_ts: @now + 30
  506. }]
  507. it "should leave updates that are far apart in time", ->
  508. oneDay = 1000 * 60 * 60 * 24
  509. result = @UpdatesManager._summarizeUpdates [{
  510. doc_id: "doc-id-1"
  511. meta:
  512. user_id: @user_2.id
  513. start_ts: @now + oneDay
  514. end_ts: @now + oneDay + 10
  515. v: 5
  516. }, {
  517. doc_id: "doc-id-1"
  518. meta:
  519. user_id: @user_1.id
  520. start_ts: @now
  521. end_ts: @now + 10
  522. v: 4
  523. }]
  524. expect(result).to.deep.equal [{
  525. docs:
  526. "doc-id-1":
  527. fromV: 5
  528. toV: 5
  529. meta:
  530. user_ids: [@user_2.id]
  531. start_ts: @now + oneDay
  532. end_ts: @now + oneDay + 10
  533. }, {
  534. docs:
  535. "doc-id-1":
  536. fromV: 4
  537. toV: 4
  538. meta:
  539. user_ids: [@user_1.id]
  540. start_ts: @now
  541. end_ts: @now + 10
  542. }]
  543. it "should concat onto existing summarized updates", ->
  544. result = @UpdatesManager._summarizeUpdates [{
  545. doc_id: "doc-id-2"
  546. meta:
  547. user_id: @user_1.id
  548. start_ts: @now + 20
  549. end_ts: @now + 30
  550. v: 5
  551. }, {
  552. doc_id: "doc-id-2"
  553. meta:
  554. user_id: @user_2.id
  555. start_ts: @now
  556. end_ts: @now + 10
  557. v: 4
  558. }], [{
  559. docs:
  560. "doc-id-1":
  561. fromV: 6
  562. toV: 8
  563. meta:
  564. user_ids: [@user_1.id]
  565. start_ts: @now + 40
  566. end_ts: @now + 50
  567. }]
  568. expect(result).to.deep.equal [{
  569. docs:
  570. "doc-id-1":
  571. toV: 8
  572. fromV: 6
  573. "doc-id-2":
  574. toV: 5
  575. fromV: 4
  576. meta:
  577. user_ids: [@user_1.id, @user_2.id]
  578. start_ts: @now
  579. end_ts: @now + 50
  580. }]
  581. it "should include null user values", ->
  582. result = @UpdatesManager._summarizeUpdates [{
  583. doc_id: "doc-id-1"
  584. meta:
  585. user_id: @user_1.id
  586. start_ts: @now + 20
  587. end_ts: @now + 30
  588. v: 5
  589. }, {
  590. doc_id: "doc-id-1"
  591. meta:
  592. user_id: null
  593. start_ts: @now
  594. end_ts: @now + 10
  595. v: 4
  596. }]
  597. expect(result).to.deep.equal [{
  598. docs:
  599. "doc-id-1":
  600. fromV: 4
  601. toV: 5
  602. meta:
  603. user_ids: [@user_1.id, null]
  604. start_ts: @now
  605. end_ts: @now + 30
  606. }]
  607. it "should include null user values, when the null is earlier in the updates list", ->
  608. result = @UpdatesManager._summarizeUpdates [{
  609. doc_id: "doc-id-1"
  610. meta:
  611. user_id: null
  612. start_ts: @now
  613. end_ts: @now + 10
  614. v: 4
  615. }, {
  616. doc_id: "doc-id-1"
  617. meta:
  618. user_id: @user_1.id
  619. start_ts: @now + 20
  620. end_ts: @now + 30
  621. v: 5
  622. }]
  623. expect(result).to.deep.equal [{
  624. docs:
  625. "doc-id-1":
  626. fromV: 4
  627. toV: 5
  628. meta:
  629. user_ids: [null, @user_1.id]
  630. start_ts: @now
  631. end_ts: @now + 30
  632. }]
  633. it "should roll several null user values into one", ->
  634. result = @UpdatesManager._summarizeUpdates [{
  635. doc_id: "doc-id-1"
  636. meta:
  637. user_id: @user_1.id
  638. start_ts: @now + 20
  639. end_ts: @now + 30
  640. v: 5
  641. }, {
  642. doc_id: "doc-id-1"
  643. meta:
  644. user_id: null
  645. start_ts: @now
  646. end_ts: @now + 10
  647. v: 4
  648. }, {
  649. doc_id: "doc-id-1"
  650. meta:
  651. user_id: null
  652. start_ts: @now + 2
  653. end_ts: @now + 4
  654. v: 4
  655. }]
  656. expect(result).to.deep.equal [{
  657. docs:
  658. "doc-id-1":
  659. fromV: 4
  660. toV: 5
  661. meta:
  662. user_ids: [@user_1.id, null]
  663. start_ts: @now
  664. end_ts: @now + 30
  665. }]