| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const chai = require('chai')
- const should = chai.should()
- const { expect } = chai
- const modulePath = '../../../../app/js/UpdatesManager.js'
- const SandboxedModule = require('sandboxed-module')
- describe('UpdatesManager', function() {
- beforeEach(function() {
- this.UpdatesManager = SandboxedModule.require(modulePath, {
- requires: {
- './UpdateCompressor': (this.UpdateCompressor = {}),
- './MongoManager': (this.MongoManager = {}),
- './PackManager': (this.PackManager = {}),
- './RedisManager': (this.RedisManager = {}),
- './LockManager': (this.LockManager = {}),
- './WebApiManager': (this.WebApiManager = {}),
- './UpdateTrimmer': (this.UpdateTrimmer = {}),
- './DocArchiveManager': (this.DocArchiveManager = {}),
- 'logger-sharelatex': { log: sinon.stub(), error: sinon.stub() },
- 'settings-sharelatex': {
- redis: {
- lock: {
- key_schema: {
- historyLock({ doc_id }) {
- return `HistoryLock:${doc_id}`
- }
- }
- }
- }
- }
- }
- })
- this.doc_id = 'doc-id-123'
- this.project_id = 'project-id-123'
- this.callback = sinon.stub()
- return (this.temporary = 'temp-mock')
- })
- describe('compressAndSaveRawUpdates', function() {
- describe('when there are no raw ops', function() {
- beforeEach(function() {
- this.MongoManager.peekLastCompressedUpdate = sinon.stub()
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- [],
- this.temporary,
- this.callback
- )
- })
- it('should not need to access the database', function() {
- return this.MongoManager.peekLastCompressedUpdate.called.should.equal(
- false
- )
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('when there is no compressed history to begin with', function() {
- beforeEach(function() {
- this.rawUpdates = [
- { v: 12, op: 'mock-op-12' },
- { v: 13, op: 'mock-op-13' }
- ]
- this.compressedUpdates = [{ v: 13, op: 'compressed-op-12' }]
- this.MongoManager.peekLastCompressedUpdate = sinon
- .stub()
- .callsArgWith(1, null, null)
- this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
- this.UpdateCompressor.compressRawUpdates = sinon
- .stub()
- .returns(this.compressedUpdates)
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- it('should look at the last compressed op', function() {
- return this.MongoManager.peekLastCompressedUpdate
- .calledWith(this.doc_id)
- .should.equal(true)
- })
- it('should save the compressed ops as a pack', function() {
- return this.PackManager.insertCompressedUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- null,
- this.compressedUpdates,
- this.temporary
- )
- .should.equal(true)
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('when the raw ops need appending to existing history', function() {
- beforeEach(function() {
- this.lastCompressedUpdate = { v: 11, op: 'compressed-op-11' }
- this.compressedUpdates = [
- { v: 12, op: 'compressed-op-11+12' },
- { v: 13, op: 'compressed-op-12' }
- ]
- this.MongoManager.peekLastCompressedUpdate = sinon
- .stub()
- .callsArgWith(
- 1,
- null,
- this.lastCompressedUpdate,
- this.lastCompressedUpdate.v
- )
- this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
- return (this.UpdateCompressor.compressRawUpdates = sinon
- .stub()
- .returns(this.compressedUpdates))
- })
- describe('when the raw ops start where the existing history ends', function() {
- beforeEach(function() {
- this.rawUpdates = [
- { v: 12, op: 'mock-op-12' },
- { v: 13, op: 'mock-op-13' }
- ]
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- it('should look at the last compressed op', function() {
- return this.MongoManager.peekLastCompressedUpdate
- .calledWith(this.doc_id)
- .should.equal(true)
- })
- it('should compress the raw ops', function() {
- return this.UpdateCompressor.compressRawUpdates
- .calledWith(null, this.rawUpdates)
- .should.equal(true)
- })
- it('should save the new compressed ops into a pack', function() {
- return this.PackManager.insertCompressedUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- this.lastCompressedUpdate,
- this.compressedUpdates,
- this.temporary
- )
- .should.equal(true)
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('when the raw ops start where the existing history ends and the history is in a pack', function() {
- beforeEach(function() {
- this.lastCompressedUpdate = {
- pack: [{ v: 11, op: 'compressed-op-11' }],
- v: 11
- }
- this.rawUpdates = [
- { v: 12, op: 'mock-op-12' },
- { v: 13, op: 'mock-op-13' }
- ]
- this.MongoManager.peekLastCompressedUpdate = sinon
- .stub()
- .callsArgWith(
- 1,
- null,
- this.lastCompressedUpdate,
- this.lastCompressedUpdate.v
- )
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- it('should look at the last compressed op', function() {
- return this.MongoManager.peekLastCompressedUpdate
- .calledWith(this.doc_id)
- .should.equal(true)
- })
- it('should compress the raw ops', function() {
- return this.UpdateCompressor.compressRawUpdates
- .calledWith(null, this.rawUpdates)
- .should.equal(true)
- })
- it('should save the new compressed ops into a pack', function() {
- return this.PackManager.insertCompressedUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- this.lastCompressedUpdate,
- this.compressedUpdates,
- this.temporary
- )
- .should.equal(true)
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('when some raw ops are passed that have already been compressed', function() {
- beforeEach(function() {
- 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' }
- ]
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- return it('should only compress the more recent raw ops', function() {
- return this.UpdateCompressor.compressRawUpdates
- .calledWith(null, this.rawUpdates.slice(-2))
- .should.equal(true)
- })
- })
- describe('when the raw ops do not follow from the last compressed op version', function() {
- beforeEach(function() {
- this.rawUpdates = [{ v: 13, op: 'mock-op-13' }]
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(
- sinon.match.has(
- 'message',
- 'Tried to apply raw op at version 13 to last compressed update with version 11 from unknown time'
- )
- )
- .should.equal(true)
- })
- return it('should not insert any update into mongo', function() {
- return this.PackManager.insertCompressedUpdates.called.should.equal(
- false
- )
- })
- })
- return describe('when the raw ops are out of order', function() {
- beforeEach(function() {
- this.rawUpdates = [
- { v: 13, op: 'mock-op-13' },
- { v: 12, op: 'mock-op-12' }
- ]
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- it('should call the callback with an error', function() {
- return this.callback
- .calledWith(sinon.match.has('message'))
- .should.equal(true)
- })
- return it('should not insert any update into mongo', function() {
- return this.PackManager.insertCompressedUpdates.called.should.equal(
- false
- )
- })
- })
- })
- return describe('when the raw ops need appending to existing history which is in S3', function() {
- beforeEach(function() {
- this.lastCompressedUpdate = null
- this.lastVersion = 11
- this.compressedUpdates = [{ v: 13, op: 'compressed-op-12' }]
- this.MongoManager.peekLastCompressedUpdate = sinon
- .stub()
- .callsArgWith(1, null, null, this.lastVersion)
- this.PackManager.insertCompressedUpdates = sinon.stub().callsArg(5)
- return (this.UpdateCompressor.compressRawUpdates = sinon
- .stub()
- .returns(this.compressedUpdates))
- })
- return describe('when the raw ops start where the existing history ends', function() {
- beforeEach(function() {
- this.rawUpdates = [
- { v: 12, op: 'mock-op-12' },
- { v: 13, op: 'mock-op-13' }
- ]
- return this.UpdatesManager.compressAndSaveRawUpdates(
- this.project_id,
- this.doc_id,
- this.rawUpdates,
- this.temporary,
- this.callback
- )
- })
- it('should try to look at the last compressed op', function() {
- return this.MongoManager.peekLastCompressedUpdate
- .calledWith(this.doc_id)
- .should.equal(true)
- })
- it('should compress the last compressed op and the raw ops', function() {
- return this.UpdateCompressor.compressRawUpdates
- .calledWith(this.lastCompressedUpdate, this.rawUpdates)
- .should.equal(true)
- })
- it('should save the compressed ops', function() {
- return this.PackManager.insertCompressedUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- null,
- this.compressedUpdates,
- this.temporary
- )
- .should.equal(true)
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- })
- })
- describe('processUncompressedUpdates', function() {
- beforeEach(function() {
- this.UpdatesManager.compressAndSaveRawUpdates = sinon
- .stub()
- .callsArgWith(4)
- this.RedisManager.deleteAppliedDocUpdates = sinon.stub().callsArg(3)
- this.MongoManager.backportProjectId = sinon.stub().callsArg(2)
- return (this.UpdateTrimmer.shouldTrimUpdates = sinon
- .stub()
- .callsArgWith(1, null, (this.temporary = 'temp mock')))
- })
- describe('when there is fewer than one batch to send', function() {
- beforeEach(function() {
- this.updates = ['mock-update']
- this.RedisManager.getOldestDocUpdates = sinon
- .stub()
- .callsArgWith(2, null, this.updates)
- this.RedisManager.expandDocUpdates = sinon
- .stub()
- .callsArgWith(1, null, this.updates)
- return this.UpdatesManager.processUncompressedUpdates(
- this.project_id,
- this.doc_id,
- this.temporary,
- this.callback
- )
- })
- it('should get the oldest updates', function() {
- return this.RedisManager.getOldestDocUpdates
- .calledWith(this.doc_id, this.UpdatesManager.REDIS_READ_BATCH_SIZE)
- .should.equal(true)
- })
- it('should compress and save the updates', function() {
- return this.UpdatesManager.compressAndSaveRawUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- this.updates,
- this.temporary
- )
- .should.equal(true)
- })
- it('should delete the batch of uncompressed updates that was just processed', function() {
- return this.RedisManager.deleteAppliedDocUpdates
- .calledWith(this.project_id, this.doc_id, this.updates)
- .should.equal(true)
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- return describe('when there are multiple batches to send', function() {
- beforeEach(function(done) {
- this.UpdatesManager.REDIS_READ_BATCH_SIZE = 2
- this.updates = [
- 'mock-update-0',
- 'mock-update-1',
- 'mock-update-2',
- 'mock-update-3',
- 'mock-update-4'
- ]
- this.redisArray = this.updates.slice()
- this.RedisManager.getOldestDocUpdates = (
- doc_id,
- batchSize,
- callback
- ) => {
- if (callback == null) {
- callback = function(error, updates) {}
- }
- const updates = this.redisArray.slice(0, batchSize)
- this.redisArray = this.redisArray.slice(batchSize)
- return callback(null, updates)
- }
- sinon.spy(this.RedisManager, 'getOldestDocUpdates')
- this.RedisManager.expandDocUpdates = (jsonUpdates, callback) => {
- return callback(null, jsonUpdates)
- }
- sinon.spy(this.RedisManager, 'expandDocUpdates')
- return this.UpdatesManager.processUncompressedUpdates(
- this.project_id,
- this.doc_id,
- this.temporary,
- (...args) => {
- this.callback(...Array.from(args || []))
- return done()
- }
- )
- })
- it('should get the oldest updates in three batches ', function() {
- return this.RedisManager.getOldestDocUpdates.callCount.should.equal(3)
- })
- it('should compress and save the updates in batches', function() {
- this.UpdatesManager.compressAndSaveRawUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- this.updates.slice(0, 2),
- this.temporary
- )
- .should.equal(true)
- this.UpdatesManager.compressAndSaveRawUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- this.updates.slice(2, 4),
- this.temporary
- )
- .should.equal(true)
- return this.UpdatesManager.compressAndSaveRawUpdates
- .calledWith(
- this.project_id,
- this.doc_id,
- this.updates.slice(4, 5),
- this.temporary
- )
- .should.equal(true)
- })
- it('should delete the batches of uncompressed updates', function() {
- return this.RedisManager.deleteAppliedDocUpdates.callCount.should.equal(
- 3
- )
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- })
- describe('processCompressedUpdatesWithLock', function() {
- beforeEach(function() {
- this.UpdateTrimmer.shouldTrimUpdates = sinon
- .stub()
- .callsArgWith(1, null, (this.temporary = 'temp mock'))
- this.MongoManager.backportProjectId = sinon.stub().callsArg(2)
- this.UpdatesManager._processUncompressedUpdates = sinon.stub().callsArg(3)
- this.LockManager.runWithLock = sinon.stub().callsArg(2)
- return this.UpdatesManager.processUncompressedUpdatesWithLock(
- this.project_id,
- this.doc_id,
- this.callback
- )
- })
- it('should check if the updates are temporary', function() {
- return this.UpdateTrimmer.shouldTrimUpdates
- .calledWith(this.project_id)
- .should.equal(true)
- })
- it('should backport the project id', function() {
- return this.MongoManager.backportProjectId
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should run processUncompressedUpdates with the lock', function() {
- return this.LockManager.runWithLock
- .calledWith(`HistoryLock:${this.doc_id}`)
- .should.equal(true)
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('getDocUpdates', function() {
- beforeEach(function() {
- this.updates = ['mock-updates']
- this.options = { to: 'mock-to', limit: 'mock-limit' }
- this.PackManager.getOpsByVersionRange = sinon
- .stub()
- .callsArgWith(4, null, this.updates)
- this.UpdatesManager.processUncompressedUpdatesWithLock = sinon
- .stub()
- .callsArg(2)
- return this.UpdatesManager.getDocUpdates(
- this.project_id,
- this.doc_id,
- this.options,
- this.callback
- )
- })
- it('should process outstanding updates', function() {
- return this.UpdatesManager.processUncompressedUpdatesWithLock
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- it('should get the updates from the database', function() {
- return this.PackManager.getOpsByVersionRange
- .calledWith(
- this.project_id,
- this.doc_id,
- this.options.from,
- this.options.to
- )
- .should.equal(true)
- })
- return it('should return the updates', function() {
- return this.callback.calledWith(null, this.updates).should.equal(true)
- })
- })
- describe('getDocUpdatesWithUserInfo', function() {
- beforeEach(function() {
- this.updates = ['mock-updates']
- this.options = { to: 'mock-to', limit: 'mock-limit' }
- this.updatesWithUserInfo = ['updates-with-user-info']
- this.UpdatesManager.getDocUpdates = sinon
- .stub()
- .callsArgWith(3, null, this.updates)
- this.UpdatesManager.fillUserInfo = sinon
- .stub()
- .callsArgWith(1, null, this.updatesWithUserInfo)
- return this.UpdatesManager.getDocUpdatesWithUserInfo(
- this.project_id,
- this.doc_id,
- this.options,
- this.callback
- )
- })
- it('should get the updates', function() {
- return this.UpdatesManager.getDocUpdates
- .calledWith(this.project_id, this.doc_id, this.options)
- .should.equal(true)
- })
- it('should file the updates with the user info', function() {
- return this.UpdatesManager.fillUserInfo
- .calledWith(this.updates)
- .should.equal(true)
- })
- return it('should return the updates with the filled details', function() {
- return this.callback
- .calledWith(null, this.updatesWithUserInfo)
- .should.equal(true)
- })
- })
- describe('processUncompressedUpdatesForProject', function() {
- beforeEach(function(done) {
- this.doc_ids = ['mock-id-1', 'mock-id-2']
- this.UpdateTrimmer.shouldTrimUpdates = sinon
- .stub()
- .callsArgWith(1, null, (this.temporary = 'temp mock'))
- this.MongoManager.backportProjectId = sinon.stub().callsArg(2)
- this.UpdatesManager._processUncompressedUpdatesForDocWithLock = sinon
- .stub()
- .callsArg(3)
- this.RedisManager.getDocIdsWithHistoryOps = sinon
- .stub()
- .callsArgWith(1, null, this.doc_ids)
- return this.UpdatesManager.processUncompressedUpdatesForProject(
- this.project_id,
- () => {
- this.callback()
- return done()
- }
- )
- })
- it('should get all the docs with history ops', function() {
- return this.RedisManager.getDocIdsWithHistoryOps
- .calledWith(this.project_id)
- .should.equal(true)
- })
- it('should process the doc ops for the each doc_id', function() {
- return Array.from(this.doc_ids).map(doc_id =>
- this.UpdatesManager._processUncompressedUpdatesForDocWithLock
- .calledWith(this.project_id, doc_id, this.temporary)
- .should.equal(true)
- )
- })
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true)
- })
- })
- describe('getSummarizedProjectUpdates', function() {
- beforeEach(function() {
- this.updates = [
- {
- doc_id: 123,
- v: 456,
- op: 'mock-updates',
- meta: { user_id: 123, start_ts: 1233, end_ts: 1234 }
- }
- ]
- this.options = { before: 'mock-before', limit: 'mock-limit' }
- this.summarizedUpdates = [
- {
- meta: { user_ids: [123], start_ts: 1233, end_ts: 1234 },
- docs: { '123': { fromV: 456, toV: 456 } }
- }
- ]
- this.updatesWithUserInfo = ['updates-with-user-info']
- this.done_state = false
- this.iterator = {
- next: cb => {
- this.done_state = true
- return cb(null, this.updates)
- },
- done: () => {
- return this.done_state
- }
- }
- this.PackManager.makeProjectIterator = sinon
- .stub()
- .callsArgWith(2, null, this.iterator)
- this.UpdatesManager.processUncompressedUpdatesForProject = sinon
- .stub()
- .callsArg(1)
- this.UpdatesManager.fillSummarizedUserInfo = sinon
- .stub()
- .callsArgWith(1, null, this.updatesWithUserInfo)
- return this.UpdatesManager.getSummarizedProjectUpdates(
- this.project_id,
- this.options,
- this.callback
- )
- })
- it('should process any outstanding updates', function() {
- return this.UpdatesManager.processUncompressedUpdatesForProject
- .calledWith(this.project_id)
- .should.equal(true)
- })
- it('should get the updates', function() {
- return this.PackManager.makeProjectIterator
- .calledWith(this.project_id, this.options.before)
- .should.equal(true)
- })
- it('should fill the updates with the user info', function() {
- return this.UpdatesManager.fillSummarizedUserInfo
- .calledWith(this.summarizedUpdates)
- .should.equal(true)
- })
- return it('should return the updates with the filled details', function() {
- return this.callback
- .calledWith(null, this.updatesWithUserInfo)
- .should.equal(true)
- })
- })
- // describe "_extendBatchOfSummarizedUpdates", ->
- // beforeEach ->
- // @before = Date.now()
- // @min_count = 2
- // @existingSummarizedUpdates = ["summarized-updates-3"]
- // @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
- // describe "when there are updates to get", ->
- // beforeEach ->
- // @updates = [
- // {op: "mock-op-1", meta: end_ts: @before - 10},
- // {op: "mock-op-1", meta: end_ts: @nextBeforeTimestamp = @before - 20}
- // ]
- // @existingSummarizedUpdates = ["summarized-updates-3"]
- // @summarizedUpdates = ["summarized-updates-3", "summarized-update-2", "summarized-update-1"]
- // @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
- // @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
- // @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
- // it "should get the updates", ->
- // @UpdatesManager.getProjectUpdatesWithUserInfo
- // .calledWith(@project_id, { before: @before, limit: 3 * @min_count })
- // .should.equal true
- // it "should summarize the updates", ->
- // @UpdatesManager._summarizeUpdates
- // .calledWith(@updates, @existingSummarizedUpdates)
- // .should.equal true
- // it "should call the callback with the summarized updates and the next before timestamp", ->
- // @callback.calledWith(null, @summarizedUpdates, @nextBeforeTimestamp).should.equal true
- // describe "when there are no more updates", ->
- // beforeEach ->
- // @updates = []
- // @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates)
- // @UpdatesManager.getProjectUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates)
- // @UpdatesManager._extendBatchOfSummarizedUpdates @project_id, @existingSummarizedUpdates, @before, @min_count, @callback
- // it "should call the callback with the summarized updates and null for nextBeforeTimestamp", ->
- // @callback.calledWith(null, @summarizedUpdates, null).should.equal true
- // describe "getSummarizedProjectUpdates", ->
- // describe "when one batch of updates is enough to meet the limit", ->
- // beforeEach ->
- // @before = Date.now()
- // @min_count = 2
- // @updates = ["summarized-updates-3", "summarized-updates-2"]
- // @nextBeforeTimestamp = @before - 100
- // @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, @nextBeforeTimestamp)
- // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
- // it "should get the batch of summarized updates", ->
- // @UpdatesManager._extendBatchOfSummarizedUpdates
- // .calledWith(@project_id, [], @before, @min_count)
- // .should.equal true
- // it "should call the callback with the updates", ->
- // @callback.calledWith(null, @updates, @nextBeforeTimestamp).should.equal true
- // describe "when multiple batches are needed to meet the limit", ->
- // beforeEach ->
- // @before = Date.now()
- // @min_count = 4
- // @firstBatch = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
- // @nextBeforeTimestamp = @before - 100
- // @secondBatch = [{ toV: 4, fromV: 4 }, { toV: 3, fromV: 3 }]
- // @nextNextBeforeTimestamp = @before - 200
- // @UpdatesManager._extendBatchOfSummarizedUpdates = (project_id, existingUpdates, before, desiredLength, callback) =>
- // if existingUpdates.length == 0
- // callback null, @firstBatch, @nextBeforeTimestamp
- // else
- // callback null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp
- // sinon.spy @UpdatesManager, "_extendBatchOfSummarizedUpdates"
- // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
- // it "should get the first batch of summarized updates", ->
- // @UpdatesManager._extendBatchOfSummarizedUpdates
- // .calledWith(@project_id, [], @before, @min_count)
- // .should.equal true
- // it "should get the second batch of summarized updates", ->
- // @UpdatesManager._extendBatchOfSummarizedUpdates
- // .calledWith(@project_id, @firstBatch, @nextBeforeTimestamp, @min_count)
- // .should.equal true
- // it "should call the callback with all the updates", ->
- // @callback.calledWith(null, @firstBatch.concat(@secondBatch), @nextNextBeforeTimestamp).should.equal true
- // describe "when the end of the database is hit", ->
- // beforeEach ->
- // @before = Date.now()
- // @min_count = 4
- // @updates = [{ toV: 6, fromV: 6 }, { toV: 5, fromV: 5 }]
- // @UpdatesManager._extendBatchOfSummarizedUpdates = sinon.stub().callsArgWith(4, null, @updates, null)
- // @UpdatesManager.getSummarizedProjectUpdates @project_id, { before: @before, min_count: @min_count }, @callback
- // it "should get the batch of summarized updates", ->
- // @UpdatesManager._extendBatchOfSummarizedUpdates
- // .calledWith(@project_id, [], @before, @min_count)
- // .should.equal true
- // it "should call the callback with the updates", ->
- // @callback.calledWith(null, @updates, null).should.equal true
- describe('fillUserInfo', function() {
- describe('with valid users', function() {
- beforeEach(function(done) {
- const { ObjectId } = require('mongojs')
- this.user_id_1 = ObjectId().toString()
- this.user_id_2 = ObjectId().toString()
- this.updates = [
- {
- meta: {
- user_id: this.user_id_1
- },
- op: 'mock-op-1'
- },
- {
- meta: {
- user_id: this.user_id_1
- },
- op: 'mock-op-2'
- },
- {
- meta: {
- user_id: this.user_id_2
- },
- op: 'mock-op-3'
- }
- ]
- this.user_info = {}
- this.user_info[this.user_id_1] = { email: 'user1@sharelatex.com' }
- this.user_info[this.user_id_2] = { email: 'user2@sharelatex.com' }
- this.WebApiManager.getUserInfo = (user_id, callback) => {
- if (callback == null) {
- callback = function(error, userInfo) {}
- }
- return callback(null, this.user_info[user_id])
- }
- sinon.spy(this.WebApiManager, 'getUserInfo')
- return this.UpdatesManager.fillUserInfo(
- this.updates,
- (error, results) => {
- this.results = results
- return done()
- }
- )
- })
- it('should only call getUserInfo once for each user_id', function() {
- this.WebApiManager.getUserInfo.calledTwice.should.equal(true)
- this.WebApiManager.getUserInfo
- .calledWith(this.user_id_1)
- .should.equal(true)
- return this.WebApiManager.getUserInfo
- .calledWith(this.user_id_2)
- .should.equal(true)
- })
- return it('should return the updates with the user info filled', function() {
- return expect(this.results).to.deep.equal([
- {
- meta: {
- user: {
- email: 'user1@sharelatex.com'
- }
- },
- op: 'mock-op-1'
- },
- {
- meta: {
- user: {
- email: 'user1@sharelatex.com'
- }
- },
- op: 'mock-op-2'
- },
- {
- meta: {
- user: {
- email: 'user2@sharelatex.com'
- }
- },
- op: 'mock-op-3'
- }
- ])
- })
- })
- return describe('with invalid user ids', function() {
- beforeEach(function(done) {
- this.updates = [
- {
- meta: {
- user_id: null
- },
- op: 'mock-op-1'
- },
- {
- meta: {
- user_id: 'anonymous-user'
- },
- op: 'mock-op-2'
- }
- ]
- this.WebApiManager.getUserInfo = (user_id, callback) => {
- if (callback == null) {
- callback = function(error, userInfo) {}
- }
- return callback(null, this.user_info[user_id])
- }
- sinon.spy(this.WebApiManager, 'getUserInfo')
- return this.UpdatesManager.fillUserInfo(
- this.updates,
- (error, results) => {
- this.results = results
- return done()
- }
- )
- })
- it('should not call getUserInfo', function() {
- return this.WebApiManager.getUserInfo.called.should.equal(false)
- })
- return it('should return the updates without the user info filled', function() {
- return expect(this.results).to.deep.equal([
- {
- meta: {},
- op: 'mock-op-1'
- },
- {
- meta: {},
- op: 'mock-op-2'
- }
- ])
- })
- })
- })
- return describe('_summarizeUpdates', function() {
- beforeEach(function() {
- this.now = Date.now()
- this.user_1 = { id: 'mock-user-1' }
- return (this.user_2 = { id: 'mock-user-2' })
- })
- it('should concat updates that are close in time', function() {
- const result = this.UpdatesManager._summarizeUpdates([
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now + 20,
- end_ts: this.now + 30
- },
- v: 5
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_2.id,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- }
- ])
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- fromV: 4,
- toV: 5
- }
- },
- meta: {
- user_ids: [this.user_1.id, this.user_2.id],
- start_ts: this.now,
- end_ts: this.now + 30
- }
- }
- ])
- })
- it('should leave updates that are far apart in time', function() {
- const oneDay = 1000 * 60 * 60 * 24
- const result = this.UpdatesManager._summarizeUpdates([
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_2.id,
- start_ts: this.now + oneDay,
- end_ts: this.now + oneDay + 10
- },
- v: 5
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- }
- ])
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- fromV: 5,
- toV: 5
- }
- },
- meta: {
- user_ids: [this.user_2.id],
- start_ts: this.now + oneDay,
- end_ts: this.now + oneDay + 10
- }
- },
- {
- docs: {
- 'doc-id-1': {
- fromV: 4,
- toV: 4
- }
- },
- meta: {
- user_ids: [this.user_1.id],
- start_ts: this.now,
- end_ts: this.now + 10
- }
- }
- ])
- })
- it('should concat onto existing summarized updates', function() {
- const result = this.UpdatesManager._summarizeUpdates(
- [
- {
- doc_id: 'doc-id-2',
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now + 20,
- end_ts: this.now + 30
- },
- v: 5
- },
- {
- doc_id: 'doc-id-2',
- meta: {
- user_id: this.user_2.id,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- }
- ],
- [
- {
- docs: {
- 'doc-id-1': {
- fromV: 6,
- toV: 8
- }
- },
- meta: {
- user_ids: [this.user_1.id],
- start_ts: this.now + 40,
- end_ts: this.now + 50
- }
- }
- ]
- )
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- toV: 8,
- fromV: 6
- },
- 'doc-id-2': {
- toV: 5,
- fromV: 4
- }
- },
- meta: {
- user_ids: [this.user_1.id, this.user_2.id],
- start_ts: this.now,
- end_ts: this.now + 50
- }
- }
- ])
- })
- it('should include null user values', function() {
- const result = this.UpdatesManager._summarizeUpdates([
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now + 20,
- end_ts: this.now + 30
- },
- v: 5
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: null,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- }
- ])
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- fromV: 4,
- toV: 5
- }
- },
- meta: {
- user_ids: [this.user_1.id, null],
- start_ts: this.now,
- end_ts: this.now + 30
- }
- }
- ])
- })
- it('should include null user values, when the null is earlier in the updates list', function() {
- const result = this.UpdatesManager._summarizeUpdates([
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: null,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now + 20,
- end_ts: this.now + 30
- },
- v: 5
- }
- ])
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- fromV: 4,
- toV: 5
- }
- },
- meta: {
- user_ids: [null, this.user_1.id],
- start_ts: this.now,
- end_ts: this.now + 30
- }
- }
- ])
- })
- it('should roll several null user values into one', function() {
- const result = this.UpdatesManager._summarizeUpdates([
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now + 20,
- end_ts: this.now + 30
- },
- v: 5
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: null,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: null,
- start_ts: this.now + 2,
- end_ts: this.now + 4
- },
- v: 4
- }
- ])
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- fromV: 4,
- toV: 5
- }
- },
- meta: {
- user_ids: [this.user_1.id, null],
- start_ts: this.now,
- end_ts: this.now + 30
- }
- }
- ])
- })
- return it('should split updates before a big delete', function() {
- const result = this.UpdatesManager._summarizeUpdates([
- {
- doc_id: 'doc-id-1',
- op: [{ d: 'this is a long long long long long delete', p: 34 }],
- meta: {
- user_id: this.user_1.id,
- start_ts: this.now + 20,
- end_ts: this.now + 30
- },
- v: 5
- },
- {
- doc_id: 'doc-id-1',
- meta: {
- user_id: this.user_2.id,
- start_ts: this.now,
- end_ts: this.now + 10
- },
- v: 4
- }
- ])
- return expect(result).to.deep.equal([
- {
- docs: {
- 'doc-id-1': {
- fromV: 5,
- toV: 5
- }
- },
- meta: {
- user_ids: [this.user_1.id],
- start_ts: this.now + 20,
- end_ts: this.now + 30
- }
- },
- {
- docs: {
- 'doc-id-1': {
- fromV: 4,
- toV: 4
- }
- },
- meta: {
- user_ids: [this.user_2.id],
- start_ts: this.now,
- end_ts: this.now + 10
- }
- }
- ])
- })
- })
- })
|