2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/RedisManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const Errors = require('../../../../app/js/Errors')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const tk = require('timekeeper')
|
|
|
|
|
|
|
|
describe('RedisManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.multi = { exec: sinon.stub() }
|
|
|
|
this.rclient = { multi: () => this.multi }
|
|
|
|
tk.freeze(new Date())
|
|
|
|
this.RedisManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
|
2021-07-12 12:47:15 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2020-05-06 06:11:36 -04:00
|
|
|
documentupdater: { logHashErrors: { write: true, read: true } },
|
|
|
|
apis: {
|
2021-07-13 07:04:42 -04:00
|
|
|
project_history: { enabled: true },
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
redis: {
|
|
|
|
documentupdater: {
|
|
|
|
key_schema: {
|
2021-11-30 08:26:20 -05:00
|
|
|
blockingKey({ doc_id: docId }) {
|
|
|
|
return `Blocking:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
docLines({ doc_id: docId }) {
|
|
|
|
return `doclines:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
docOps({ doc_id: docId }) {
|
|
|
|
return `DocOps:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
docVersion({ doc_id: docId }) {
|
|
|
|
return `DocVersion:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
docHash({ doc_id: docId }) {
|
|
|
|
return `DocHash:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
projectKey({ doc_id: docId }) {
|
|
|
|
return `ProjectId:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
pendingUpdates({ doc_id: docId }) {
|
|
|
|
return `PendingUpdates:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
docsInProject({ project_id: projectId }) {
|
|
|
|
return `DocsIn:${projectId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
ranges({ doc_id: docId }) {
|
|
|
|
return `Ranges:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
pathname({ doc_id: docId }) {
|
|
|
|
return `Pathname:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
projectHistoryId({ doc_id: docId }) {
|
|
|
|
return `ProjectHistoryId:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
projectHistoryType({ doc_id: docId }) {
|
|
|
|
return `ProjectHistoryType:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
projectState({ project_id: projectId }) {
|
|
|
|
return `ProjectState:${projectId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
unflushedTime({ doc_id: docId }) {
|
|
|
|
return `UnflushedTime:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
lastUpdatedBy({ doc_id: docId }) {
|
|
|
|
return `lastUpdatedBy:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
lastUpdatedAt({ doc_id: docId }) {
|
|
|
|
return `lastUpdatedAt:${docId}`
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
history: {
|
|
|
|
key_schema: {
|
2021-11-30 08:26:20 -05:00
|
|
|
uncompressedHistoryOps({ doc_id: docId }) {
|
|
|
|
return `UncompressedHistoryOps:${docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
2021-11-30 08:26:20 -05:00
|
|
|
docsWithHistoryOps({ project_id: projectId }) {
|
|
|
|
return `DocsWithHistoryOps:${projectId}`
|
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
|
|
|
},
|
|
|
|
'./Metrics': (this.metrics = {
|
|
|
|
inc: sinon.stub(),
|
|
|
|
summary: sinon.stub(),
|
2021-11-30 08:26:20 -05:00
|
|
|
Timer: class Timer {
|
2020-05-06 06:11:36 -04:00
|
|
|
constructor() {
|
|
|
|
this.start = new Date()
|
|
|
|
}
|
|
|
|
|
|
|
|
done() {
|
|
|
|
const timeSpan = new Date() - this.start
|
|
|
|
return timeSpan
|
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
}),
|
2021-07-13 07:04:42 -04:00
|
|
|
'./Errors': Errors,
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId = 'doc-id-123'
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.projectHistoryId = 123
|
2021-11-30 08:26:20 -05:00
|
|
|
this.callback = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
tk.reset()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('getDoc', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.lines = ['one', 'two', 'three', 'これは'] // include some utf8
|
|
|
|
this.jsonlines = JSON.stringify(this.lines)
|
|
|
|
this.version = 42
|
|
|
|
this.hash = crypto
|
|
|
|
.createHash('sha1')
|
|
|
|
.update(this.jsonlines, 'utf8')
|
|
|
|
.digest('hex')
|
|
|
|
this.ranges = { comments: 'mock', entries: 'mock' }
|
|
|
|
this.json_ranges = JSON.stringify(this.ranges)
|
|
|
|
this.unflushed_time = 12345
|
|
|
|
this.pathname = '/a/b/c.tex'
|
2021-03-28 07:30:51 -04:00
|
|
|
this.rclient.mget = sinon
|
2020-05-06 06:11:36 -04:00
|
|
|
.stub()
|
2021-03-28 07:30:51 -04:00
|
|
|
.yields(null, [
|
2020-05-06 06:11:36 -04:00
|
|
|
this.jsonlines,
|
|
|
|
this.version,
|
|
|
|
this.hash,
|
|
|
|
this.project_id,
|
|
|
|
this.json_ranges,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId.toString(),
|
2021-07-13 07:04:42 -04:00
|
|
|
this.unflushed_time,
|
2020-05-06 06:11:36 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDoc(this.project_id, this.docId, this.callback)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should get all the details in one call to redis', function () {
|
|
|
|
this.rclient.mget
|
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`doclines:${this.docId}`,
|
|
|
|
`DocVersion:${this.docId}`,
|
|
|
|
`DocHash:${this.docId}`,
|
|
|
|
`ProjectId:${this.docId}`,
|
|
|
|
`Ranges:${this.docId}`,
|
|
|
|
`Pathname:${this.docId}`,
|
|
|
|
`ProjectHistoryId:${this.docId}`,
|
|
|
|
`UnflushedTime:${this.docId}`,
|
|
|
|
`lastUpdatedAt:${this.docId}`,
|
|
|
|
`lastUpdatedBy:${this.docId}`
|
2021-03-28 07:30:51 -04:00
|
|
|
)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the document', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWithExactly(
|
|
|
|
null,
|
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ranges,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.unflushed_time,
|
|
|
|
this.lastUpdatedAt,
|
|
|
|
this.lastUpdatedBy
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should not log any errors', function () {
|
|
|
|
this.logger.error.calledWith().should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a corrupted document', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.badHash = 'INVALID-HASH-VALUE'
|
2021-03-28 07:30:51 -04:00
|
|
|
this.rclient.mget = sinon
|
2020-05-06 06:11:36 -04:00
|
|
|
.stub()
|
2021-03-28 07:30:51 -04:00
|
|
|
.yields(null, [
|
2020-05-06 06:11:36 -04:00
|
|
|
this.jsonlines,
|
|
|
|
this.version,
|
|
|
|
this.badHash,
|
|
|
|
this.project_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
this.json_ranges,
|
2020-05-06 06:11:36 -04:00
|
|
|
])
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDoc(this.project_id, this.docId, this.callback)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should log a hash error', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.logger.error.calledWith().should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return the document', function () {
|
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(null, this.lines, this.version, this.ranges)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a slow request to redis', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.clock = sinon.useFakeTimers()
|
2021-03-28 07:30:51 -04:00
|
|
|
this.rclient.mget = (...args) => {
|
|
|
|
const cb = args.pop()
|
2020-05-06 06:11:36 -04:00
|
|
|
this.clock.tick(6000)
|
2021-11-30 08:26:20 -05:00
|
|
|
cb(null, [
|
2020-05-06 06:11:36 -04:00
|
|
|
this.jsonlines,
|
|
|
|
this.version,
|
|
|
|
this.another_project_id,
|
|
|
|
this.json_ranges,
|
|
|
|
this.pathname,
|
2021-07-13 07:04:42 -04:00
|
|
|
this.unflushed_time,
|
2020-05-06 06:11:36 -04:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDoc(this.project_id, this.docId, this.callback)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.clock.restore()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return an error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('getDoc with an invalid project id', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.another_project_id = 'project-id-456'
|
2021-03-28 07:30:51 -04:00
|
|
|
this.rclient.mget = sinon
|
2020-05-06 06:11:36 -04:00
|
|
|
.stub()
|
2021-03-28 07:30:51 -04:00
|
|
|
.yields(null, [
|
2020-05-06 06:11:36 -04:00
|
|
|
this.jsonlines,
|
|
|
|
this.version,
|
|
|
|
this.hash,
|
|
|
|
this.another_project_id,
|
|
|
|
this.json_ranges,
|
|
|
|
this.pathname,
|
2021-07-13 07:04:42 -04:00
|
|
|
this.unflushed_time,
|
2020-05-06 06:11:36 -04:00
|
|
|
])
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDoc(this.project_id, this.docId, this.callback)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return an error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Errors.NotFoundError))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPreviousDocOpsTests', function () {
|
|
|
|
describe('with a start and an end value', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.first_version_in_redis = 30
|
|
|
|
this.version = 70
|
|
|
|
this.length = this.version - this.first_version_in_redis
|
|
|
|
this.start = 50
|
|
|
|
this.end = 60
|
|
|
|
this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
|
2021-07-13 07:04:42 -04:00
|
|
|
this.jsonOps = this.ops.map(op => JSON.stringify(op))
|
2020-05-06 06:11:36 -04:00
|
|
|
this.rclient.llen = sinon.stub().callsArgWith(1, null, this.length)
|
|
|
|
this.rclient.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.version.toString())
|
|
|
|
this.rclient.lrange = sinon.stub().callsArgWith(3, null, this.jsonOps)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getPreviousDocOps(
|
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.start,
|
|
|
|
this.end,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the length of the existing doc ops', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.rclient.llen.calledWith(`DocOps:${this.docId}`).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the current version of the doc', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.rclient.get
|
|
|
|
.calledWith(`DocVersion:${this.docId}`)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the appropriate docs ops', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.rclient.lrange
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`DocOps:${this.docId}`,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.start - this.first_version_in_redis,
|
|
|
|
this.end - this.first_version_in_redis
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return the docs with the doc ops deserialized', function () {
|
|
|
|
this.callback.calledWith(null, this.ops).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an end value of -1', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.first_version_in_redis = 30
|
|
|
|
this.version = 70
|
|
|
|
this.length = this.version - this.first_version_in_redis
|
|
|
|
this.start = 50
|
|
|
|
this.end = -1
|
|
|
|
this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
|
2021-07-13 07:04:42 -04:00
|
|
|
this.jsonOps = this.ops.map(op => JSON.stringify(op))
|
2020-05-06 06:11:36 -04:00
|
|
|
this.rclient.llen = sinon.stub().callsArgWith(1, null, this.length)
|
|
|
|
this.rclient.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.version.toString())
|
|
|
|
this.rclient.lrange = sinon.stub().callsArgWith(3, null, this.jsonOps)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getPreviousDocOps(
|
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.start,
|
|
|
|
this.end,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the appropriate docs ops to the end of list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.rclient.lrange
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`DocOps:${this.docId}`,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.start - this.first_version_in_redis,
|
|
|
|
-1
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return the docs with the doc ops deserialized', function () {
|
|
|
|
this.callback.calledWith(null, this.ops).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the requested range is not in Redis', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.first_version_in_redis = 30
|
|
|
|
this.version = 70
|
|
|
|
this.length = this.version - this.first_version_in_redis
|
|
|
|
this.start = 20
|
|
|
|
this.end = -1
|
|
|
|
this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
|
2021-07-13 07:04:42 -04:00
|
|
|
this.jsonOps = this.ops.map(op => JSON.stringify(op))
|
2020-05-06 06:11:36 -04:00
|
|
|
this.rclient.llen = sinon.stub().callsArgWith(1, null, this.length)
|
|
|
|
this.rclient.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.version.toString())
|
|
|
|
this.rclient.lrange = sinon.stub().callsArgWith(3, null, this.jsonOps)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getPreviousDocOps(
|
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.start,
|
|
|
|
this.end,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return an error', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Errors.OpRangeNotAvailableError))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should log out the problem as a debug message', function () {
|
|
|
|
this.logger.debug.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('with a slow request to redis', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.first_version_in_redis = 30
|
|
|
|
this.version = 70
|
|
|
|
this.length = this.version - this.first_version_in_redis
|
|
|
|
this.start = 50
|
|
|
|
this.end = 60
|
|
|
|
this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
|
2021-07-13 07:04:42 -04:00
|
|
|
this.jsonOps = this.ops.map(op => JSON.stringify(op))
|
2020-05-06 06:11:36 -04:00
|
|
|
this.rclient.llen = sinon.stub().callsArgWith(1, null, this.length)
|
|
|
|
this.rclient.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.version.toString())
|
|
|
|
this.clock = sinon.useFakeTimers()
|
|
|
|
this.rclient.lrange = (key, start, end, cb) => {
|
|
|
|
this.clock.tick(6000)
|
2021-11-30 08:26:20 -05:00
|
|
|
cb(null, this.jsonOps)
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getPreviousDocOps(
|
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.start,
|
|
|
|
this.end,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.clock.restore()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return an error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('updateDocument', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.lines = ['one', 'two', 'three', 'これは']
|
|
|
|
this.ops = [{ op: [{ i: 'foo', p: 4 }] }, { op: [{ i: 'bar', p: 8 }] }]
|
|
|
|
this.version = 42
|
|
|
|
this.hash = crypto
|
|
|
|
.createHash('sha1')
|
|
|
|
.update(JSON.stringify(this.lines), 'utf8')
|
|
|
|
.digest('hex')
|
|
|
|
this.ranges = { comments: 'mock', entries: 'mock' }
|
|
|
|
this.updateMeta = { user_id: 'last-author-fake-id' }
|
|
|
|
this.doc_update_list_length = sinon.stub()
|
|
|
|
this.project_update_list_length = sinon.stub()
|
|
|
|
|
|
|
|
this.RedisManager.getDocVersion = sinon.stub()
|
2021-03-28 07:30:51 -04:00
|
|
|
this.multi.mset = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
this.multi.set = sinon.stub()
|
|
|
|
this.multi.rpush = sinon.stub()
|
|
|
|
this.multi.expire = sinon.stub()
|
|
|
|
this.multi.ltrim = sinon.stub()
|
|
|
|
this.multi.del = sinon.stub()
|
|
|
|
this.multi.exec = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, [
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
this.doc_update_list_length,
|
|
|
|
null,
|
2021-07-13 07:04:42 -04:00
|
|
|
null,
|
2020-05-06 06:11:36 -04:00
|
|
|
])
|
2021-11-30 08:26:20 -05:00
|
|
|
this.ProjectHistoryRedisManager.queueOps = sinon
|
2020-05-06 06:11:36 -04:00
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
this.ops.length + 1,
|
|
|
|
null,
|
|
|
|
this.project_update_list_length
|
2021-11-30 08:26:20 -05:00
|
|
|
)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a consistent version', function () {
|
|
|
|
beforeEach(function () {})
|
|
|
|
|
|
|
|
describe('with project history enabled', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.settings.apis.project_history.enabled = true
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the current doc version to check for consistency', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDocVersion
|
|
|
|
.calledWith(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should set most details in a single MSET call', function () {
|
|
|
|
this.multi.mset
|
|
|
|
.calledWith({
|
2021-11-30 08:26:20 -05:00
|
|
|
[`doclines:${this.docId}`]: JSON.stringify(this.lines),
|
|
|
|
[`DocVersion:${this.docId}`]: this.version,
|
|
|
|
[`DocHash:${this.docId}`]: this.hash,
|
|
|
|
[`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
|
|
|
|
[`lastUpdatedAt:${this.docId}`]: Date.now(),
|
|
|
|
[`lastUpdatedBy:${this.docId}`]: 'last-author-fake-id',
|
2021-03-28 07:30:51 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the unflushed time', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.set
|
|
|
|
.calledWith(`UnflushedTime:${this.docId}`, Date.now(), 'NX')
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the doc op into the doc ops list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.rpush
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`DocOps:${this.docId}`,
|
2020-05-06 06:11:36 -04:00
|
|
|
JSON.stringify(this.ops[0]),
|
|
|
|
JSON.stringify(this.ops[1])
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should renew the expiry ttl on the doc ops array', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.expire
|
|
|
|
.calledWith(`DocOps:${this.docId}`, this.RedisManager.DOC_OPS_TTL)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should truncate the list to 100 members', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.ltrim
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`DocOps:${this.docId}`,
|
2020-05-06 06:11:36 -04:00
|
|
|
-this.RedisManager.DOC_OPS_MAX_LENGTH,
|
|
|
|
-1
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the updates into the history ops list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.rpush
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`UncompressedHistoryOps:${this.docId}`,
|
2020-05-06 06:11:36 -04:00
|
|
|
JSON.stringify(this.ops[0]),
|
|
|
|
JSON.stringify(this.ops[1])
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the updates into the project history ops list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.ProjectHistoryRedisManager.queueOps
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, JSON.stringify(this.ops[0]))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the callback', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
null,
|
|
|
|
this.doc_update_list_length,
|
|
|
|
this.project_update_list_length
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should not log any errors', function () {
|
|
|
|
this.logger.error.calledWith().should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with project history disabled', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.settings.apis.project_history.enabled = false
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not push the updates into the project history ops list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.ProjectHistoryRedisManager.queueOps.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(null, this.doc_update_list_length)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('with a doc using project history only', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length, 'project-history')
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not push the updates to the track-changes ops list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.rpush
|
|
|
|
.calledWith(`UncompressedHistoryOps:${this.docId}`)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the updates into the project history ops list', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.ProjectHistoryRedisManager.queueOps
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id, JSON.stringify(this.ops[0]))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback with the project update count only', function () {
|
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(null, undefined, this.project_update_list_length)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an inconsistent version', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length - 1)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not call multi.exec', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.exec.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback with an error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with no updates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
[],
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not try to enqueue doc updates', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.rpush.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not try to enqueue project updates', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.ProjectHistoryRedisManager.queueOps.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should still set the doclines', function () {
|
2021-03-28 07:30:51 -04:00
|
|
|
this.multi.mset
|
|
|
|
.calledWith({
|
2021-11-30 08:26:20 -05:00
|
|
|
[`doclines:${this.docId}`]: JSON.stringify(this.lines),
|
|
|
|
[`DocVersion:${this.docId}`]: this.version,
|
|
|
|
[`DocHash:${this.docId}`]: this.hash,
|
|
|
|
[`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
|
|
|
|
[`lastUpdatedAt:${this.docId}`]: Date.now(),
|
|
|
|
[`lastUpdatedBy:${this.docId}`]: 'last-author-fake-id',
|
2021-03-28 07:30:51 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with empty ranges', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
{},
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should set empty ranges', function () {
|
|
|
|
this.multi.mset
|
|
|
|
.calledWith({
|
2021-11-30 08:26:20 -05:00
|
|
|
[`doclines:${this.docId}`]: JSON.stringify(this.lines),
|
|
|
|
[`DocVersion:${this.docId}`]: this.version,
|
|
|
|
[`DocHash:${this.docId}`]: this.hash,
|
|
|
|
[`Ranges:${this.docId}`]: null,
|
|
|
|
[`lastUpdatedAt:${this.docId}`]: Date.now(),
|
|
|
|
[`lastUpdatedBy:${this.docId}`]: 'last-author-fake-id',
|
2021-03-28 07:30:51 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with null bytes in the serialized doc lines', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length)
|
2021-04-01 15:51:00 -04:00
|
|
|
this.stringifyStub = sinon
|
|
|
|
.stub(JSON, 'stringify')
|
|
|
|
.callsFake(() => '["bad bytes! \u0000 <- here"]')
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
2021-04-01 15:51:00 -04:00
|
|
|
this.stringifyStub.restore()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should log an error', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.logger.error.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback with an error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with ranges that are too big', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length)
|
|
|
|
this.RedisManager._serializeRanges = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(new Error('ranges are too large'))
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
this.updateMeta,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log an error', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.logger.error.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('without user id from meta', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDocVersion
|
2021-11-30 08:26:20 -05:00
|
|
|
.withArgs(this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.yields(null, this.version - this.ops.length)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.updateDocument(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ops,
|
|
|
|
this.ranges,
|
|
|
|
{},
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should unset last updater', function () {
|
|
|
|
this.multi.mset
|
|
|
|
.calledWith({
|
2021-11-30 08:26:20 -05:00
|
|
|
[`doclines:${this.docId}`]: JSON.stringify(this.lines),
|
|
|
|
[`DocVersion:${this.docId}`]: this.version,
|
|
|
|
[`DocHash:${this.docId}`]: this.hash,
|
|
|
|
[`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
|
|
|
|
[`lastUpdatedAt:${this.docId}`]: Date.now(),
|
|
|
|
[`lastUpdatedBy:${this.docId}`]: undefined,
|
2021-03-28 07:30:51 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('putDocInMemory', function () {
|
|
|
|
beforeEach(function () {
|
2021-03-28 07:30:51 -04:00
|
|
|
this.rclient.mset = sinon.stub().yields(null)
|
2020-05-06 06:11:36 -04:00
|
|
|
this.rclient.sadd = sinon.stub().yields()
|
|
|
|
this.lines = ['one', 'two', 'three', 'これは']
|
|
|
|
this.version = 42
|
|
|
|
this.hash = crypto
|
|
|
|
.createHash('sha1')
|
|
|
|
.update(JSON.stringify(this.lines), 'utf8')
|
|
|
|
.digest('hex')
|
|
|
|
this.ranges = { comments: 'mock', entries: 'mock' }
|
2021-11-30 08:26:20 -05:00
|
|
|
this.pathname = '/a/b/c.tex'
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('with non-empty ranges', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.putDocInMemory(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ranges,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should set all the details in a single MSET call', function () {
|
|
|
|
this.rclient.mset
|
|
|
|
.calledWith({
|
2021-11-30 08:26:20 -05:00
|
|
|
[`doclines:${this.docId}`]: JSON.stringify(this.lines),
|
|
|
|
[`ProjectId:${this.docId}`]: this.project_id,
|
|
|
|
[`DocVersion:${this.docId}`]: this.version,
|
|
|
|
[`DocHash:${this.docId}`]: this.hash,
|
|
|
|
[`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
|
|
|
|
[`Pathname:${this.docId}`]: this.pathname,
|
|
|
|
[`ProjectHistoryId:${this.docId}`]: this.projectHistoryId,
|
2021-03-28 07:30:51 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should add the docId to the project set', function () {
|
|
|
|
this.rclient.sadd
|
|
|
|
.calledWith(`DocsIn:${this.project_id}`, this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should not log any errors', function () {
|
|
|
|
this.logger.error.calledWith().should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with empty ranges', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.putDocInMemory(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
{},
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should unset ranges', function () {
|
|
|
|
this.rclient.mset
|
|
|
|
.calledWith({
|
2021-11-30 08:26:20 -05:00
|
|
|
[`doclines:${this.docId}`]: JSON.stringify(this.lines),
|
|
|
|
[`ProjectId:${this.docId}`]: this.project_id,
|
|
|
|
[`DocVersion:${this.docId}`]: this.version,
|
|
|
|
[`DocHash:${this.docId}`]: this.hash,
|
|
|
|
[`Ranges:${this.docId}`]: null,
|
|
|
|
[`Pathname:${this.docId}`]: this.pathname,
|
|
|
|
[`ProjectHistoryId:${this.docId}`]: this.projectHistoryId,
|
2021-03-28 07:30:51 -04:00
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with null bytes in the serialized doc lines', function () {
|
|
|
|
beforeEach(function () {
|
2021-04-01 15:51:00 -04:00
|
|
|
this.stringifyStub = sinon
|
|
|
|
.stub(JSON, 'stringify')
|
|
|
|
.callsFake(() => '["bad bytes! \u0000 <- here"]')
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.putDocInMemory(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ranges,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
2021-04-01 15:51:00 -04:00
|
|
|
this.stringifyStub.restore()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should log an error', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.logger.error.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback with an error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('with ranges that are too big', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager._serializeRanges = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(new Error('ranges are too large'))
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.putDocInMemory(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.lines,
|
|
|
|
this.version,
|
|
|
|
this.ranges,
|
|
|
|
this.pathname,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log an error', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.logger.error.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback
|
2020-05-15 14:29:49 -04:00
|
|
|
.calledWith(sinon.match.instanceOf(Error))
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('removeDocFromMemory', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.multi.strlen = sinon.stub()
|
|
|
|
this.multi.del = sinon.stub()
|
|
|
|
this.multi.srem = sinon.stub()
|
|
|
|
this.multi.exec.yields()
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.removeDocFromMemory(this.project_id, this.docId, done)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should check the length of the current doclines', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.strlen.calledWith(`doclines:${this.docId}`).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-03-28 07:30:51 -04:00
|
|
|
it('should delete the details in a singe call', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.multi.del
|
2021-03-28 07:30:51 -04:00
|
|
|
.calledWith(
|
2021-11-30 08:26:20 -05:00
|
|
|
`doclines:${this.docId}`,
|
|
|
|
`ProjectId:${this.docId}`,
|
|
|
|
`DocVersion:${this.docId}`,
|
|
|
|
`DocHash:${this.docId}`,
|
|
|
|
`Ranges:${this.docId}`,
|
|
|
|
`Pathname:${this.docId}`,
|
|
|
|
`ProjectHistoryId:${this.docId}`,
|
|
|
|
`ProjectHistoryType:${this.docId}`,
|
|
|
|
`UnflushedTime:${this.docId}`,
|
|
|
|
`lastUpdatedAt:${this.docId}`,
|
|
|
|
`lastUpdatedBy:${this.docId}`
|
2021-03-28 07:30:51 -04:00
|
|
|
)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should remove the docId from the project set', function () {
|
|
|
|
this.multi.srem
|
|
|
|
.calledWith(`DocsIn:${this.project_id}`, this.docId)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('clearProjectState', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.rclient.del = sinon.stub().callsArg(1)
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.clearProjectState(this.project_id, done)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should delete the project state', function () {
|
|
|
|
this.rclient.del
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(`ProjectState:${this.project_id}`)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('renameDoc', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.rpush = sinon.stub().yields()
|
|
|
|
this.rclient.set = sinon.stub().yields()
|
2021-11-30 08:26:20 -05:00
|
|
|
this.update = {
|
|
|
|
id: this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
pathname: (this.pathname = 'pathname'),
|
2021-07-13 07:04:42 -04:00
|
|
|
newPathname: (this.newPathname = 'new-pathname'),
|
2021-11-30 08:26:20 -05:00
|
|
|
}
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('the document is cached in redis', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, 'lines', 'version')
|
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity = sinon
|
|
|
|
.stub()
|
|
|
|
.yields()
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.renameDoc(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.userId,
|
|
|
|
this.update,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('update the cached pathname', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.rclient.set
|
|
|
|
.calledWith(`Pathname:${this.docId}`, this.newPathname)
|
2020-05-06 06:11:36 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('the document is not cached in redis', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RedisManager.getDoc = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, null, null)
|
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity = sinon
|
|
|
|
.stub()
|
|
|
|
.yields()
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.renameDoc(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
2021-11-30 08:26:20 -05:00
|
|
|
this.docId,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.userId,
|
|
|
|
this.update,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not update the cached pathname', function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.rclient.set.called.should.equal(false)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('getDocVersion', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
2021-11-30 08:26:20 -05:00
|
|
|
this.version = 12345
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the document does not have a project history type set', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.mget = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(
|
2021-11-30 08:26:20 -05:00
|
|
|
`DocVersion:${this.docId}`,
|
|
|
|
`ProjectHistoryType:${this.docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.callsArgWith(2, null, [`${this.version}`])
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDocVersion(this.docId, this.callback)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return the document version and an undefined history type', function () {
|
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWithExactly(null, this.version, undefined)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
describe('when the document has a project history type set', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.mget = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(
|
2021-11-30 08:26:20 -05:00
|
|
|
`DocVersion:${this.docId}`,
|
|
|
|
`ProjectHistoryType:${this.docId}`
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.callsArgWith(2, null, [`${this.version}`, 'project-history'])
|
2021-11-30 08:26:20 -05:00
|
|
|
this.RedisManager.getDocVersion(this.docId, this.callback)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2021-11-30 08:26:20 -05:00
|
|
|
it('should return the document version and history type', function () {
|
|
|
|
this.callback
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWithExactly(null, this.version, 'project-history')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|