UpdatesManagerTests.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const sinon = require('sinon');
  17. const chai = require('chai');
  18. const should = chai.should();
  19. const { expect } = chai;
  20. const modulePath = "../../../../app/js/UpdatesManager.js";
  21. const SandboxedModule = require('sandboxed-module');
  22. describe("UpdatesManager", function() {
  23. beforeEach(function() {
  24. this.UpdatesManager = SandboxedModule.require(modulePath, { requires: {
  25. "./UpdateCompressor": (this.UpdateCompressor = {}),
  26. "./MongoManager" : (this.MongoManager = {}),
  27. "./PackManager" : (this.PackManager = {}),
  28. "./RedisManager" : (this.RedisManager = {}),
  29. "./LockManager" : (this.LockManager = {}),
  30. "./WebApiManager": (this.WebApiManager = {}),
  31. "./UpdateTrimmer": (this.UpdateTrimmer = {}),
  32. "./DocArchiveManager": (this.DocArchiveManager = {}),
  33. "logger-sharelatex": { log: sinon.stub(), error: sinon.stub() },
  34. "settings-sharelatex": {
  35. redis: { lock: { key_schema: {
  36. historyLock({doc_id}) { return `HistoryLock:${doc_id}`; }
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. );
  44. this.doc_id = "doc-id-123";
  45. this.project_id = "project-id-123";
  46. this.callback = sinon.stub();
  47. return this.temporary = "temp-mock";
  48. });
  49. describe("compressAndSaveRawUpdates", function() {
  50. describe("when there are no raw ops", function() {
  51. beforeEach(function() {
  52. this.MongoManager.peekLastCompressedUpdate = sinon.stub();
  53. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, [], this.temporary, this.callback);
  54. });
  55. it("should not need to access the database", function() {
  56. return this.MongoManager.peekLastCompressedUpdate.called.should.equal(false);
  57. });
  58. return it("should call the callback", function() {
  59. return this.callback.called.should.equal(true);
  60. });
  61. });
  62. describe("when there is no compressed history to begin with", function() {
  63. beforeEach(function() {
  64. this.rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }];
  65. this.compressedUpdates = [ { v: 13, op: "compressed-op-12" } ];
  66. this.MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null);
  67. this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5);
  68. this.UpdateCompressor.compressRawUpdates = sinon.stub().returns(this.compressedUpdates);
  69. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  70. });
  71. it("should look at the last compressed op", function() {
  72. return this.MongoManager.peekLastCompressedUpdate
  73. .calledWith(this.doc_id)
  74. .should.equal(true);
  75. });
  76. it("should save the compressed ops as a pack", function() {
  77. return this.PackManager.insertCompressedUpdates
  78. .calledWith(this.project_id, this.doc_id, null, this.compressedUpdates, this.temporary)
  79. .should.equal(true);
  80. });
  81. return it("should call the callback", function() {
  82. return this.callback.called.should.equal(true);
  83. });
  84. });
  85. describe("when the raw ops need appending to existing history", function() {
  86. beforeEach(function() {
  87. this.lastCompressedUpdate = { v: 11, op: "compressed-op-11" };
  88. this.compressedUpdates = [ { v: 12, op: "compressed-op-11+12" }, { v: 13, op: "compressed-op-12" } ];
  89. this.MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, this.lastCompressedUpdate, this.lastCompressedUpdate.v);
  90. this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5);
  91. return this.UpdateCompressor.compressRawUpdates = sinon.stub().returns(this.compressedUpdates);
  92. });
  93. describe("when the raw ops start where the existing history ends", function() {
  94. beforeEach(function() {
  95. this.rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }];
  96. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  97. });
  98. it("should look at the last compressed op", function() {
  99. return this.MongoManager.peekLastCompressedUpdate
  100. .calledWith(this.doc_id)
  101. .should.equal(true);
  102. });
  103. it("should compress the raw ops", function() {
  104. return this.UpdateCompressor.compressRawUpdates
  105. .calledWith(null, this.rawUpdates)
  106. .should.equal(true);
  107. });
  108. it("should save the new compressed ops into a pack", function() {
  109. return this.PackManager.insertCompressedUpdates
  110. .calledWith(this.project_id, this.doc_id, this.lastCompressedUpdate, this.compressedUpdates, this.temporary)
  111. .should.equal(true);
  112. });
  113. return it("should call the callback", function() {
  114. return this.callback.called.should.equal(true);
  115. });
  116. });
  117. describe("when the raw ops start where the existing history ends and the history is in a pack", function() {
  118. beforeEach(function() {
  119. this.lastCompressedUpdate = {pack: [{ v: 11, op: "compressed-op-11" }], v:11};
  120. this.rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }];
  121. this.MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, this.lastCompressedUpdate, this.lastCompressedUpdate.v);
  122. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  123. });
  124. it("should look at the last compressed op", function() {
  125. return this.MongoManager.peekLastCompressedUpdate
  126. .calledWith(this.doc_id)
  127. .should.equal(true);
  128. });
  129. it("should compress the raw ops", function() {
  130. return this.UpdateCompressor.compressRawUpdates
  131. .calledWith(null, this.rawUpdates)
  132. .should.equal(true);
  133. });
  134. it("should save the new compressed ops into a pack", function() {
  135. return this.PackManager.insertCompressedUpdates
  136. .calledWith(this.project_id, this.doc_id, this.lastCompressedUpdate, this.compressedUpdates, this.temporary)
  137. .should.equal(true);
  138. });
  139. return it("should call the callback", function() {
  140. return this.callback.called.should.equal(true);
  141. });
  142. });
  143. describe("when some raw ops are passed that have already been compressed", function() {
  144. beforeEach(function() {
  145. this.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" }];
  146. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  147. });
  148. return it("should only compress the more recent raw ops", function() {
  149. return this.UpdateCompressor.compressRawUpdates
  150. .calledWith(null, this.rawUpdates.slice(-2))
  151. .should.equal(true);
  152. });
  153. });
  154. describe("when the raw ops do not follow from the last compressed op version", function() {
  155. beforeEach(function() {
  156. this.rawUpdates = [{ v: 13, op: "mock-op-13" }];
  157. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  158. });
  159. it("should call the callback with an error", function() {
  160. return this.callback
  161. .calledWith(sinon.match.has('message', "Tried to apply raw op at version 13 to last compressed update with version 11 from unknown time"))
  162. .should.equal(true);
  163. });
  164. return it("should not insert any update into mongo", function() {
  165. return this.PackManager.insertCompressedUpdates.called.should.equal(false);
  166. });
  167. });
  168. return describe("when the raw ops are out of order", function() {
  169. beforeEach(function() {
  170. this.rawUpdates = [{ v: 13, op: "mock-op-13" }, { v: 12, op: "mock-op-12" }];
  171. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  172. });
  173. it("should call the callback with an error", function() {
  174. return this.callback
  175. .calledWith(sinon.match.has('message'))
  176. .should.equal(true);
  177. });
  178. return it("should not insert any update into mongo", function() {
  179. return this.PackManager.insertCompressedUpdates.called.should.equal(false);
  180. });
  181. });
  182. });
  183. return describe("when the raw ops need appending to existing history which is in S3", function() {
  184. beforeEach(function() {
  185. this.lastCompressedUpdate = null;
  186. this.lastVersion = 11;
  187. this.compressedUpdates = [ { v: 13, op: "compressed-op-12" } ];
  188. this.MongoManager.peekLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null, this.lastVersion);
  189. this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5);
  190. return this.UpdateCompressor.compressRawUpdates = sinon.stub().returns(this.compressedUpdates);
  191. });
  192. return describe("when the raw ops start where the existing history ends", function() {
  193. beforeEach(function() {
  194. this.rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }];
  195. return this.UpdatesManager.compressAndSaveRawUpdates(this.project_id, this.doc_id, this.rawUpdates, this.temporary, this.callback);
  196. });
  197. it("should try to look at the last compressed op", function() {
  198. return this.MongoManager.peekLastCompressedUpdate
  199. .calledWith(this.doc_id)
  200. .should.equal(true);
  201. });
  202. it("should compress the last compressed op and the raw ops", function() {
  203. return this.UpdateCompressor.compressRawUpdates
  204. .calledWith(this.lastCompressedUpdate, this.rawUpdates)
  205. .should.equal(true);
  206. });
  207. it("should save the compressed ops", function() {
  208. return this.PackManager.insertCompressedUpdates
  209. .calledWith(this.project_id, this.doc_id, null, this.compressedUpdates, this.temporary)
  210. .should.equal(true);
  211. });
  212. return it("should call the callback", function() {
  213. return this.callback.called.should.equal(true);
  214. });
  215. });
  216. });
  217. });
  218. describe("processUncompressedUpdates", function() {
  219. beforeEach(function() {
  220. this.UpdatesManager.compressAndSaveRawUpdates = sinon.stub().callsArgWith(4);
  221. this.RedisManager.deleteAppliedDocUpdates = sinon.stub().callsArg(3);
  222. this.MongoManager.backportProjectId = sinon.stub().callsArg(2);
  223. return this.UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, (this.temporary = "temp mock"));
  224. });
  225. describe("when there is fewer than one batch to send", function() {
  226. beforeEach(function() {
  227. this.updates = ["mock-update"];
  228. this.RedisManager.getOldestDocUpdates = sinon.stub().callsArgWith(2, null, this.updates);
  229. this.RedisManager.expandDocUpdates = sinon.stub().callsArgWith(1, null, this.updates);
  230. return this.UpdatesManager.processUncompressedUpdates(this.project_id, this.doc_id, this.temporary, this.callback);
  231. });
  232. it("should get the oldest updates", function() {
  233. return this.RedisManager.getOldestDocUpdates
  234. .calledWith(this.doc_id, this.UpdatesManager.REDIS_READ_BATCH_SIZE)
  235. .should.equal(true);
  236. });
  237. it("should compress and save the updates", function() {
  238. return this.UpdatesManager.compressAndSaveRawUpdates
  239. .calledWith(this.project_id, this.doc_id, this.updates, this.temporary)
  240. .should.equal(true);
  241. });
  242. it("should delete the batch of uncompressed updates that was just processed", function() {
  243. return this.RedisManager.deleteAppliedDocUpdates
  244. .calledWith(this.project_id, this.doc_id, this.updates)
  245. .should.equal(true);
  246. });
  247. return it("should call the callback", function() {
  248. return this.callback.called.should.equal(true);
  249. });
  250. });
  251. return describe("when there are multiple batches to send", function() {
  252. beforeEach(function(done) {
  253. this.UpdatesManager.REDIS_READ_BATCH_SIZE = 2;
  254. this.updates = ["mock-update-0", "mock-update-1", "mock-update-2", "mock-update-3", "mock-update-4"];
  255. this.redisArray = this.updates.slice();
  256. this.RedisManager.getOldestDocUpdates = (doc_id, batchSize, callback) => {
  257. if (callback == null) { callback = function(error, updates) {}; }
  258. const updates = this.redisArray.slice(0, batchSize);
  259. this.redisArray = this.redisArray.slice(batchSize);
  260. return callback(null, updates);
  261. };
  262. sinon.spy(this.RedisManager, "getOldestDocUpdates");
  263. this.RedisManager.expandDocUpdates = (jsonUpdates, callback) => {
  264. return callback(null, jsonUpdates);
  265. };
  266. sinon.spy(this.RedisManager, "expandDocUpdates");
  267. return this.UpdatesManager.processUncompressedUpdates(this.project_id, this.doc_id, this.temporary, (...args) => {
  268. this.callback(...Array.from(args || []));
  269. return done();
  270. });
  271. });
  272. it("should get the oldest updates in three batches ", function() {
  273. return this.RedisManager.getOldestDocUpdates.callCount.should.equal(3);
  274. });
  275. it("should compress and save the updates in batches", function() {
  276. this.UpdatesManager.compressAndSaveRawUpdates
  277. .calledWith(this.project_id, this.doc_id, this.updates.slice(0,2), this.temporary)
  278. .should.equal(true);
  279. this.UpdatesManager.compressAndSaveRawUpdates
  280. .calledWith(this.project_id, this.doc_id, this.updates.slice(2,4), this.temporary)
  281. .should.equal(true);
  282. return this.UpdatesManager.compressAndSaveRawUpdates
  283. .calledWith(this.project_id, this.doc_id, this.updates.slice(4,5), this.temporary)
  284. .should.equal(true);
  285. });
  286. it("should delete the batches of uncompressed updates", function() {
  287. return this.RedisManager.deleteAppliedDocUpdates.callCount.should.equal(3);
  288. });
  289. return it("should call the callback", function() {
  290. return this.callback.called.should.equal(true);
  291. });
  292. });
  293. });
  294. describe("processCompressedUpdatesWithLock", function() {
  295. beforeEach(function() {
  296. this.UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, (this.temporary = "temp mock"));
  297. this.MongoManager.backportProjectId = sinon.stub().callsArg(2);
  298. this.UpdatesManager._processUncompressedUpdates = sinon.stub().callsArg(3);
  299. this.LockManager.runWithLock = sinon.stub().callsArg(2);
  300. return this.UpdatesManager.processUncompressedUpdatesWithLock(this.project_id, this.doc_id, this.callback);
  301. });
  302. it("should check if the updates are temporary", function() {
  303. return this.UpdateTrimmer.shouldTrimUpdates
  304. .calledWith(this.project_id)
  305. .should.equal(true);
  306. });
  307. it("should backport the project id", function() {
  308. return this.MongoManager.backportProjectId
  309. .calledWith(this.project_id, this.doc_id)
  310. .should.equal(true);
  311. });
  312. it("should run processUncompressedUpdates with the lock", function() {
  313. return this.LockManager.runWithLock
  314. .calledWith(
  315. `HistoryLock:${this.doc_id}`
  316. )
  317. .should.equal(true);
  318. });
  319. return it("should call the callback", function() {
  320. return this.callback.called.should.equal(true);
  321. });
  322. });
  323. describe("getDocUpdates", function() {
  324. beforeEach(function() {
  325. this.updates = ["mock-updates"];
  326. this.options = { to: "mock-to", limit: "mock-limit" };
  327. this.PackManager.getOpsByVersionRange = sinon.stub().callsArgWith(4, null, this.updates);
  328. this.UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2);
  329. return this.UpdatesManager.getDocUpdates(this.project_id, this.doc_id, this.options, this.callback);
  330. });
  331. it("should process outstanding updates", function() {
  332. return this.UpdatesManager.processUncompressedUpdatesWithLock
  333. .calledWith(this.project_id, this.doc_id)
  334. .should.equal(true);
  335. });
  336. it("should get the updates from the database", function() {
  337. return this.PackManager.getOpsByVersionRange
  338. .calledWith(this.project_id, this.doc_id, this.options.from, this.options.to)
  339. .should.equal(true);
  340. });
  341. return it("should return the updates", function() {
  342. return this.callback
  343. .calledWith(null, this.updates)
  344. .should.equal(true);
  345. });
  346. });
  347. describe("getDocUpdatesWithUserInfo", function() {
  348. beforeEach(function() {
  349. this.updates = ["mock-updates"];
  350. this.options = { to: "mock-to", limit: "mock-limit" };
  351. this.updatesWithUserInfo = ["updates-with-user-info"];
  352. this.UpdatesManager.getDocUpdates = sinon.stub().callsArgWith(3, null, this.updates);
  353. this.UpdatesManager.fillUserInfo = sinon.stub().callsArgWith(1, null, this.updatesWithUserInfo);
  354. return this.UpdatesManager.getDocUpdatesWithUserInfo(this.project_id, this.doc_id, this.options, this.callback);
  355. });
  356. it("should get the updates", function() {
  357. return this.UpdatesManager.getDocUpdates
  358. .calledWith(this.project_id, this.doc_id, this.options)
  359. .should.equal(true);
  360. });
  361. it("should file the updates with the user info", function() {
  362. return this.UpdatesManager.fillUserInfo
  363. .calledWith(this.updates)
  364. .should.equal(true);
  365. });
  366. return it("should return the updates with the filled details", function() {
  367. return this.callback.calledWith(null, this.updatesWithUserInfo).should.equal(true);
  368. });
  369. });
  370. describe("processUncompressedUpdatesForProject", function() {
  371. beforeEach(function(done) {
  372. this.doc_ids = ["mock-id-1", "mock-id-2"];
  373. this.UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, (this.temporary = "temp mock"));
  374. this.MongoManager.backportProjectId = sinon.stub().callsArg(2);
  375. this.UpdatesManager._processUncompressedUpdatesForDocWithLock = sinon.stub().callsArg(3);
  376. this.RedisManager.getDocIdsWithHistoryOps = sinon.stub().callsArgWith(1, null, this.doc_ids);
  377. return this.UpdatesManager.processUncompressedUpdatesForProject(this.project_id, () => {
  378. this.callback();
  379. return done();
  380. });
  381. });
  382. it("should get all the docs with history ops", function() {
  383. return this.RedisManager.getDocIdsWithHistoryOps
  384. .calledWith(this.project_id)
  385. .should.equal(true);
  386. });
  387. it("should process the doc ops for the each doc_id", function() {
  388. return Array.from(this.doc_ids).map((doc_id) =>
  389. this.UpdatesManager._processUncompressedUpdatesForDocWithLock
  390. .calledWith(this.project_id, doc_id, this.temporary)
  391. .should.equal(true));
  392. });
  393. return it("should call the callback", function() {
  394. return this.callback.called.should.equal(true);
  395. });
  396. });
  397. describe("getSummarizedProjectUpdates", function() {
  398. beforeEach(function() {
  399. this.updates = [{doc_id: 123, v:456, op: "mock-updates", meta: {user_id: 123, start_ts: 1233, end_ts:1234}}];
  400. this.options = { before: "mock-before", limit: "mock-limit" };
  401. this.summarizedUpdates = [
  402. {meta: {user_ids: [123], start_ts: 1233, end_ts:1234},docs:{"123":{fromV:456,toV:456}}}
  403. ];
  404. this.updatesWithUserInfo = ["updates-with-user-info"];
  405. this.done_state = false;
  406. this.iterator = {
  407. next: cb => {
  408. this.done_state = true;
  409. return cb(null, this.updates);
  410. },
  411. done: () => {
  412. return this.done_state;
  413. }
  414. };
  415. this.PackManager.makeProjectIterator = sinon.stub().callsArgWith(2, null, this.iterator);
  416. this.UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1);
  417. this.UpdatesManager.fillSummarizedUserInfo = sinon.stub().callsArgWith(1, null, this.updatesWithUserInfo);
  418. return this.UpdatesManager.getSummarizedProjectUpdates(this.project_id, this.options, this.callback);
  419. });
  420. it("should process any outstanding updates", function() {
  421. return this.UpdatesManager.processUncompressedUpdatesForProject
  422. .calledWith(this.project_id)
  423. .should.equal(true);
  424. });
  425. it("should get the updates", function() {
  426. return this.PackManager.makeProjectIterator
  427. .calledWith(this.project_id, this.options.before)
  428. .should.equal(true);
  429. });
  430. it("should fill the updates with the user info", function() {
  431. return this.UpdatesManager.fillSummarizedUserInfo
  432. .calledWith(this.summarizedUpdates)
  433. .should.equal(true);
  434. });
  435. return it("should return the updates with the filled details", function() {
  436. return this.callback.calledWith(null, this.updatesWithUserInfo).should.equal(true);
  437. });
  438. });
  439. // describe "_extendBatchOfSummarizedUpdates", ->
  440. // beforeEach ->
  441. // @before = Date.now()
  442. // @min_count = 2
  443. // @existingSummarizedUpdates = ["summarized-updates-3"]
  444. // @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  445. // describe "when there are updates to get", ->
  446. // beforeEach ->
  447. // @updates = [
  448. // {op: "mock-op-1", meta: end_ts: @before - 10},
  449. // {op: "mock-op-1", meta: end_ts: @nextBeforeTimestamp = @before - 20}
  450. // ]
  451. // @existingSummarizedUpdates = ["summarized-updates-3"]
  452. // @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  453. // @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  454. // @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  455. // @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  456. // it "should get the updates", ->
  457. // @UpdatesManager.getProjectUpdatesWithUserInfo
  458. // .calledWith(@project_id, { before: @before, limit: 3 * @min_count })
  459. // .should.equal true
  460. // it "should summarize the updates", ->
  461. // @UpdatesManager._summarizeUpdates
  462. // .calledWith(@updates, @existingSummarizedUpdates)
  463. // .should.equal true
  464. // it "should call the callback with the summarized updates and the next before timestamp", ->
  465. // @callback.calledWith(null, @summarizedUpdates, @nextBeforeTimestamp).should.equal true
  466. // describe "when there are no more updates", ->
  467. // beforeEach ->
  468. // @updates = []
  469. // @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  470. // @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  471. // @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  472. // it "should call the callback with the summarized updates and null for nextBeforeTimestamp", ->
  473. // @callback.calledWith(null, @summarizedUpdates, null).should.equal true
  474. // describe "getSummarizedProjectUpdates", ->
  475. // describe "when one batch of updates is enough to meet the limit", ->
  476. // beforeEach ->
  477. // @before = Date.now()
  478. // @min_count = 2
  479. // @updates = ["summarized-updates-3", "summarized-updates-2"]
  480. // @nextBeforeTimestamp = @before - 100
  481. // @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, @nextBeforeTimestamp)
  482. // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  483. // it "should get the batch of summarized updates", ->
  484. // @UpdatesManager._extendBatchOfSummarizedUpdates
  485. // .calledWith(@project_id, [], @before, @min_count)
  486. // .should.equal true
  487. // it "should call the callback with the updates", ->
  488. // @callback.calledWith(null, @updates, @nextBeforeTimestamp).should.equal true
  489. // describe "when multiple batches are needed to meet the limit", ->
  490. // beforeEach ->
  491. // @before = Date.now()
  492. // @min_count = 4
  493. // @firstBatch = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  494. // @nextBeforeTimestamp = @before - 100
  495. // @secondBatch = [{ toV: 4, fromV: 4 }, { toV: 3, fromV: 3 }]
  496. // @nextNextBeforeTimestamp = @before - 200
  497. // @UpdatesManager._extendBatchOfSummarizedUpdates = (project_id, existingUpdates, before, desiredLength, callback) =>
  498. // if existingUpdates.length == 0
  499. // callback null, @firstBatch, @nextBeforeTimestamp
  500. // else
  501. // callback null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp
  502. // sinon.spy @UpdatesManager, "_extendBatchOfSummarizedUpdates"
  503. // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  504. // it "should get the first batch of summarized updates", ->
  505. // @UpdatesManager._extendBatchOfSummarizedUpdates
  506. // .calledWith(@project_id, [], @before, @min_count)
  507. // .should.equal true
  508. // it "should get the second batch of summarized updates", ->
  509. // @UpdatesManager._extendBatchOfSummarizedUpdates
  510. // .calledWith(@project_id, @firstBatch, @nextBeforeTimestamp, @min_count)
  511. // .should.equal true
  512. // it "should call the callback with all the updates", ->
  513. // @callback.calledWith(null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp).should.equal true
  514. // describe "when the end of the database is hit", ->
  515. // beforeEach ->
  516. // @before = Date.now()
  517. // @min_count = 4
  518. // @updates = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  519. // @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, null)
  520. // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  521. // it "should get the batch of summarized updates", ->
  522. // @UpdatesManager._extendBatchOfSummarizedUpdates
  523. // .calledWith(@project_id, [], @before, @min_count)
  524. // .should.equal true
  525. // it "should call the callback with the updates", ->
  526. // @callback.calledWith(null, @updates, null).should.equal true
  527. describe("fillUserInfo", function() {
  528. describe("with valid users", function() {
  529. beforeEach(function(done) {
  530. const {ObjectId} = require("mongojs");
  531. this.user_id_1 = ObjectId().toString();
  532. this.user_id_2 = ObjectId().toString();
  533. this.updates = [{
  534. meta: {
  535. user_id: this.user_id_1
  536. },
  537. op: "mock-op-1"
  538. }, {
  539. meta: {
  540. user_id: this.user_id_1
  541. },
  542. op: "mock-op-2"
  543. }, {
  544. meta: {
  545. user_id: this.user_id_2
  546. },
  547. op: "mock-op-3"
  548. }];
  549. this.user_info = {};
  550. this.user_info[this.user_id_1] = {email: "user1@sharelatex.com"};
  551. this.user_info[this.user_id_2] = {email: "user2@sharelatex.com"};
  552. this.WebApiManager.getUserInfo = (user_id, callback) => {
  553. if (callback == null) { callback = function(error, userInfo) {}; }
  554. return callback(null, this.user_info[user_id]);
  555. };
  556. sinon.spy(this.WebApiManager, "getUserInfo");
  557. return this.UpdatesManager.fillUserInfo(this.updates, (error, results) => {
  558. this.results = results;
  559. return done();
  560. });
  561. });
  562. it("should only call getUserInfo once for each user_id", function() {
  563. this.WebApiManager.getUserInfo.calledTwice.should.equal(true);
  564. this.WebApiManager.getUserInfo
  565. .calledWith(this.user_id_1)
  566. .should.equal(true);
  567. return this.WebApiManager.getUserInfo
  568. .calledWith(this.user_id_2)
  569. .should.equal(true);
  570. });
  571. return it("should return the updates with the user info filled", function() {
  572. return expect(this.results).to.deep.equal([{
  573. meta: {
  574. user: {
  575. email: "user1@sharelatex.com"
  576. }
  577. },
  578. op: "mock-op-1"
  579. }, {
  580. meta: {
  581. user: {
  582. email: "user1@sharelatex.com"
  583. }
  584. },
  585. op: "mock-op-2"
  586. }, {
  587. meta: {
  588. user: {
  589. email: "user2@sharelatex.com"
  590. }
  591. },
  592. op: "mock-op-3"
  593. }]);
  594. });
  595. });
  596. return describe("with invalid user ids", function() {
  597. beforeEach(function(done) {
  598. this.updates = [{
  599. meta: {
  600. user_id: null
  601. },
  602. op: "mock-op-1"
  603. }, {
  604. meta: {
  605. user_id: "anonymous-user"
  606. },
  607. op: "mock-op-2"
  608. }];
  609. this.WebApiManager.getUserInfo = (user_id, callback) => {
  610. if (callback == null) { callback = function(error, userInfo) {}; }
  611. return callback(null, this.user_info[user_id]);
  612. };
  613. sinon.spy(this.WebApiManager, "getUserInfo");
  614. return this.UpdatesManager.fillUserInfo(this.updates, (error, results) => {
  615. this.results = results;
  616. return done();
  617. });
  618. });
  619. it("should not call getUserInfo", function() {
  620. return this.WebApiManager.getUserInfo.called.should.equal(false);
  621. });
  622. return it("should return the updates without the user info filled", function() {
  623. return expect(this.results).to.deep.equal([{
  624. meta: {},
  625. op: "mock-op-1"
  626. }, {
  627. meta: {},
  628. op: "mock-op-2"
  629. }]);
  630. });
  631. });
  632. });
  633. return describe("_summarizeUpdates", function() {
  634. beforeEach(function() {
  635. this.now = Date.now();
  636. this.user_1 = { id: "mock-user-1" };
  637. return this.user_2 = { id: "mock-user-2" };});
  638. it("should concat updates that are close in time", function() {
  639. const result = this.UpdatesManager._summarizeUpdates([{
  640. doc_id: "doc-id-1",
  641. meta: {
  642. user_id: this.user_1.id,
  643. start_ts: this.now + 20,
  644. end_ts: this.now + 30
  645. },
  646. v: 5
  647. }, {
  648. doc_id: "doc-id-1",
  649. meta: {
  650. user_id: this.user_2.id,
  651. start_ts: this.now,
  652. end_ts: this.now + 10
  653. },
  654. v: 4
  655. }]);
  656. return expect(result).to.deep.equal([{
  657. docs: {
  658. "doc-id-1": {
  659. fromV: 4,
  660. toV: 5
  661. }
  662. },
  663. meta: {
  664. user_ids: [this.user_1.id, this.user_2.id],
  665. start_ts: this.now,
  666. end_ts: this.now + 30
  667. }
  668. }]);
  669. });
  670. it("should leave updates that are far apart in time", function() {
  671. const oneDay = 1000 * 60 * 60 * 24;
  672. const result = this.UpdatesManager._summarizeUpdates([{
  673. doc_id: "doc-id-1",
  674. meta: {
  675. user_id: this.user_2.id,
  676. start_ts: this.now + oneDay,
  677. end_ts: this.now + oneDay + 10
  678. },
  679. v: 5
  680. }, {
  681. doc_id: "doc-id-1",
  682. meta: {
  683. user_id: this.user_1.id,
  684. start_ts: this.now,
  685. end_ts: this.now + 10
  686. },
  687. v: 4
  688. }]);
  689. return expect(result).to.deep.equal([{
  690. docs: {
  691. "doc-id-1": {
  692. fromV: 5,
  693. toV: 5
  694. }
  695. },
  696. meta: {
  697. user_ids: [this.user_2.id],
  698. start_ts: this.now + oneDay,
  699. end_ts: this.now + oneDay + 10
  700. }
  701. }, {
  702. docs: {
  703. "doc-id-1": {
  704. fromV: 4,
  705. toV: 4
  706. }
  707. },
  708. meta: {
  709. user_ids: [this.user_1.id],
  710. start_ts: this.now,
  711. end_ts: this.now + 10
  712. }
  713. }]);
  714. });
  715. it("should concat onto existing summarized updates", function() {
  716. const result = this.UpdatesManager._summarizeUpdates([{
  717. doc_id: "doc-id-2",
  718. meta: {
  719. user_id: this.user_1.id,
  720. start_ts: this.now + 20,
  721. end_ts: this.now + 30
  722. },
  723. v: 5
  724. }, {
  725. doc_id: "doc-id-2",
  726. meta: {
  727. user_id: this.user_2.id,
  728. start_ts: this.now,
  729. end_ts: this.now + 10
  730. },
  731. v: 4
  732. }], [{
  733. docs: {
  734. "doc-id-1": {
  735. fromV: 6,
  736. toV: 8
  737. }
  738. },
  739. meta: {
  740. user_ids: [this.user_1.id],
  741. start_ts: this.now + 40,
  742. end_ts: this.now + 50
  743. }
  744. }]);
  745. return expect(result).to.deep.equal([{
  746. docs: {
  747. "doc-id-1": {
  748. toV: 8,
  749. fromV: 6
  750. },
  751. "doc-id-2": {
  752. toV: 5,
  753. fromV: 4
  754. }
  755. },
  756. meta: {
  757. user_ids: [this.user_1.id, this.user_2.id],
  758. start_ts: this.now,
  759. end_ts: this.now + 50
  760. }
  761. }]);
  762. });
  763. it("should include null user values", function() {
  764. const result = this.UpdatesManager._summarizeUpdates([{
  765. doc_id: "doc-id-1",
  766. meta: {
  767. user_id: this.user_1.id,
  768. start_ts: this.now + 20,
  769. end_ts: this.now + 30
  770. },
  771. v: 5
  772. }, {
  773. doc_id: "doc-id-1",
  774. meta: {
  775. user_id: null,
  776. start_ts: this.now,
  777. end_ts: this.now + 10
  778. },
  779. v: 4
  780. }]);
  781. return expect(result).to.deep.equal([{
  782. docs: {
  783. "doc-id-1": {
  784. fromV: 4,
  785. toV: 5
  786. }
  787. },
  788. meta: {
  789. user_ids: [this.user_1.id, null],
  790. start_ts: this.now,
  791. end_ts: this.now + 30
  792. }
  793. }]);
  794. });
  795. it("should include null user values, when the null is earlier in the updates list", function() {
  796. const result = this.UpdatesManager._summarizeUpdates([{
  797. doc_id: "doc-id-1",
  798. meta: {
  799. user_id: null,
  800. start_ts: this.now,
  801. end_ts: this.now + 10
  802. },
  803. v: 4
  804. }, {
  805. doc_id: "doc-id-1",
  806. meta: {
  807. user_id: this.user_1.id,
  808. start_ts: this.now + 20,
  809. end_ts: this.now + 30
  810. },
  811. v: 5
  812. }]);
  813. return expect(result).to.deep.equal([{
  814. docs: {
  815. "doc-id-1": {
  816. fromV: 4,
  817. toV: 5
  818. }
  819. },
  820. meta: {
  821. user_ids: [null, this.user_1.id],
  822. start_ts: this.now,
  823. end_ts: this.now + 30
  824. }
  825. }]);
  826. });
  827. it("should roll several null user values into one", function() {
  828. const result = this.UpdatesManager._summarizeUpdates([{
  829. doc_id: "doc-id-1",
  830. meta: {
  831. user_id: this.user_1.id,
  832. start_ts: this.now + 20,
  833. end_ts: this.now + 30
  834. },
  835. v: 5
  836. }, {
  837. doc_id: "doc-id-1",
  838. meta: {
  839. user_id: null,
  840. start_ts: this.now,
  841. end_ts: this.now + 10
  842. },
  843. v: 4
  844. }, {
  845. doc_id: "doc-id-1",
  846. meta: {
  847. user_id: null,
  848. start_ts: this.now + 2,
  849. end_ts: this.now + 4
  850. },
  851. v: 4
  852. }]);
  853. return expect(result).to.deep.equal([{
  854. docs: {
  855. "doc-id-1": {
  856. fromV: 4,
  857. toV: 5
  858. }
  859. },
  860. meta: {
  861. user_ids: [this.user_1.id, null],
  862. start_ts: this.now,
  863. end_ts: this.now + 30
  864. }
  865. }]);
  866. });
  867. return it("should split updates before a big delete", function() {
  868. const result = this.UpdatesManager._summarizeUpdates([{
  869. doc_id: "doc-id-1",
  870. op: [{ d: "this is a long long long long long delete", p: 34 }],
  871. meta: {
  872. user_id: this.user_1.id,
  873. start_ts: this.now + 20,
  874. end_ts: this.now + 30
  875. },
  876. v: 5
  877. }, {
  878. doc_id: "doc-id-1",
  879. meta: {
  880. user_id: this.user_2.id,
  881. start_ts: this.now,
  882. end_ts: this.now + 10
  883. },
  884. v: 4
  885. }]);
  886. return expect(result).to.deep.equal([{
  887. docs: {
  888. "doc-id-1": {
  889. fromV: 5,
  890. toV: 5
  891. }
  892. },
  893. meta: {
  894. user_ids: [this.user_1.id],
  895. start_ts: this.now + 20,
  896. end_ts: this.now + 30
  897. }
  898. }, {
  899. docs: {
  900. "doc-id-1": {
  901. fromV: 4,
  902. toV: 4
  903. }
  904. },
  905. meta: {
  906. user_ids: [this.user_2.id],
  907. start_ts: this.now,
  908. end_ts: this.now + 10
  909. }
  910. }]);
  911. });
  912. });
  913. });