2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/ProjectManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
describe('ProjectManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 08:40:11 -04:00
|
|
|
this.RedisManager = {}
|
|
|
|
this.ProjectHistoryRedisManager = {
|
|
|
|
queueRenameEntity: sinon.stub().yields(),
|
2021-07-13 07:04:42 -04:00
|
|
|
queueAddEntity: sinon.stub().yields(),
|
2020-05-15 08:40:11 -04:00
|
|
|
}
|
|
|
|
this.DocumentManager = {
|
2021-07-13 07:04:42 -04:00
|
|
|
renameDocWithLock: sinon.stub().yields(),
|
2020-05-15 08:40:11 -04:00
|
|
|
}
|
|
|
|
this.HistoryManager = {
|
|
|
|
flushProjectChangesAsync: sinon.stub(),
|
2021-07-13 07:04:42 -04:00
|
|
|
shouldFlushHistoryOps: sinon.stub().returns(false),
|
2020-05-15 08:40:11 -04:00
|
|
|
}
|
2024-06-18 10:00:00 -04:00
|
|
|
this.LockManager = {
|
|
|
|
getLock: sinon.stub().yields(),
|
|
|
|
releaseLock: sinon.stub().yields(),
|
|
|
|
}
|
2020-05-15 08:40:11 -04:00
|
|
|
this.Metrics = {
|
2021-07-13 07:04:42 -04:00
|
|
|
Timer: class Timer {},
|
2020-05-15 08:40:11 -04:00
|
|
|
}
|
|
|
|
this.Metrics.Timer.prototype.done = sinon.stub()
|
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
this.ProjectManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-05-15 08:40:11 -04:00
|
|
|
'./RedisManager': this.RedisManager,
|
|
|
|
'./ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
|
|
|
|
'./DocumentManager': this.DocumentManager,
|
|
|
|
'./HistoryManager': this.HistoryManager,
|
2024-06-18 10:00:00 -04:00
|
|
|
'./LockManager': this.LockManager,
|
2021-07-13 07:04:42 -04:00
|
|
|
'./Metrics': this.Metrics,
|
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.projectHistoryId = 'history-id-123'
|
|
|
|
this.user_id = 'user-id-123'
|
|
|
|
this.version = 1234567
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source = 'editor'
|
2020-05-15 08:31:06 -04:00
|
|
|
this.callback = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('updateProjectWithLocks', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('rename operations', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.firstDocUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'rename-doc',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 1,
|
|
|
|
pathname: 'foo',
|
2021-07-13 07:04:42 -04:00
|
|
|
newPathname: 'foo',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
this.secondDocUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'rename-doc',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 2,
|
|
|
|
pathname: 'bar',
|
2021-07-13 07:04:42 -04:00
|
|
|
newPathname: 'bar2',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
this.firstFileUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'rename-file',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 2,
|
|
|
|
pathname: 'bar',
|
2021-07-13 07:04:42 -04:00
|
|
|
newPathname: 'bar2',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates = [
|
|
|
|
this.firstDocUpdate,
|
|
|
|
this.secondDocUpdate,
|
2021-07-13 07:04:42 -04:00
|
|
|
this.firstFileUpdate,
|
2020-05-15 15:54:31 -04:00
|
|
|
]
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should rename the docs in the updates', function () {
|
|
|
|
const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
|
2021-07-13 07:04:42 -04:00
|
|
|
version: `${this.version}.0`,
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
const secondDocUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.secondDocUpdate,
|
|
|
|
{ version: `${this.version}.1` }
|
|
|
|
)
|
|
|
|
this.DocumentManager.renameDocWithLock
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.firstDocUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
firstDocUpdateWithVersion,
|
|
|
|
this.projectHistoryId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.DocumentManager.renameDocWithLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.secondDocUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
secondDocUpdateWithVersion,
|
|
|
|
this.projectHistoryId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should rename the files in the updates', function () {
|
|
|
|
const firstFileUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.firstFileUpdate,
|
|
|
|
{ version: `${this.version}.2` }
|
|
|
|
)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.firstFileUpdate.id,
|
|
|
|
this.user_id,
|
2022-08-25 08:01:39 -04:00
|
|
|
firstFileUpdateWithVersion,
|
|
|
|
this.source
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush the history', function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when renaming a doc fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
2020-05-15 08:40:11 -04:00
|
|
|
this.DocumentManager.renameDocWithLock.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when renaming a file fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
2020-05-15 08:40:11 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('with enough ops to flush', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
2020-05-15 08:40:11 -04:00
|
|
|
this.HistoryManager.shouldFlushHistoryOps.returns(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should flush the history', function () {
|
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('add operations', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.firstDocUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'add-doc',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 1,
|
2021-07-13 07:04:42 -04:00
|
|
|
docLines: 'a\nb',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
this.secondDocUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'add-doc',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 2,
|
2021-07-13 07:04:42 -04:00
|
|
|
docLines: 'a\nb',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
this.firstFileUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'add-file',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 3,
|
2021-07-13 07:04:42 -04:00
|
|
|
url: 'filestore.example.com/2',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
this.secondFileUpdate = {
|
2020-05-15 15:54:31 -04:00
|
|
|
type: 'add-file',
|
2020-05-06 06:11:36 -04:00
|
|
|
id: 4,
|
2021-07-13 07:04:42 -04:00
|
|
|
url: 'filestore.example.com/3',
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates = [
|
|
|
|
this.firstDocUpdate,
|
|
|
|
this.secondDocUpdate,
|
|
|
|
this.firstFileUpdate,
|
2021-07-13 07:04:42 -04:00
|
|
|
this.secondFileUpdate,
|
2020-05-15 15:54:31 -04:00
|
|
|
]
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add the docs in the updates', function () {
|
|
|
|
const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
|
2021-07-13 07:04:42 -04:00
|
|
|
version: `${this.version}.0`,
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
const secondDocUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.secondDocUpdate,
|
|
|
|
{ version: `${this.version}.1` }
|
|
|
|
)
|
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
|
|
|
.getCall(0)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
this.firstDocUpdate.id,
|
|
|
|
this.user_id,
|
2022-08-25 08:01:39 -04:00
|
|
|
firstDocUpdateWithVersion,
|
|
|
|
this.source
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
2020-05-06 06:11:36 -04:00
|
|
|
.getCall(1)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
this.secondDocUpdate.id,
|
|
|
|
this.user_id,
|
2022-08-25 08:01:39 -04:00
|
|
|
secondDocUpdateWithVersion,
|
|
|
|
this.source
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add the files in the updates', function () {
|
|
|
|
const firstFileUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.firstFileUpdate,
|
|
|
|
{ version: `${this.version}.2` }
|
|
|
|
)
|
|
|
|
const secondFileUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.secondFileUpdate,
|
|
|
|
{ version: `${this.version}.3` }
|
|
|
|
)
|
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
|
|
|
.getCall(2)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.firstFileUpdate.id,
|
|
|
|
this.user_id,
|
2022-08-25 08:01:39 -04:00
|
|
|
firstFileUpdateWithVersion,
|
|
|
|
this.source
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
2020-05-06 06:11:36 -04:00
|
|
|
.getCall(3)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.secondFileUpdate.id,
|
|
|
|
this.user_id,
|
2022-08-25 08:01:39 -04:00
|
|
|
secondFileUpdateWithVersion,
|
|
|
|
this.source
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush the history', function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when adding a doc fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
2020-05-15 08:40:11 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when adding a file fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
2020-05-15 08:40:11 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('with enough ops to flush', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
2020-05-15 08:40:11 -04:00
|
|
|
this.HistoryManager.shouldFlushHistoryOps.returns(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.updates,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should flush the history', function () {
|
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-05-15 15:54:31 -04:00
|
|
|
|
|
|
|
describe('when given an unknown operation type', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.updates = [{ type: 'brew-coffee' }]
|
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.updates,
|
|
|
|
this.version,
|
2022-08-25 08:01:39 -04:00
|
|
|
this.source,
|
2020-05-15 15:54:31 -04:00
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call back with an error', function () {
|
|
|
|
this.callback.calledWith(sinon.match.instanceOf(Error)).should.be.true
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|