2020-05-06 06:11:22 -04: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-05-06 06:10:51 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/ProjectHistoryRedisManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const tk = require('timekeeper')
|
|
|
|
|
|
|
|
describe('ProjectHistoryRedisManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.projectHistoryId = 'history-id-123'
|
|
|
|
this.user_id = 'user-id-123'
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.rclient = {}
|
|
|
|
tk.freeze(new Date())
|
2021-11-25 04:25:28 -05:00
|
|
|
|
|
|
|
this.Limits = {
|
|
|
|
docIsTooLarge: sinon.stub().returns(false),
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
return (this.ProjectHistoryRedisManager = SandboxedModule.require(
|
|
|
|
modulePath,
|
|
|
|
{
|
|
|
|
requires: {
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2021-11-25 04:25:28 -05:00
|
|
|
max_doc_length: 123,
|
2020-05-06 06:11:36 -04:00
|
|
|
redis: {
|
|
|
|
project_history: {
|
|
|
|
key_schema: {
|
|
|
|
projectHistoryOps({ project_id }) {
|
|
|
|
return `ProjectHistory:Ops:${project_id}`
|
|
|
|
},
|
|
|
|
projectHistoryFirstOpTimestamp({ project_id }) {
|
|
|
|
return `ProjectHistory:FirstOpTimestamp:${project_id}`
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
}),
|
2020-11-10 06:32:04 -05:00
|
|
|
'@overleaf/redis-wrapper': {
|
2021-07-13 07:04:42 -04:00
|
|
|
createClient: () => this.rclient,
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-07-13 07:04:42 -04:00
|
|
|
'./Metrics': (this.metrics = { summary: sinon.stub() }),
|
2021-11-25 04:25:28 -05:00
|
|
|
'./Limits': this.Limits,
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
return tk.reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('queueOps', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.ops = ['mock-op-1', 'mock-op-2']
|
|
|
|
this.multi = { exec: sinon.stub() }
|
|
|
|
this.multi.rpush = sinon.stub()
|
|
|
|
this.multi.setnx = sinon.stub()
|
|
|
|
this.rclient.multi = () => this.multi
|
|
|
|
// @rclient = multi: () => @multi
|
|
|
|
return this.ProjectHistoryRedisManager.queueOps(
|
|
|
|
this.project_id,
|
|
|
|
...Array.from(this.ops),
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should queue an update', function () {
|
|
|
|
return this.multi.rpush
|
|
|
|
.calledWithExactly(
|
|
|
|
`ProjectHistory:Ops:${this.project_id}`,
|
|
|
|
this.ops[0],
|
|
|
|
this.ops[1]
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should set the queue timestamp if not present', function () {
|
|
|
|
return this.multi.setnx
|
|
|
|
.calledWithExactly(
|
|
|
|
`ProjectHistory:FirstOpTimestamp:${this.project_id}`,
|
|
|
|
Date.now()
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('queueRenameEntity', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.file_id = 1234
|
|
|
|
|
|
|
|
this.rawUpdate = {
|
|
|
|
pathname: (this.pathname = '/old'),
|
|
|
|
newPathname: (this.newPathname = '/new'),
|
2021-07-13 07:04:42 -04:00
|
|
|
version: (this.version = 2),
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectHistoryRedisManager.queueOps = sinon.stub()
|
|
|
|
return this.ProjectHistoryRedisManager.queueRenameEntity(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.file_id,
|
|
|
|
this.user_id,
|
|
|
|
this.rawUpdate,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should queue an update', function () {
|
|
|
|
const update = {
|
|
|
|
pathname: this.pathname,
|
|
|
|
new_pathname: this.newPathname,
|
|
|
|
meta: {
|
|
|
|
user_id: this.user_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
version: this.version,
|
|
|
|
projectHistoryId: this.projectHistoryId,
|
2021-07-13 07:04:42 -04:00
|
|
|
file: this.file_id,
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.ProjectHistoryRedisManager.queueOps
|
|
|
|
.calledWithExactly(
|
|
|
|
this.project_id,
|
2021-04-01 15:51:00 -04:00
|
|
|
JSON.stringify(update),
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('queueAddEntity', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.rpush = sinon.stub().yields()
|
|
|
|
this.doc_id = 1234
|
|
|
|
|
|
|
|
this.rawUpdate = {
|
|
|
|
pathname: (this.pathname = '/old'),
|
|
|
|
docLines: (this.docLines = 'a\nb'),
|
|
|
|
version: (this.version = 2),
|
2021-07-13 07:04:42 -04:00
|
|
|
url: (this.url = 'filestore.example.com'),
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectHistoryRedisManager.queueOps = sinon.stub()
|
|
|
|
return this.ProjectHistoryRedisManager.queueAddEntity(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
this.doc_id,
|
|
|
|
this.user_id,
|
|
|
|
this.rawUpdate,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should queue an update', function () {
|
|
|
|
const update = {
|
|
|
|
pathname: this.pathname,
|
|
|
|
docLines: this.docLines,
|
|
|
|
url: this.url,
|
|
|
|
meta: {
|
|
|
|
user_id: this.user_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
version: this.version,
|
|
|
|
projectHistoryId: this.projectHistoryId,
|
2021-07-13 07:04:42 -04:00
|
|
|
doc: this.doc_id,
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.ProjectHistoryRedisManager.queueOps
|
|
|
|
.calledWithExactly(
|
|
|
|
this.project_id,
|
2021-04-01 15:51:00 -04:00
|
|
|
JSON.stringify(update),
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('queueResyncProjectStructure', function () {
|
|
|
|
return it('should queue an update', function () {})
|
|
|
|
})
|
|
|
|
|
2021-11-25 04:25:28 -05:00
|
|
|
describe('queueResyncDocContent', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.doc_id = 1234
|
|
|
|
this.lines = ['one', 'two']
|
|
|
|
this.version = 2
|
|
|
|
this.pathname = '/path'
|
|
|
|
|
|
|
|
this.update = {
|
|
|
|
resyncDocContent: {
|
|
|
|
content: this.lines.join('\n'),
|
|
|
|
version: this.version,
|
|
|
|
},
|
|
|
|
projectHistoryId: this.projectHistoryId,
|
|
|
|
path: this.pathname,
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
ts: new Date(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ProjectHistoryRedisManager.queueOps = sinon.stub()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a good doc', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.ProjectHistoryRedisManager.queueResyncDocContent(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.doc_id,
|
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.pathname,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
it('should check if the doc is too large', function () {
|
|
|
|
this.Limits.docIsTooLarge
|
|
|
|
.calledWith(
|
|
|
|
JSON.stringify(this.update).length,
|
|
|
|
this.lines,
|
|
|
|
this.settings.max_doc_length
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should queue an update', function () {
|
|
|
|
this.ProjectHistoryRedisManager.queueOps
|
|
|
|
.calledWithExactly(
|
|
|
|
this.project_id,
|
|
|
|
JSON.stringify(this.update),
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a doc that is too large', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.Limits.docIsTooLarge.returns(true)
|
|
|
|
this.ProjectHistoryRedisManager.queueResyncDocContent(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.doc_id,
|
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.pathname,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
it('should not queue an update if the doc is too large', function () {
|
|
|
|
this.ProjectHistoryRedisManager.queueOps.called.should.equal(false)
|
|
|
|
this.callback
|
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|