UpdatesManagerTests.coffee 25 KB

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