UpdatesManagerTests.coffee 21 KB

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