UpdatesManagerTests.coffee 25 KB

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