UpdatesManagerTests.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const { expect } = require('chai')
  17. const { ObjectId } = require('mongodb')
  18. const modulePath = '../../../../app/js/UpdatesManager.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('UpdatesManager', function () {
  21. beforeEach(function () {
  22. this.UpdatesManager = SandboxedModule.require(modulePath, {
  23. singleOnly: true,
  24. 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. '@overleaf/settings': {
  34. redis: {
  35. lock: {
  36. key_schema: {
  37. historyLock({ doc_id }) {
  38. return `HistoryLock:${doc_id}`
  39. },
  40. },
  41. },
  42. },
  43. },
  44. },
  45. })
  46. this.doc_id = 'doc-id-123'
  47. this.project_id = 'project-id-123'
  48. this.callback = sinon.stub()
  49. return (this.temporary = 'temp-mock')
  50. })
  51. describe('compressAndSaveRawUpdates', function () {
  52. describe('when there are no raw ops', function () {
  53. beforeEach(function () {
  54. this.MongoManager.peekLastCompressedUpdate = sinon.stub()
  55. return this.UpdatesManager.compressAndSaveRawUpdates(
  56. this.project_id,
  57. this.doc_id,
  58. [],
  59. this.temporary,
  60. this.callback
  61. )
  62. })
  63. it('should not need to access the database', function () {
  64. return this.MongoManager.peekLastCompressedUpdate.called.should.equal(
  65. false
  66. )
  67. })
  68. return it('should call the callback', function () {
  69. return this.callback.called.should.equal(true)
  70. })
  71. })
  72. describe('when there is no compressed history to begin with', function () {
  73. beforeEach(function () {
  74. this.rawUpdates = [
  75. { v: 12, op: 'mock-op-12' },
  76. { v: 13, op: 'mock-op-13' },
  77. ]
  78. this.compressedUpdates = [{ v: 13, op: 'compressed-op-12' }]
  79. this.MongoManager.peekLastCompressedUpdate = sinon
  80. .stub()
  81. .callsArgWith(1, null, null)
  82. this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
  83. this.UpdateCompressor.compressRawUpdates = sinon
  84. .stub()
  85. .returns(this.compressedUpdates)
  86. return this.UpdatesManager.compressAndSaveRawUpdates(
  87. this.project_id,
  88. this.doc_id,
  89. this.rawUpdates,
  90. this.temporary,
  91. this.callback
  92. )
  93. })
  94. it('should look at the last compressed op', function () {
  95. return this.MongoManager.peekLastCompressedUpdate
  96. .calledWith(this.doc_id)
  97. .should.equal(true)
  98. })
  99. it('should save the compressed ops as a pack', function () {
  100. return this.PackManager.insertCompressedUpdates
  101. .calledWith(
  102. this.project_id,
  103. this.doc_id,
  104. null,
  105. this.compressedUpdates,
  106. this.temporary
  107. )
  108. .should.equal(true)
  109. })
  110. return it('should call the callback', function () {
  111. return this.callback.called.should.equal(true)
  112. })
  113. })
  114. describe('when the raw ops need appending to existing history', function () {
  115. beforeEach(function () {
  116. this.lastCompressedUpdate = { v: 11, op: 'compressed-op-11' }
  117. this.compressedUpdates = [
  118. { v: 12, op: 'compressed-op-11+12' },
  119. { v: 13, op: 'compressed-op-12' },
  120. ]
  121. this.MongoManager.peekLastCompressedUpdate = sinon
  122. .stub()
  123. .callsArgWith(
  124. 1,
  125. null,
  126. this.lastCompressedUpdate,
  127. this.lastCompressedUpdate.v
  128. )
  129. this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
  130. return (this.UpdateCompressor.compressRawUpdates = sinon
  131. .stub()
  132. .returns(this.compressedUpdates))
  133. })
  134. describe('when the raw ops start where the existing history ends', function () {
  135. beforeEach(function () {
  136. this.rawUpdates = [
  137. { v: 12, op: 'mock-op-12' },
  138. { v: 13, op: 'mock-op-13' },
  139. ]
  140. return this.UpdatesManager.compressAndSaveRawUpdates(
  141. this.project_id,
  142. this.doc_id,
  143. this.rawUpdates,
  144. this.temporary,
  145. this.callback
  146. )
  147. })
  148. it('should look at the last compressed op', function () {
  149. return this.MongoManager.peekLastCompressedUpdate
  150. .calledWith(this.doc_id)
  151. .should.equal(true)
  152. })
  153. it('should compress the raw ops', function () {
  154. return this.UpdateCompressor.compressRawUpdates
  155. .calledWith(null, this.rawUpdates)
  156. .should.equal(true)
  157. })
  158. it('should save the new compressed ops into a pack', function () {
  159. return this.PackManager.insertCompressedUpdates
  160. .calledWith(
  161. this.project_id,
  162. this.doc_id,
  163. this.lastCompressedUpdate,
  164. this.compressedUpdates,
  165. this.temporary
  166. )
  167. .should.equal(true)
  168. })
  169. return it('should call the callback', function () {
  170. return this.callback.called.should.equal(true)
  171. })
  172. })
  173. describe('when the raw ops start where the existing history ends and the history is in a pack', function () {
  174. beforeEach(function () {
  175. this.lastCompressedUpdate = {
  176. pack: [{ v: 11, op: 'compressed-op-11' }],
  177. v: 11,
  178. }
  179. this.rawUpdates = [
  180. { v: 12, op: 'mock-op-12' },
  181. { v: 13, op: 'mock-op-13' },
  182. ]
  183. this.MongoManager.peekLastCompressedUpdate = sinon
  184. .stub()
  185. .callsArgWith(
  186. 1,
  187. null,
  188. this.lastCompressedUpdate,
  189. this.lastCompressedUpdate.v
  190. )
  191. return this.UpdatesManager.compressAndSaveRawUpdates(
  192. this.project_id,
  193. this.doc_id,
  194. this.rawUpdates,
  195. this.temporary,
  196. this.callback
  197. )
  198. })
  199. it('should look at the last compressed op', function () {
  200. return this.MongoManager.peekLastCompressedUpdate
  201. .calledWith(this.doc_id)
  202. .should.equal(true)
  203. })
  204. it('should compress the raw ops', function () {
  205. return this.UpdateCompressor.compressRawUpdates
  206. .calledWith(null, this.rawUpdates)
  207. .should.equal(true)
  208. })
  209. it('should save the new compressed ops into a pack', function () {
  210. return this.PackManager.insertCompressedUpdates
  211. .calledWith(
  212. this.project_id,
  213. this.doc_id,
  214. this.lastCompressedUpdate,
  215. this.compressedUpdates,
  216. this.temporary
  217. )
  218. .should.equal(true)
  219. })
  220. return it('should call the callback', function () {
  221. return this.callback.called.should.equal(true)
  222. })
  223. })
  224. describe('when some raw ops are passed that have already been compressed', function () {
  225. beforeEach(function () {
  226. this.rawUpdates = [
  227. { v: 10, op: 'mock-op-10' },
  228. { v: 11, op: 'mock-op-11' },
  229. { v: 12, op: 'mock-op-12' },
  230. { v: 13, op: 'mock-op-13' },
  231. ]
  232. return this.UpdatesManager.compressAndSaveRawUpdates(
  233. this.project_id,
  234. this.doc_id,
  235. this.rawUpdates,
  236. this.temporary,
  237. this.callback
  238. )
  239. })
  240. return it('should only compress the more recent raw ops', function () {
  241. return this.UpdateCompressor.compressRawUpdates
  242. .calledWith(null, this.rawUpdates.slice(-2))
  243. .should.equal(true)
  244. })
  245. })
  246. describe('when the raw ops do not follow from the last compressed op version', function () {
  247. beforeEach(function () {
  248. this.rawUpdates = [{ v: 13, op: 'mock-op-13' }]
  249. return this.UpdatesManager.compressAndSaveRawUpdates(
  250. this.project_id,
  251. this.doc_id,
  252. this.rawUpdates,
  253. this.temporary,
  254. this.callback
  255. )
  256. })
  257. it('should call the callback with an error', function () {
  258. return this.callback
  259. .calledWith(
  260. sinon.match.has(
  261. 'message',
  262. 'Tried to apply raw op at version 13 to last compressed update with version 11 from unknown time'
  263. )
  264. )
  265. .should.equal(true)
  266. })
  267. return it('should not insert any update into mongo', function () {
  268. return this.PackManager.insertCompressedUpdates.called.should.equal(
  269. false
  270. )
  271. })
  272. })
  273. return describe('when the raw ops are out of order', function () {
  274. beforeEach(function () {
  275. this.rawUpdates = [
  276. { v: 13, op: 'mock-op-13' },
  277. { v: 12, op: 'mock-op-12' },
  278. ]
  279. return this.UpdatesManager.compressAndSaveRawUpdates(
  280. this.project_id,
  281. this.doc_id,
  282. this.rawUpdates,
  283. this.temporary,
  284. this.callback
  285. )
  286. })
  287. it('should call the callback with an error', function () {
  288. return this.callback
  289. .calledWith(sinon.match.has('message'))
  290. .should.equal(true)
  291. })
  292. return it('should not insert any update into mongo', function () {
  293. return this.PackManager.insertCompressedUpdates.called.should.equal(
  294. false
  295. )
  296. })
  297. })
  298. })
  299. return describe('when the raw ops need appending to existing history which is in S3', function () {
  300. beforeEach(function () {
  301. this.lastCompressedUpdate = null
  302. this.lastVersion = 11
  303. this.compressedUpdates = [{ v: 13, op: 'compressed-op-12' }]
  304. this.MongoManager.peekLastCompressedUpdate = sinon
  305. .stub()
  306. .callsArgWith(1, null, null, this.lastVersion)
  307. this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
  308. return (this.UpdateCompressor.compressRawUpdates = sinon
  309. .stub()
  310. .returns(this.compressedUpdates))
  311. })
  312. return describe('when the raw ops start where the existing history ends', function () {
  313. beforeEach(function () {
  314. this.rawUpdates = [
  315. { v: 12, op: 'mock-op-12' },
  316. { v: 13, op: 'mock-op-13' },
  317. ]
  318. return this.UpdatesManager.compressAndSaveRawUpdates(
  319. this.project_id,
  320. this.doc_id,
  321. this.rawUpdates,
  322. this.temporary,
  323. this.callback
  324. )
  325. })
  326. it('should try to look at the last compressed op', function () {
  327. return this.MongoManager.peekLastCompressedUpdate
  328. .calledWith(this.doc_id)
  329. .should.equal(true)
  330. })
  331. it('should compress the last compressed op and the raw ops', function () {
  332. return this.UpdateCompressor.compressRawUpdates
  333. .calledWith(this.lastCompressedUpdate, this.rawUpdates)
  334. .should.equal(true)
  335. })
  336. it('should save the compressed ops', function () {
  337. return this.PackManager.insertCompressedUpdates
  338. .calledWith(
  339. this.project_id,
  340. this.doc_id,
  341. null,
  342. this.compressedUpdates,
  343. this.temporary
  344. )
  345. .should.equal(true)
  346. })
  347. return it('should call the callback', function () {
  348. return this.callback.called.should.equal(true)
  349. })
  350. })
  351. })
  352. })
  353. describe('processUncompressedUpdates', function () {
  354. beforeEach(function () {
  355. this.UpdatesManager.compressAndSaveRawUpdates = sinon
  356. .stub()
  357. .callsArgWith(4)
  358. this.RedisManager.deleteAppliedDocUpdates = sinon.stub().callsArg(3)
  359. this.MongoManager.backportProjectId = sinon.stub().callsArg(2)
  360. return (this.UpdateTrimmer.shouldTrimUpdates = sinon
  361. .stub()
  362. .callsArgWith(1, null, (this.temporary = 'temp mock')))
  363. })
  364. describe('when there is fewer than one batch to send', function () {
  365. beforeEach(function () {
  366. this.updates = ['mock-update']
  367. this.RedisManager.getOldestDocUpdates = sinon
  368. .stub()
  369. .callsArgWith(2, null, this.updates)
  370. this.RedisManager.expandDocUpdates = sinon
  371. .stub()
  372. .callsArgWith(1, null, this.updates)
  373. return this.UpdatesManager.processUncompressedUpdates(
  374. this.project_id,
  375. this.doc_id,
  376. this.temporary,
  377. this.callback
  378. )
  379. })
  380. it('should get the oldest updates', function () {
  381. return this.RedisManager.getOldestDocUpdates
  382. .calledWith(this.doc_id, this.UpdatesManager.REDIS_READ_BATCH_SIZE)
  383. .should.equal(true)
  384. })
  385. it('should compress and save the updates', function () {
  386. return this.UpdatesManager.compressAndSaveRawUpdates
  387. .calledWith(
  388. this.project_id,
  389. this.doc_id,
  390. this.updates,
  391. this.temporary
  392. )
  393. .should.equal(true)
  394. })
  395. it('should delete the batch of uncompressed updates that was just processed', function () {
  396. return this.RedisManager.deleteAppliedDocUpdates
  397. .calledWith(this.project_id, this.doc_id, this.updates)
  398. .should.equal(true)
  399. })
  400. return it('should call the callback', function () {
  401. return this.callback.called.should.equal(true)
  402. })
  403. })
  404. return describe('when there are multiple batches to send', function () {
  405. beforeEach(function (done) {
  406. this.UpdatesManager.REDIS_READ_BATCH_SIZE = 2
  407. this.updates = [
  408. 'mock-update-0',
  409. 'mock-update-1',
  410. 'mock-update-2',
  411. 'mock-update-3',
  412. 'mock-update-4',
  413. ]
  414. this.redisArray = this.updates.slice()
  415. this.RedisManager.getOldestDocUpdates = (
  416. doc_id,
  417. batchSize,
  418. callback
  419. ) => {
  420. if (callback == null) {
  421. callback = function () {}
  422. }
  423. const updates = this.redisArray.slice(0, batchSize)
  424. this.redisArray = this.redisArray.slice(batchSize)
  425. return callback(null, updates)
  426. }
  427. sinon.spy(this.RedisManager, 'getOldestDocUpdates')
  428. this.RedisManager.expandDocUpdates = (jsonUpdates, callback) => {
  429. return callback(null, jsonUpdates)
  430. }
  431. sinon.spy(this.RedisManager, 'expandDocUpdates')
  432. return this.UpdatesManager.processUncompressedUpdates(
  433. this.project_id,
  434. this.doc_id,
  435. this.temporary,
  436. (...args) => {
  437. this.callback(...Array.from(args || []))
  438. return done()
  439. }
  440. )
  441. })
  442. it('should get the oldest updates in three batches ', function () {
  443. return this.RedisManager.getOldestDocUpdates.callCount.should.equal(3)
  444. })
  445. it('should compress and save the updates in batches', function () {
  446. this.UpdatesManager.compressAndSaveRawUpdates
  447. .calledWith(
  448. this.project_id,
  449. this.doc_id,
  450. this.updates.slice(0, 2),
  451. this.temporary
  452. )
  453. .should.equal(true)
  454. this.UpdatesManager.compressAndSaveRawUpdates
  455. .calledWith(
  456. this.project_id,
  457. this.doc_id,
  458. this.updates.slice(2, 4),
  459. this.temporary
  460. )
  461. .should.equal(true)
  462. return this.UpdatesManager.compressAndSaveRawUpdates
  463. .calledWith(
  464. this.project_id,
  465. this.doc_id,
  466. this.updates.slice(4, 5),
  467. this.temporary
  468. )
  469. .should.equal(true)
  470. })
  471. it('should delete the batches of uncompressed updates', function () {
  472. return this.RedisManager.deleteAppliedDocUpdates.callCount.should.equal(
  473. 3
  474. )
  475. })
  476. return it('should call the callback', function () {
  477. return this.callback.called.should.equal(true)
  478. })
  479. })
  480. })
  481. describe('processCompressedUpdatesWithLock', function () {
  482. beforeEach(function () {
  483. this.UpdateTrimmer.shouldTrimUpdates = sinon
  484. .stub()
  485. .callsArgWith(1, null, (this.temporary = 'temp mock'))
  486. this.MongoManager.backportProjectId = sinon.stub().callsArg(2)
  487. this.UpdatesManager._processUncompressedUpdates = sinon.stub().callsArg(3)
  488. this.LockManager.runWithLock = sinon.stub().callsArg(2)
  489. return this.UpdatesManager.processUncompressedUpdatesWithLock(
  490. this.project_id,
  491. this.doc_id,
  492. this.callback
  493. )
  494. })
  495. it('should check if the updates are temporary', function () {
  496. return this.UpdateTrimmer.shouldTrimUpdates
  497. .calledWith(this.project_id)
  498. .should.equal(true)
  499. })
  500. it('should backport the project id', function () {
  501. return this.MongoManager.backportProjectId
  502. .calledWith(this.project_id, this.doc_id)
  503. .should.equal(true)
  504. })
  505. it('should run processUncompressedUpdates with the lock', function () {
  506. return this.LockManager.runWithLock
  507. .calledWith(`HistoryLock:${this.doc_id}`)
  508. .should.equal(true)
  509. })
  510. return it('should call the callback', function () {
  511. return this.callback.called.should.equal(true)
  512. })
  513. })
  514. describe('getDocUpdates', function () {
  515. beforeEach(function () {
  516. this.updates = ['mock-updates']
  517. this.options = { to: 'mock-to', limit: 'mock-limit' }
  518. this.PackManager.getOpsByVersionRange = sinon
  519. .stub()
  520. .callsArgWith(4, null, this.updates)
  521. this.UpdatesManager.processUncompressedUpdatesWithLock = sinon
  522. .stub()
  523. .callsArg(2)
  524. return this.UpdatesManager.getDocUpdates(
  525. this.project_id,
  526. this.doc_id,
  527. this.options,
  528. this.callback
  529. )
  530. })
  531. it('should process outstanding updates', function () {
  532. return this.UpdatesManager.processUncompressedUpdatesWithLock
  533. .calledWith(this.project_id, this.doc_id)
  534. .should.equal(true)
  535. })
  536. it('should get the updates from the database', function () {
  537. return this.PackManager.getOpsByVersionRange
  538. .calledWith(
  539. this.project_id,
  540. this.doc_id,
  541. this.options.from,
  542. this.options.to
  543. )
  544. .should.equal(true)
  545. })
  546. return it('should return the updates', function () {
  547. return this.callback.calledWith(null, this.updates).should.equal(true)
  548. })
  549. })
  550. describe('getDocUpdatesWithUserInfo', function () {
  551. beforeEach(function () {
  552. this.updates = ['mock-updates']
  553. this.options = { to: 'mock-to', limit: 'mock-limit' }
  554. this.updatesWithUserInfo = ['updates-with-user-info']
  555. this.UpdatesManager.getDocUpdates = sinon
  556. .stub()
  557. .callsArgWith(3, null, this.updates)
  558. this.UpdatesManager.fillUserInfo = sinon
  559. .stub()
  560. .callsArgWith(1, null, this.updatesWithUserInfo)
  561. return this.UpdatesManager.getDocUpdatesWithUserInfo(
  562. this.project_id,
  563. this.doc_id,
  564. this.options,
  565. this.callback
  566. )
  567. })
  568. it('should get the updates', function () {
  569. return this.UpdatesManager.getDocUpdates
  570. .calledWith(this.project_id, this.doc_id, this.options)
  571. .should.equal(true)
  572. })
  573. it('should file the updates with the user info', function () {
  574. return this.UpdatesManager.fillUserInfo
  575. .calledWith(this.updates)
  576. .should.equal(true)
  577. })
  578. return it('should return the updates with the filled details', function () {
  579. return this.callback
  580. .calledWith(null, this.updatesWithUserInfo)
  581. .should.equal(true)
  582. })
  583. })
  584. describe('processUncompressedUpdatesForProject', function () {
  585. beforeEach(function (done) {
  586. this.doc_ids = ['mock-id-1', 'mock-id-2']
  587. this.UpdateTrimmer.shouldTrimUpdates = sinon
  588. .stub()
  589. .callsArgWith(1, null, (this.temporary = 'temp mock'))
  590. this.MongoManager.backportProjectId = sinon.stub().callsArg(2)
  591. this.UpdatesManager._processUncompressedUpdatesForDocWithLock = sinon
  592. .stub()
  593. .callsArg(3)
  594. this.RedisManager.getDocIdsWithHistoryOps = sinon
  595. .stub()
  596. .callsArgWith(1, null, this.doc_ids)
  597. return this.UpdatesManager.processUncompressedUpdatesForProject(
  598. this.project_id,
  599. () => {
  600. this.callback()
  601. return done()
  602. }
  603. )
  604. })
  605. it('should get all the docs with history ops', function () {
  606. return this.RedisManager.getDocIdsWithHistoryOps
  607. .calledWith(this.project_id)
  608. .should.equal(true)
  609. })
  610. it('should process the doc ops for the each doc_id', function () {
  611. return Array.from(this.doc_ids).map(doc_id =>
  612. this.UpdatesManager._processUncompressedUpdatesForDocWithLock
  613. .calledWith(this.project_id, doc_id, this.temporary)
  614. .should.equal(true)
  615. )
  616. })
  617. return it('should call the callback', function () {
  618. return this.callback.called.should.equal(true)
  619. })
  620. })
  621. describe('getSummarizedProjectUpdates', function () {
  622. beforeEach(function () {
  623. this.updates = [
  624. {
  625. doc_id: 123,
  626. v: 456,
  627. op: 'mock-updates',
  628. meta: { user_id: 123, start_ts: 1233, end_ts: 1234 },
  629. },
  630. ]
  631. this.options = { before: 'mock-before', limit: 'mock-limit' }
  632. this.summarizedUpdates = [
  633. {
  634. meta: { user_ids: [123], start_ts: 1233, end_ts: 1234 },
  635. docs: { 123: { fromV: 456, toV: 456 } },
  636. },
  637. ]
  638. this.updatesWithUserInfo = ['updates-with-user-info']
  639. this.done_state = false
  640. this.iterator = {
  641. next: cb => {
  642. this.done_state = true
  643. return cb(null, this.updates)
  644. },
  645. done: () => {
  646. return this.done_state
  647. },
  648. }
  649. this.PackManager.makeProjectIterator = sinon
  650. .stub()
  651. .callsArgWith(2, null, this.iterator)
  652. this.UpdatesManager.processUncompressedUpdatesForProject = sinon
  653. .stub()
  654. .callsArg(1)
  655. this.UpdatesManager.fillSummarizedUserInfo = sinon
  656. .stub()
  657. .callsArgWith(1, null, this.updatesWithUserInfo)
  658. return this.UpdatesManager.getSummarizedProjectUpdates(
  659. this.project_id,
  660. this.options,
  661. this.callback
  662. )
  663. })
  664. it('should process any outstanding updates', function () {
  665. return this.UpdatesManager.processUncompressedUpdatesForProject
  666. .calledWith(this.project_id)
  667. .should.equal(true)
  668. })
  669. it('should get the updates', function () {
  670. return this.PackManager.makeProjectIterator
  671. .calledWith(this.project_id, this.options.before)
  672. .should.equal(true)
  673. })
  674. it('should fill the updates with the user info', function () {
  675. return this.UpdatesManager.fillSummarizedUserInfo
  676. .calledWith(this.summarizedUpdates)
  677. .should.equal(true)
  678. })
  679. return it('should return the updates with the filled details', function () {
  680. return this.callback
  681. .calledWith(null, this.updatesWithUserInfo)
  682. .should.equal(true)
  683. })
  684. })
  685. // describe "_extendBatchOfSummarizedUpdates", ->
  686. // beforeEach ->
  687. // @before = Date.now()
  688. // @min_count = 2
  689. // @existingSummarizedUpdates = ["summarized-updates-3"]
  690. // @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  691. // describe "when there are updates to get", ->
  692. // beforeEach ->
  693. // @updates = [
  694. // {op: "mock-op-1", meta: end_ts: @before - 10},
  695. // {op: "mock-op-1", meta: end_ts: @nextBeforeTimestamp = @before - 20}
  696. // ]
  697. // @existingSummarizedUpdates = ["summarized-updates-3"]
  698. // @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
  699. // @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  700. // @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  701. // @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  702. // it "should get the updates", ->
  703. // @UpdatesManager.getProjectUpdatesWithUserInfo
  704. // .calledWith(@project_id, { before: @before, limit: 3 * @min_count })
  705. // .should.equal true
  706. // it "should summarize the updates", ->
  707. // @UpdatesManager._summarizeUpdates
  708. // .calledWith(@updates, @existingSummarizedUpdates)
  709. // .should.equal true
  710. // it "should call the callback with the summarized updates and the next before timestamp", ->
  711. // @callback.calledWith(null, @summarizedUpdates, @nextBeforeTimestamp).should.equal true
  712. // describe "when there are no more updates", ->
  713. // beforeEach ->
  714. // @updates = []
  715. // @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
  716. // @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
  717. // @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
  718. // it "should call the callback with the summarized updates and null for nextBeforeTimestamp", ->
  719. // @callback.calledWith(null, @summarizedUpdates, null).should.equal true
  720. // describe "getSummarizedProjectUpdates", ->
  721. // describe "when one batch of updates is enough to meet the limit", ->
  722. // beforeEach ->
  723. // @before = Date.now()
  724. // @min_count = 2
  725. // @updates = ["summarized-updates-3", "summarized-updates-2"]
  726. // @nextBeforeTimestamp = @before - 100
  727. // @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, @nextBeforeTimestamp)
  728. // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  729. // it "should get the batch of summarized updates", ->
  730. // @UpdatesManager._extendBatchOfSummarizedUpdates
  731. // .calledWith(@project_id, [], @before, @min_count)
  732. // .should.equal true
  733. // it "should call the callback with the updates", ->
  734. // @callback.calledWith(null, @updates, @nextBeforeTimestamp).should.equal true
  735. // describe "when multiple batches are needed to meet the limit", ->
  736. // beforeEach ->
  737. // @before = Date.now()
  738. // @min_count = 4
  739. // @firstBatch = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  740. // @nextBeforeTimestamp = @before - 100
  741. // @secondBatch = [{ toV: 4, fromV: 4 }, { toV: 3, fromV: 3 }]
  742. // @nextNextBeforeTimestamp = @before - 200
  743. // @UpdatesManager._extendBatchOfSummarizedUpdates = (project_id, existingUpdates, before, desiredLength, callback) =>
  744. // if existingUpdates.length == 0
  745. // callback null, @firstBatch, @nextBeforeTimestamp
  746. // else
  747. // callback null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp
  748. // sinon.spy @UpdatesManager, "_extendBatchOfSummarizedUpdates"
  749. // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  750. // it "should get the first batch of summarized updates", ->
  751. // @UpdatesManager._extendBatchOfSummarizedUpdates
  752. // .calledWith(@project_id, [], @before, @min_count)
  753. // .should.equal true
  754. // it "should get the second batch of summarized updates", ->
  755. // @UpdatesManager._extendBatchOfSummarizedUpdates
  756. // .calledWith(@project_id, @firstBatch, @nextBeforeTimestamp, @min_count)
  757. // .should.equal true
  758. // it "should call the callback with all the updates", ->
  759. // @callback.calledWith(null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp).should.equal true
  760. // describe "when the end of the database is hit", ->
  761. // beforeEach ->
  762. // @before = Date.now()
  763. // @min_count = 4
  764. // @updates = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
  765. // @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, null)
  766. // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
  767. // it "should get the batch of summarized updates", ->
  768. // @UpdatesManager._extendBatchOfSummarizedUpdates
  769. // .calledWith(@project_id, [], @before, @min_count)
  770. // .should.equal true
  771. // it "should call the callback with the updates", ->
  772. // @callback.calledWith(null, @updates, null).should.equal true
  773. describe('fillUserInfo', function () {
  774. describe('with valid users', function () {
  775. beforeEach(function (done) {
  776. this.user_id_1 = ObjectId().toString()
  777. this.user_id_2 = ObjectId().toString()
  778. this.updates = [
  779. {
  780. meta: {
  781. user_id: this.user_id_1,
  782. },
  783. op: 'mock-op-1',
  784. },
  785. {
  786. meta: {
  787. user_id: this.user_id_1,
  788. },
  789. op: 'mock-op-2',
  790. },
  791. {
  792. meta: {
  793. user_id: this.user_id_2,
  794. },
  795. op: 'mock-op-3',
  796. },
  797. ]
  798. this.user_info = {}
  799. this.user_info[this.user_id_1] = { email: 'user1@sharelatex.com' }
  800. this.user_info[this.user_id_2] = { email: 'user2@sharelatex.com' }
  801. this.WebApiManager.getUserInfo = (user_id, callback) => {
  802. if (callback == null) {
  803. callback = function () {}
  804. }
  805. return callback(null, this.user_info[user_id])
  806. }
  807. sinon.spy(this.WebApiManager, 'getUserInfo')
  808. return this.UpdatesManager.fillUserInfo(
  809. this.updates,
  810. (error, results) => {
  811. if (error) return done(error)
  812. this.results = results
  813. return done()
  814. }
  815. )
  816. })
  817. it('should only call getUserInfo once for each user_id', function () {
  818. this.WebApiManager.getUserInfo.calledTwice.should.equal(true)
  819. this.WebApiManager.getUserInfo
  820. .calledWith(this.user_id_1)
  821. .should.equal(true)
  822. return this.WebApiManager.getUserInfo
  823. .calledWith(this.user_id_2)
  824. .should.equal(true)
  825. })
  826. return it('should return the updates with the user info filled', function () {
  827. return expect(this.results).to.deep.equal([
  828. {
  829. meta: {
  830. user: {
  831. email: 'user1@sharelatex.com',
  832. },
  833. },
  834. op: 'mock-op-1',
  835. },
  836. {
  837. meta: {
  838. user: {
  839. email: 'user1@sharelatex.com',
  840. },
  841. },
  842. op: 'mock-op-2',
  843. },
  844. {
  845. meta: {
  846. user: {
  847. email: 'user2@sharelatex.com',
  848. },
  849. },
  850. op: 'mock-op-3',
  851. },
  852. ])
  853. })
  854. })
  855. return describe('with invalid user ids', function () {
  856. beforeEach(function (done) {
  857. this.updates = [
  858. {
  859. meta: {
  860. user_id: null,
  861. },
  862. op: 'mock-op-1',
  863. },
  864. {
  865. meta: {
  866. user_id: 'anonymous-user',
  867. },
  868. op: 'mock-op-2',
  869. },
  870. ]
  871. this.WebApiManager.getUserInfo = (user_id, callback) => {
  872. if (callback == null) {
  873. callback = function () {}
  874. }
  875. return callback(null, this.user_info[user_id])
  876. }
  877. sinon.spy(this.WebApiManager, 'getUserInfo')
  878. return this.UpdatesManager.fillUserInfo(
  879. this.updates,
  880. (error, results) => {
  881. if (error) return done(error)
  882. this.results = results
  883. return done()
  884. }
  885. )
  886. })
  887. it('should not call getUserInfo', function () {
  888. return this.WebApiManager.getUserInfo.called.should.equal(false)
  889. })
  890. return it('should return the updates without the user info filled', function () {
  891. return expect(this.results).to.deep.equal([
  892. {
  893. meta: {},
  894. op: 'mock-op-1',
  895. },
  896. {
  897. meta: {},
  898. op: 'mock-op-2',
  899. },
  900. ])
  901. })
  902. })
  903. })
  904. return describe('_summarizeUpdates', function () {
  905. beforeEach(function () {
  906. this.now = Date.now()
  907. this.user_1 = { id: 'mock-user-1' }
  908. return (this.user_2 = { id: 'mock-user-2' })
  909. })
  910. it('should concat updates that are close in time', function () {
  911. const result = this.UpdatesManager._summarizeUpdates([
  912. {
  913. doc_id: 'doc-id-1',
  914. meta: {
  915. user_id: this.user_1.id,
  916. start_ts: this.now + 20,
  917. end_ts: this.now + 30,
  918. },
  919. v: 5,
  920. },
  921. {
  922. doc_id: 'doc-id-1',
  923. meta: {
  924. user_id: this.user_2.id,
  925. start_ts: this.now,
  926. end_ts: this.now + 10,
  927. },
  928. v: 4,
  929. },
  930. ])
  931. return expect(result).to.deep.equal([
  932. {
  933. docs: {
  934. 'doc-id-1': {
  935. fromV: 4,
  936. toV: 5,
  937. },
  938. },
  939. meta: {
  940. user_ids: [this.user_1.id, this.user_2.id],
  941. start_ts: this.now,
  942. end_ts: this.now + 30,
  943. },
  944. },
  945. ])
  946. })
  947. it('should leave updates that are far apart in time', function () {
  948. const oneDay = 1000 * 60 * 60 * 24
  949. const result = this.UpdatesManager._summarizeUpdates([
  950. {
  951. doc_id: 'doc-id-1',
  952. meta: {
  953. user_id: this.user_2.id,
  954. start_ts: this.now + oneDay,
  955. end_ts: this.now + oneDay + 10,
  956. },
  957. v: 5,
  958. },
  959. {
  960. doc_id: 'doc-id-1',
  961. meta: {
  962. user_id: this.user_1.id,
  963. start_ts: this.now,
  964. end_ts: this.now + 10,
  965. },
  966. v: 4,
  967. },
  968. ])
  969. return expect(result).to.deep.equal([
  970. {
  971. docs: {
  972. 'doc-id-1': {
  973. fromV: 5,
  974. toV: 5,
  975. },
  976. },
  977. meta: {
  978. user_ids: [this.user_2.id],
  979. start_ts: this.now + oneDay,
  980. end_ts: this.now + oneDay + 10,
  981. },
  982. },
  983. {
  984. docs: {
  985. 'doc-id-1': {
  986. fromV: 4,
  987. toV: 4,
  988. },
  989. },
  990. meta: {
  991. user_ids: [this.user_1.id],
  992. start_ts: this.now,
  993. end_ts: this.now + 10,
  994. },
  995. },
  996. ])
  997. })
  998. it('should concat onto existing summarized updates', function () {
  999. const result = this.UpdatesManager._summarizeUpdates(
  1000. [
  1001. {
  1002. doc_id: 'doc-id-2',
  1003. meta: {
  1004. user_id: this.user_1.id,
  1005. start_ts: this.now + 20,
  1006. end_ts: this.now + 30,
  1007. },
  1008. v: 5,
  1009. },
  1010. {
  1011. doc_id: 'doc-id-2',
  1012. meta: {
  1013. user_id: this.user_2.id,
  1014. start_ts: this.now,
  1015. end_ts: this.now + 10,
  1016. },
  1017. v: 4,
  1018. },
  1019. ],
  1020. [
  1021. {
  1022. docs: {
  1023. 'doc-id-1': {
  1024. fromV: 6,
  1025. toV: 8,
  1026. },
  1027. },
  1028. meta: {
  1029. user_ids: [this.user_1.id],
  1030. start_ts: this.now + 40,
  1031. end_ts: this.now + 50,
  1032. },
  1033. },
  1034. ]
  1035. )
  1036. return expect(result).to.deep.equal([
  1037. {
  1038. docs: {
  1039. 'doc-id-1': {
  1040. toV: 8,
  1041. fromV: 6,
  1042. },
  1043. 'doc-id-2': {
  1044. toV: 5,
  1045. fromV: 4,
  1046. },
  1047. },
  1048. meta: {
  1049. user_ids: [this.user_1.id, this.user_2.id],
  1050. start_ts: this.now,
  1051. end_ts: this.now + 50,
  1052. },
  1053. },
  1054. ])
  1055. })
  1056. it('should include null user values', function () {
  1057. const result = this.UpdatesManager._summarizeUpdates([
  1058. {
  1059. doc_id: 'doc-id-1',
  1060. meta: {
  1061. user_id: this.user_1.id,
  1062. start_ts: this.now + 20,
  1063. end_ts: this.now + 30,
  1064. },
  1065. v: 5,
  1066. },
  1067. {
  1068. doc_id: 'doc-id-1',
  1069. meta: {
  1070. user_id: null,
  1071. start_ts: this.now,
  1072. end_ts: this.now + 10,
  1073. },
  1074. v: 4,
  1075. },
  1076. ])
  1077. return expect(result).to.deep.equal([
  1078. {
  1079. docs: {
  1080. 'doc-id-1': {
  1081. fromV: 4,
  1082. toV: 5,
  1083. },
  1084. },
  1085. meta: {
  1086. user_ids: [this.user_1.id, null],
  1087. start_ts: this.now,
  1088. end_ts: this.now + 30,
  1089. },
  1090. },
  1091. ])
  1092. })
  1093. it('should include null user values, when the null is earlier in the updates list', function () {
  1094. const result = this.UpdatesManager._summarizeUpdates([
  1095. {
  1096. doc_id: 'doc-id-1',
  1097. meta: {
  1098. user_id: null,
  1099. start_ts: this.now,
  1100. end_ts: this.now + 10,
  1101. },
  1102. v: 4,
  1103. },
  1104. {
  1105. doc_id: 'doc-id-1',
  1106. meta: {
  1107. user_id: this.user_1.id,
  1108. start_ts: this.now + 20,
  1109. end_ts: this.now + 30,
  1110. },
  1111. v: 5,
  1112. },
  1113. ])
  1114. return expect(result).to.deep.equal([
  1115. {
  1116. docs: {
  1117. 'doc-id-1': {
  1118. fromV: 4,
  1119. toV: 5,
  1120. },
  1121. },
  1122. meta: {
  1123. user_ids: [null, this.user_1.id],
  1124. start_ts: this.now,
  1125. end_ts: this.now + 30,
  1126. },
  1127. },
  1128. ])
  1129. })
  1130. it('should roll several null user values into one', function () {
  1131. const result = this.UpdatesManager._summarizeUpdates([
  1132. {
  1133. doc_id: 'doc-id-1',
  1134. meta: {
  1135. user_id: this.user_1.id,
  1136. start_ts: this.now + 20,
  1137. end_ts: this.now + 30,
  1138. },
  1139. v: 5,
  1140. },
  1141. {
  1142. doc_id: 'doc-id-1',
  1143. meta: {
  1144. user_id: null,
  1145. start_ts: this.now,
  1146. end_ts: this.now + 10,
  1147. },
  1148. v: 4,
  1149. },
  1150. {
  1151. doc_id: 'doc-id-1',
  1152. meta: {
  1153. user_id: null,
  1154. start_ts: this.now + 2,
  1155. end_ts: this.now + 4,
  1156. },
  1157. v: 4,
  1158. },
  1159. ])
  1160. return expect(result).to.deep.equal([
  1161. {
  1162. docs: {
  1163. 'doc-id-1': {
  1164. fromV: 4,
  1165. toV: 5,
  1166. },
  1167. },
  1168. meta: {
  1169. user_ids: [this.user_1.id, null],
  1170. start_ts: this.now,
  1171. end_ts: this.now + 30,
  1172. },
  1173. },
  1174. ])
  1175. })
  1176. return it('should split updates before a big delete', function () {
  1177. const result = this.UpdatesManager._summarizeUpdates([
  1178. {
  1179. doc_id: 'doc-id-1',
  1180. op: [{ d: 'this is a long long long long long delete', p: 34 }],
  1181. meta: {
  1182. user_id: this.user_1.id,
  1183. start_ts: this.now + 20,
  1184. end_ts: this.now + 30,
  1185. },
  1186. v: 5,
  1187. },
  1188. {
  1189. doc_id: 'doc-id-1',
  1190. meta: {
  1191. user_id: this.user_2.id,
  1192. start_ts: this.now,
  1193. end_ts: this.now + 10,
  1194. },
  1195. v: 4,
  1196. },
  1197. ])
  1198. return expect(result).to.deep.equal([
  1199. {
  1200. docs: {
  1201. 'doc-id-1': {
  1202. fromV: 5,
  1203. toV: 5,
  1204. },
  1205. },
  1206. meta: {
  1207. user_ids: [this.user_1.id],
  1208. start_ts: this.now + 20,
  1209. end_ts: this.now + 30,
  1210. },
  1211. },
  1212. {
  1213. docs: {
  1214. 'doc-id-1': {
  1215. fromV: 4,
  1216. toV: 4,
  1217. },
  1218. },
  1219. meta: {
  1220. user_ids: [this.user_2.id],
  1221. start_ts: this.now,
  1222. end_ts: this.now + 10,
  1223. },
  1224. },
  1225. ])
  1226. })
  1227. })
  1228. })