UpdatesManagerTests.coffee 27 KB

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