| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- /* eslint-disable
- mocha/no-nested-tests,
- no-return-assign,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const modulePath = require('path').join(
- __dirname,
- '../../../../app/js/HistoryManager'
- )
- describe('HistoryManager', function () {
- beforeEach(function () {
- this.HistoryManager = SandboxedModule.require(modulePath, {
- requires: {
- request: (this.request = {}),
- '@overleaf/settings': (this.Settings = {
- apis: {
- project_history: {
- enabled: true,
- url: 'http://project_history.example.com',
- },
- trackchanges: {
- url: 'http://trackchanges.example.com',
- },
- },
- }),
- './DocumentManager': (this.DocumentManager = {}),
- './HistoryRedisManager': (this.HistoryRedisManager = {}),
- './RedisManager': (this.RedisManager = {}),
- './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
- './Metrics': (this.metrics = { inc: sinon.stub() }),
- },
- })
- this.project_id = 'mock-project-id'
- this.doc_id = 'mock-doc-id'
- return (this.callback = sinon.stub())
- })
- describe('flushDocChangesAsync', function () {
- beforeEach(function () {
- return (this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 }))
- })
- describe('when the project uses track changes', function () {
- beforeEach(function () {
- this.RedisManager.getHistoryType = sinon
- .stub()
- .yields(null, 'track-changes')
- return this.HistoryManager.flushDocChangesAsync(
- this.project_id,
- this.doc_id
- )
- })
- return it('should send a request to the track changes api', function () {
- return this.request.post
- .calledWith(
- `${this.Settings.apis.trackchanges.url}/project/${this.project_id}/doc/${this.doc_id}/flush`
- )
- .should.equal(true)
- })
- })
- describe('when the project uses project history and double flush is not disabled', function () {
- beforeEach(function () {
- this.RedisManager.getHistoryType = sinon
- .stub()
- .yields(null, 'project-history')
- return this.HistoryManager.flushDocChangesAsync(
- this.project_id,
- this.doc_id
- )
- })
- return it('should send a request to the track changes api', function () {
- return this.request.post.called.should.equal(true)
- })
- })
- return describe('when the project uses project history and double flush is disabled', function () {
- beforeEach(function () {
- this.Settings.disableDoubleFlush = true
- this.RedisManager.getHistoryType = sinon
- .stub()
- .yields(null, 'project-history')
- return this.HistoryManager.flushDocChangesAsync(
- this.project_id,
- this.doc_id
- )
- })
- return it('should not send a request to the track changes api', function () {
- return this.request.post.called.should.equal(false)
- })
- })
- })
- describe('flushProjectChangesAsync', function () {
- beforeEach(function () {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 })
- return this.HistoryManager.flushProjectChangesAsync(this.project_id)
- })
- return it('should send a request to the project history api', function () {
- return this.request.post
- .calledWith({
- url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`,
- qs: { background: true },
- })
- .should.equal(true)
- })
- })
- describe('flushProjectChanges', function () {
- describe('in the normal case', function () {
- beforeEach(function () {
- this.request.post = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 204 })
- return this.HistoryManager.flushProjectChanges(this.project_id, {
- background: true,
- })
- })
- return it('should send a request to the project history api', function () {
- return this.request.post
- .calledWith({
- url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`,
- qs: { background: true },
- })
- .should.equal(true)
- })
- })
- return describe('with the skip_history_flush option', function () {
- beforeEach(function () {
- this.request.post = sinon.stub()
- return this.HistoryManager.flushProjectChanges(this.project_id, {
- skip_history_flush: true,
- })
- })
- return it('should not send a request to the project history api', function () {
- return this.request.post.called.should.equal(false)
- })
- })
- })
- describe('recordAndFlushHistoryOps', function () {
- beforeEach(function () {
- this.ops = ['mock-ops']
- this.project_ops_length = 10
- this.doc_ops_length = 5
- this.HistoryManager.flushProjectChangesAsync = sinon.stub()
- this.HistoryRedisManager.recordDocHasHistoryOps = sinon.stub().callsArg(3)
- return (this.HistoryManager.flushDocChangesAsync = sinon.stub())
- })
- describe('with no ops', function () {
- beforeEach(function () {
- return this.HistoryManager.recordAndFlushHistoryOps(
- this.project_id,
- this.doc_id,
- [],
- this.doc_ops_length,
- this.project_ops_length,
- this.callback
- )
- })
- it('should not flush project changes', function () {
- return this.HistoryManager.flushProjectChangesAsync.called.should.equal(
- false
- )
- })
- it('should not record doc has history ops', function () {
- return this.HistoryRedisManager.recordDocHasHistoryOps.called.should.equal(
- false
- )
- })
- it('should not flush doc changes', function () {
- return this.HistoryManager.flushDocChangesAsync.called.should.equal(
- false
- )
- })
- return it('should call the callback', function () {
- return this.callback.called.should.equal(true)
- })
- })
- describe('with enough ops to flush project changes', function () {
- beforeEach(function () {
- this.HistoryManager.shouldFlushHistoryOps = sinon.stub()
- this.HistoryManager.shouldFlushHistoryOps
- .withArgs(this.project_ops_length)
- .returns(true)
- this.HistoryManager.shouldFlushHistoryOps
- .withArgs(this.doc_ops_length)
- .returns(false)
- return this.HistoryManager.recordAndFlushHistoryOps(
- this.project_id,
- this.doc_id,
- this.ops,
- this.doc_ops_length,
- this.project_ops_length,
- this.callback
- )
- })
- it('should flush project changes', function () {
- return this.HistoryManager.flushProjectChangesAsync
- .calledWith(this.project_id)
- .should.equal(true)
- })
- it('should record doc has history ops', function () {
- return this.HistoryRedisManager.recordDocHasHistoryOps.calledWith(
- this.project_id,
- this.doc_id,
- this.ops
- )
- })
- it('should not flush doc changes', function () {
- return this.HistoryManager.flushDocChangesAsync.called.should.equal(
- false
- )
- })
- return it('should call the callback', function () {
- return this.callback.called.should.equal(true)
- })
- })
- describe('with enough ops to flush doc changes', function () {
- beforeEach(function () {
- this.HistoryManager.shouldFlushHistoryOps = sinon.stub()
- this.HistoryManager.shouldFlushHistoryOps
- .withArgs(this.project_ops_length)
- .returns(false)
- this.HistoryManager.shouldFlushHistoryOps
- .withArgs(this.doc_ops_length)
- .returns(true)
- return this.HistoryManager.recordAndFlushHistoryOps(
- this.project_id,
- this.doc_id,
- this.ops,
- this.doc_ops_length,
- this.project_ops_length,
- this.callback
- )
- })
- it('should not flush project changes', function () {
- return this.HistoryManager.flushProjectChangesAsync.called.should.equal(
- false
- )
- })
- it('should record doc has history ops', function () {
- return this.HistoryRedisManager.recordDocHasHistoryOps.calledWith(
- this.project_id,
- this.doc_id,
- this.ops
- )
- })
- it('should flush doc changes', function () {
- return this.HistoryManager.flushDocChangesAsync
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- return it('should call the callback', function () {
- return this.callback.called.should.equal(true)
- })
- })
- describe('when recording doc has history ops errors', function () {
- beforeEach(function () {
- this.error = new Error('error')
- this.HistoryRedisManager.recordDocHasHistoryOps = sinon
- .stub()
- .callsArgWith(3, this.error)
- return this.HistoryManager.recordAndFlushHistoryOps(
- this.project_id,
- this.doc_id,
- this.ops,
- this.doc_ops_length,
- this.project_ops_length,
- this.callback
- )
- })
- it('should not flush doc changes', function () {
- return this.HistoryManager.flushDocChangesAsync.called.should.equal(
- false
- )
- })
- return it('should call the callback with the error', function () {
- return this.callback.calledWith(this.error).should.equal(true)
- })
- })
- return describe('shouldFlushHistoryOps', function () {
- it('should return false if the number of ops is not known', function () {
- return this.HistoryManager.shouldFlushHistoryOps(
- null,
- ['a', 'b', 'c'].length,
- 1
- ).should.equal(false)
- })
- it("should return false if the updates didn't take us past the threshold", function () {
- // Currently there are 14 ops
- // Previously we were on 11 ops
- // We didn't pass over a multiple of 5
- this.HistoryManager.shouldFlushHistoryOps(
- 14,
- ['a', 'b', 'c'].length,
- 5
- ).should.equal(false)
- it('should return true if the updates took to the threshold', function () {})
- // Currently there are 15 ops
- // Previously we were on 12 ops
- // We've reached a new multiple of 5
- return this.HistoryManager.shouldFlushHistoryOps(
- 15,
- ['a', 'b', 'c'].length,
- 5
- ).should.equal(true)
- })
- return it('should return true if the updates took past the threshold', function () {
- // Currently there are 19 ops
- // Previously we were on 16 ops
- // We didn't pass over a multiple of 5
- return this.HistoryManager.shouldFlushHistoryOps(
- 17,
- ['a', 'b', 'c'].length,
- 5
- ).should.equal(true)
- })
- })
- })
- return describe('resyncProjectHistory', function () {
- beforeEach(function () {
- this.projectHistoryId = 'history-id-1234'
- this.docs = [
- {
- doc: this.doc_id,
- path: 'main.tex',
- },
- ]
- this.files = [
- {
- file: 'mock-file-id',
- path: 'universe.png',
- url: `www.filestore.test/${this.project_id}/mock-file-id`,
- },
- ]
- this.ProjectHistoryRedisManager.queueResyncProjectStructure = sinon
- .stub()
- .yields()
- this.DocumentManager.resyncDocContentsWithLock = sinon.stub().yields()
- return this.HistoryManager.resyncProjectHistory(
- this.project_id,
- this.projectHistoryId,
- this.docs,
- this.files,
- this.callback
- )
- })
- it('should queue a project structure reync', function () {
- return this.ProjectHistoryRedisManager.queueResyncProjectStructure
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- this.docs,
- this.files
- )
- .should.equal(true)
- })
- it('should queue doc content reyncs', function () {
- return this.DocumentManager.resyncDocContentsWithLock
- .calledWith(this.project_id, this.doc_id)
- .should.equal(true)
- })
- return it('should call the callback', function () {
- return this.callback.called.should.equal(true)
- })
- })
- })
|