| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- const sinon = require('sinon')
- const modulePath = '../../../../app/js/ProjectManager.js'
- const SandboxedModule = require('sandboxed-module')
- const _ = require('lodash')
- describe('ProjectManager', function () {
- beforeEach(function () {
- this.RedisManager = {}
- this.ProjectHistoryRedisManager = {
- queueRenameEntity: sinon.stub().yields(),
- queueAddEntity: sinon.stub().yields(),
- }
- this.DocumentManager = {
- renameDocWithLock: sinon.stub().yields(),
- }
- this.HistoryManager = {
- flushProjectChangesAsync: sinon.stub(),
- shouldFlushHistoryOps: sinon.stub().returns(false),
- }
- this.LockManager = {
- getLock: sinon.stub().yields(),
- releaseLock: sinon.stub().yields(),
- }
- this.Metrics = {
- Timer: class Timer {},
- }
- this.Metrics.Timer.prototype.done = sinon.stub()
- this.ProjectManager = SandboxedModule.require(modulePath, {
- requires: {
- './RedisManager': this.RedisManager,
- './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
- './DocumentManager': this.DocumentManager,
- './HistoryManager': this.HistoryManager,
- './LockManager': this.LockManager,
- './Metrics': this.Metrics,
- },
- })
- this.project_id = 'project-id-123'
- this.projectHistoryId = 'history-id-123'
- this.user_id = 'user-id-123'
- this.version = 1234567
- this.source = 'editor'
- this.callback = sinon.stub()
- })
- describe('updateProjectWithLocks', function () {
- describe('rename operations', function () {
- beforeEach(function () {
- this.firstDocUpdate = {
- type: 'rename-doc',
- id: 1,
- pathname: 'foo',
- newPathname: 'foo',
- }
- this.secondDocUpdate = {
- type: 'rename-doc',
- id: 2,
- pathname: 'bar',
- newPathname: 'bar2',
- }
- this.firstFileUpdate = {
- type: 'rename-file',
- id: 2,
- pathname: 'bar',
- newPathname: 'bar2',
- }
- this.updates = [
- this.firstDocUpdate,
- this.secondDocUpdate,
- this.firstFileUpdate,
- ]
- })
- describe('successfully', function () {
- beforeEach(function () {
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should rename the docs in the updates', function () {
- const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
- version: `${this.version}.0`,
- })
- 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)
- this.DocumentManager.renameDocWithLock
- .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` }
- )
- this.ProjectHistoryRedisManager.queueRenameEntity
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- 'file',
- this.firstFileUpdate.id,
- this.user_id,
- firstFileUpdateWithVersion,
- this.source
- )
- .should.equal(true)
- })
- it('should not flush the history', function () {
- this.HistoryManager.flushProjectChangesAsync
- .calledWith(this.project_id)
- .should.equal(false)
- })
- it('should call the callback', function () {
- this.callback.called.should.equal(true)
- })
- })
- describe('when renaming a doc fails', function () {
- beforeEach(function () {
- this.error = new Error('error')
- this.DocumentManager.renameDocWithLock.yields(this.error)
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should call the callback with the error', function () {
- this.callback.calledWith(this.error).should.equal(true)
- })
- })
- describe('when renaming a file fails', function () {
- beforeEach(function () {
- this.error = new Error('error')
- this.ProjectHistoryRedisManager.queueRenameEntity.yields(this.error)
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should call the callback with the error', function () {
- this.callback.calledWith(this.error).should.equal(true)
- })
- })
- describe('with enough ops to flush', function () {
- beforeEach(function () {
- this.HistoryManager.shouldFlushHistoryOps.returns(true)
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should flush the history', function () {
- this.HistoryManager.flushProjectChangesAsync
- .calledWith(this.project_id)
- .should.equal(true)
- })
- })
- })
- describe('add operations', function () {
- beforeEach(function () {
- this.firstDocUpdate = {
- type: 'add-doc',
- id: 1,
- docLines: 'a\nb',
- }
- this.secondDocUpdate = {
- type: 'add-doc',
- id: 2,
- docLines: 'a\nb',
- }
- this.firstFileUpdate = {
- type: 'add-file',
- id: 3,
- url: 'filestore.example.com/2',
- }
- this.secondFileUpdate = {
- type: 'add-file',
- id: 4,
- url: 'filestore.example.com/3',
- }
- this.updates = [
- this.firstDocUpdate,
- this.secondDocUpdate,
- this.firstFileUpdate,
- this.secondFileUpdate,
- ]
- })
- describe('successfully', function () {
- beforeEach(function () {
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should add the docs in the updates', function () {
- const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
- version: `${this.version}.0`,
- })
- 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,
- firstDocUpdateWithVersion,
- this.source
- )
- .should.equal(true)
- this.ProjectHistoryRedisManager.queueAddEntity
- .getCall(1)
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- 'doc',
- this.secondDocUpdate.id,
- this.user_id,
- secondDocUpdateWithVersion,
- this.source
- )
- .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,
- firstFileUpdateWithVersion,
- this.source
- )
- .should.equal(true)
- this.ProjectHistoryRedisManager.queueAddEntity
- .getCall(3)
- .calledWith(
- this.project_id,
- this.projectHistoryId,
- 'file',
- this.secondFileUpdate.id,
- this.user_id,
- secondFileUpdateWithVersion,
- this.source
- )
- .should.equal(true)
- })
- it('should not flush the history', function () {
- this.HistoryManager.flushProjectChangesAsync
- .calledWith(this.project_id)
- .should.equal(false)
- })
- it('should call the callback', function () {
- this.callback.called.should.equal(true)
- })
- })
- describe('when adding a doc fails', function () {
- beforeEach(function () {
- this.error = new Error('error')
- this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should call the callback with the error', function () {
- this.callback.calledWith(this.error).should.equal(true)
- })
- })
- describe('when adding a file fails', function () {
- beforeEach(function () {
- this.error = new Error('error')
- this.ProjectHistoryRedisManager.queueAddEntity.yields(this.error)
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should call the callback with the error', function () {
- this.callback.calledWith(this.error).should.equal(true)
- })
- })
- describe('with enough ops to flush', function () {
- beforeEach(function () {
- this.HistoryManager.shouldFlushHistoryOps.returns(true)
- this.ProjectManager.updateProjectWithLocks(
- this.project_id,
- this.projectHistoryId,
- this.user_id,
- this.updates,
- this.version,
- this.source,
- this.callback
- )
- })
- it('should flush the history', function () {
- this.HistoryManager.flushProjectChangesAsync
- .calledWith(this.project_id)
- .should.equal(true)
- })
- })
- })
- 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,
- this.source,
- this.callback
- )
- })
- it('should call back with an error', function () {
- this.callback.calledWith(sinon.match.instanceOf(Error)).should.be.true
- })
- })
- })
- })
|