UpdatesManagerTests.coffee 24 KB

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