2020-05-06 06:11:22 -04:00
|
|
|
/* eslint-disable
|
|
|
|
mocha/no-nested-tests,
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
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 = {}),
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': (this.Settings = {
|
2020-05-06 06:11:36 -04:00
|
|
|
apis: {
|
|
|
|
project_history: {
|
2021-07-13 07:04:42 -04:00
|
|
|
url: 'http://project_history.example.com',
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
}),
|
|
|
|
'./DocumentManager': (this.DocumentManager = {}),
|
|
|
|
'./RedisManager': (this.RedisManager = {}),
|
|
|
|
'./ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
|
2021-07-13 07:04:42 -04:00
|
|
|
'./Metrics': (this.metrics = { inc: sinon.stub() }),
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
this.project_id = 'mock-project-id'
|
2023-06-06 06:19:55 -04:00
|
|
|
this.callback = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('flushProjectChangesAsync', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.post = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 204 })
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync(this.project_id)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should send a request to the project history api', function () {
|
|
|
|
this.request.post
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith({
|
|
|
|
url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`,
|
2021-07-13 07:04:42 -04:00
|
|
|
qs: { background: true },
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('flushProjectChanges', function () {
|
|
|
|
describe('in the normal case', function () {
|
2021-10-27 05:49:18 -04:00
|
|
|
beforeEach(function (done) {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.request.post = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 204 })
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.flushProjectChanges(
|
2021-10-27 05:49:18 -04:00
|
|
|
this.project_id,
|
|
|
|
{
|
|
|
|
background: true,
|
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should send a request to the project history api', function () {
|
|
|
|
this.request.post
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith({
|
|
|
|
url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`,
|
2021-07-13 07:04:42 -04:00
|
|
|
qs: { background: true },
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('with the skip_history_flush option', function () {
|
2021-10-27 05:49:18 -04:00
|
|
|
beforeEach(function (done) {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.request.post = sinon.stub()
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.flushProjectChanges(
|
2021-10-27 05:49:18 -04:00
|
|
|
this.project_id,
|
|
|
|
{
|
|
|
|
skip_history_flush: true,
|
|
|
|
},
|
|
|
|
done
|
|
|
|
)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should not send a request to the project history api', function () {
|
|
|
|
this.request.post.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('recordAndFlushHistoryOps', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.ops = ['mock-ops']
|
|
|
|
this.project_ops_length = 10
|
|
|
|
|
|
|
|
this.HistoryManager.flushProjectChangesAsync = sinon.stub()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with no ops', function () {
|
|
|
|
beforeEach(function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.recordAndFlushHistoryOps(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
[],
|
2023-06-06 06:19:55 -04:00
|
|
|
this.project_ops_length
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush project changes', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.recordAndFlushHistoryOps(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.ops,
|
2023-06-06 06:19:55 -04:00
|
|
|
this.project_ops_length
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should flush project changes', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.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)
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.recordAndFlushHistoryOps(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.ops,
|
2023-06-06 06:19:55 -04:00
|
|
|
this.project_ops_length
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush project changes', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('shouldFlushHistoryOps', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
it('should return false if the number of ops is not known', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.shouldFlushHistoryOps(
|
2020-05-06 06:11:36 -04:00
|
|
|
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
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.shouldFlushHistoryOps(
|
2020-05-06 06:11:36 -04:00
|
|
|
15,
|
|
|
|
['a', 'b', 'c'].length,
|
|
|
|
5
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should return true if the updates took past the threshold', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
// Currently there are 19 ops
|
|
|
|
// Previously we were on 16 ops
|
|
|
|
// We didn't pass over a multiple of 5
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.shouldFlushHistoryOps(
|
2020-05-06 06:11:36 -04:00
|
|
|
17,
|
|
|
|
['a', 'b', 'c'].length,
|
|
|
|
5
|
|
|
|
).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('resyncProjectHistory', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.projectHistoryId = 'history-id-1234'
|
|
|
|
this.docs = [
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
path: 'main.tex',
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
]
|
|
|
|
this.files = [
|
|
|
|
{
|
|
|
|
file: 'mock-file-id',
|
|
|
|
path: 'universe.png',
|
2021-07-13 07:04:42 -04:00
|
|
|
url: `www.filestore.test/${this.project_id}/mock-file-id`,
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
]
|
|
|
|
this.ProjectHistoryRedisManager.queueResyncProjectStructure = sinon
|
|
|
|
.stub()
|
|
|
|
.yields()
|
|
|
|
this.DocumentManager.resyncDocContentsWithLock = sinon.stub().yields()
|
2023-06-06 06:19:55 -04:00
|
|
|
this.HistoryManager.resyncProjectHistory(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.docs,
|
|
|
|
this.files,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should queue a project structure reync', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueResyncProjectStructure
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.docs,
|
|
|
|
this.files
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should queue doc content reyncs', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.DocumentManager.resyncDocContentsWithLock
|
2021-11-22 09:15:36 -05:00
|
|
|
.calledWith(this.project_id, this.docs[0].doc, this.docs[0].path)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|