2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
2021-03-23 15:08:32 -04:00
|
|
|
const { expect } = require('chai')
|
2020-02-17 12:35:16 -05:00
|
|
|
const modulePath = '../../../../app/js/DiffManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('DiffManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./UpdatesManager': (this.UpdatesManager = {}),
|
|
|
|
'./DocumentUpdaterManager': (this.DocumentUpdaterManager = {}),
|
2021-07-13 07:04:43 -04:00
|
|
|
'./DiffGenerator': (this.DiffGenerator = {}),
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.from = new Date()
|
|
|
|
this.to = new Date(Date.now() + 10000)
|
|
|
|
this.project_id = 'mock-project-id'
|
|
|
|
return (this.doc_id = 'mock-doc-id')
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getLatestDocAndUpdates', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.content = 'hello world'
|
|
|
|
this.version = 42
|
|
|
|
this.updates = ['mock-update-1', 'mock-update-2']
|
|
|
|
|
|
|
|
this.DocumentUpdaterManager.getDocument = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.content, this.version)
|
|
|
|
return (this.UpdatesManager.getDocUpdatesWithUserInfo = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.updates))
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with a fromVersion', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager.getLatestDocAndUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.from,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the latest version of the doc', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DocumentUpdaterManager.getDocument
|
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the latest updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.UpdatesManager.getDocUpdatesWithUserInfo
|
|
|
|
.calledWith(this.project_id, this.doc_id, { from: this.from })
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the content, version and updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.content, this.version, this.updates)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('with no fromVersion', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager.getLatestDocAndUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
null,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the latest version of the doc', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DocumentUpdaterManager.getDocument
|
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should not get the latest updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.UpdatesManager.getDocUpdatesWithUserInfo.called.should.equal(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the content, version and blank updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.content, this.version, [])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getDiff', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.content = 'hello world'
|
|
|
|
// Op versions are the version they were applied to, so doc is always one version
|
|
|
|
// ahead.s
|
|
|
|
this.version = 43
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
op: 'mock-4',
|
|
|
|
v: 42,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() + 20) },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
op: 'mock-3',
|
|
|
|
v: 41,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() + 10) },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
op: 'mock-2',
|
|
|
|
v: 40,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() - 10) },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
op: 'mock-1',
|
|
|
|
v: 39,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() - 20) },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
this.fromVersion = 39
|
|
|
|
this.toVersion = 40
|
|
|
|
this.diffed_updates = this.updates.slice(2)
|
|
|
|
this.rewound_content = 'rewound-content'
|
|
|
|
return (this.diff = [{ u: 'mock-diff' }])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with matching versions', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager.getDocumentBeforeVersion = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.rewound_content, this.updates)
|
|
|
|
this.DiffGenerator.buildDiff = sinon.stub().returns(this.diff)
|
|
|
|
return this.DiffManager.getDiff(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.toVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the latest doc and version with all recent updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager.getDocumentBeforeVersion
|
|
|
|
.calledWith(this.project_id, this.doc_id, this.fromVersion)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should generate the diff', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffGenerator.buildDiff
|
|
|
|
.calledWith(
|
|
|
|
this.rewound_content,
|
|
|
|
this.diffed_updates.slice().reverse()
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the diff', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(null, this.diff).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the updates are inconsistent', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager.getLatestDocAndUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.content, this.version, this.updates)
|
|
|
|
this.DiffGenerator.buildDiff = sinon
|
|
|
|
.stub()
|
|
|
|
.throws((this.error = new Error('inconsistent!')))
|
2020-06-04 04:24:21 -04:00
|
|
|
this.DiffGenerator.rewindUpdates = sinon.stub()
|
2020-03-23 05:35:53 -04:00
|
|
|
this.DiffManager.getDiff(
|
2020-02-17 12:35:16 -05:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.toVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-03-23 05:35:53 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
|
|
|
const errorObj = this.callback.args[0][0]
|
|
|
|
expect(errorObj.message).to.include('inconsistent!')
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('getDocumentBeforeVersion', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager._tryGetDocumentBeforeVersion = sinon.stub()
|
|
|
|
this.document = 'mock-documents'
|
|
|
|
return (this.rewound_updates = 'mock-rewound-updates')
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('succesfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager._tryGetDocumentBeforeVersion.yields(
|
|
|
|
null,
|
|
|
|
this.document,
|
|
|
|
this.rewound_updates
|
|
|
|
)
|
|
|
|
return this.DiffManager.getDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call _tryGetDocumentBeforeVersion', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager._tryGetDocumentBeforeVersion
|
|
|
|
.calledWith(this.project_id, this.doc_id, this.version)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the response', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.document, this.rewound_updates)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with a retry needed', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
let retried = false
|
|
|
|
this.DiffManager._tryGetDocumentBeforeVersion = (
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
version,
|
|
|
|
callback
|
|
|
|
) => {
|
|
|
|
if (!retried) {
|
|
|
|
retried = true
|
|
|
|
const error = new Error()
|
|
|
|
error.retry = true
|
|
|
|
return callback(error)
|
|
|
|
} else {
|
|
|
|
return callback(null, this.document, this.rewound_updates)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sinon.spy(this.DiffManager, '_tryGetDocumentBeforeVersion')
|
|
|
|
return this.DiffManager.getDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call _tryGetDocumentBeforeVersion twice', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager._tryGetDocumentBeforeVersion.calledTwice.should.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the response', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.document, this.rewound_updates)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with a non-retriable error', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.error = new Error('oops')
|
|
|
|
this.DiffManager._tryGetDocumentBeforeVersion.yields(this.error)
|
|
|
|
return this.DiffManager.getDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call _tryGetDocumentBeforeVersion once', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager._tryGetDocumentBeforeVersion.calledOnce.should.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when retry limit is matched', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.error = new Error('oops')
|
|
|
|
this.error.retry = true
|
|
|
|
this.DiffManager._tryGetDocumentBeforeVersion.yields(this.error)
|
|
|
|
return this.DiffManager.getDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should call _tryGetDocumentBeforeVersion three times (max retries)', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager._tryGetDocumentBeforeVersion.calledThrice.should.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('_tryGetDocumentBeforeVersion', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.content = 'hello world'
|
|
|
|
// Op versions are the version they were applied to, so doc is always one version
|
|
|
|
// ahead.s
|
|
|
|
this.version = 43
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
op: 'mock-4',
|
|
|
|
v: 42,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() + 20) },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
op: 'mock-3',
|
|
|
|
v: 41,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() + 10) },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
op: 'mock-2',
|
|
|
|
v: 40,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() - 10) },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
op: 'mock-1',
|
|
|
|
v: 39,
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: new Date(this.to.getTime() - 20) },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
this.fromVersion = 39
|
|
|
|
this.rewound_content = 'rewound-content'
|
|
|
|
return (this.diff = [{ u: 'mock-diff' }])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with matching versions', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager.getLatestDocAndUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.content, this.version, this.updates)
|
|
|
|
this.DiffGenerator.rewindUpdates = sinon.spy((content, updates) => {
|
|
|
|
// the rewindUpdates method reverses the 'updates' array
|
|
|
|
updates.reverse()
|
|
|
|
return this.rewound_content
|
|
|
|
})
|
|
|
|
this.rewindUpdatesWithArgs = this.DiffGenerator.rewindUpdates.withArgs(
|
|
|
|
this.content,
|
|
|
|
this.updates.slice().reverse()
|
|
|
|
)
|
|
|
|
return this.DiffManager._tryGetDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should get the latest doc and version with all recent updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffManager.getLatestDocAndUpdates
|
|
|
|
.calledWith(this.project_id, this.doc_id, this.fromVersion)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should rewind the diff', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return sinon.assert.calledOnce(this.rewindUpdatesWithArgs)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with the rewound document and updates', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.rewound_content, this.updates)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with mismatching versions', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.version = 50
|
|
|
|
this.updates = [
|
|
|
|
{ op: 'mock-1', v: 40 },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ op: 'mock-1', v: 39 },
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
this.DiffManager.getLatestDocAndUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.content, this.version, this.updates)
|
|
|
|
return this.DiffManager._tryGetDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with an error with retry = true set', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.callback.calledOnce.should.equal(true)
|
|
|
|
const error = this.callback.args[0][0]
|
|
|
|
return expect(error.retry).to.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the updates are inconsistent', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffManager.getLatestDocAndUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.content, this.version, this.updates)
|
|
|
|
this.DiffGenerator.rewindUpdates = sinon
|
|
|
|
.stub()
|
|
|
|
.throws((this.error = new Error('inconsistent!')))
|
|
|
|
return this.DiffManager.getDocumentBeforeVersion(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback with an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|