UpdatesManagerTests.coffee 25 KB

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