2014-01-27 11:26:58 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/HistoryManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "HistoryManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@HistoryManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"./UpdateCompressor": @UpdateCompressor = {}
|
2014-02-24 12:43:27 -05:00
|
|
|
"./MongoManager" : @MongoManager = {}
|
2014-01-27 11:26:58 -05:00
|
|
|
"logger-sharelatex": { log: sinon.stub() }
|
|
|
|
@doc_id = "doc-id-123"
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "when there are no raw ops", ->
|
|
|
|
beforeEach ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.popLastCompressedUpdate = sinon.stub()
|
|
|
|
@MongoManager.insertCompressedUpdates = sinon.stub()
|
2014-01-27 11:26:58 -05:00
|
|
|
@HistoryManager.compressAndSaveRawUpdates @doc_id, [], @callback
|
|
|
|
|
|
|
|
it "should not need to access the database", ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.popLastCompressedUpdate.called.should.equal false
|
|
|
|
@MongoManager.insertCompressedUpdates.called.should.equal false
|
2014-01-27 11:26:58 -05:00
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "when there is no compressed history to begin with", ->
|
|
|
|
beforeEach ->
|
|
|
|
@rawUpdates = ["mock-raw-op-1", "mock-raw-op-2"]
|
|
|
|
@compressedUpdates = ["mock-compressed-op"]
|
|
|
|
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.popLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
|
|
|
|
@MongoManager.insertCompressedUpdates = sinon.stub().callsArg(2)
|
2014-01-27 11:26:58 -05:00
|
|
|
@UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
|
|
|
|
@HistoryManager.compressAndSaveRawUpdates @doc_id, @rawUpdates, @callback
|
|
|
|
|
|
|
|
it "should try to pop the last compressed op", ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.popLastCompressedUpdate
|
2014-01-27 11:26:58 -05:00
|
|
|
.calledWith(@doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should compress the raw ops", ->
|
|
|
|
@UpdateCompressor.compressRawUpdates
|
|
|
|
.calledWith(null, @rawUpdates)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should save the compressed ops", ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.insertCompressedUpdates
|
2014-01-27 11:26:58 -05:00
|
|
|
.calledWith(@doc_id, @compressedUpdates)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "when the raw ops need appending to existing history", ->
|
|
|
|
beforeEach ->
|
|
|
|
@rawUpdates = ["mock-raw-op-1", "mock-raw-op-2"]
|
|
|
|
@lastCompressedUpdate = "mock-last-compressed-op-0"
|
|
|
|
@compressedUpdates = ["mock-compressed-op-1"]
|
|
|
|
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.popLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate)
|
|
|
|
@MongoManager.insertCompressedUpdates = sinon.stub().callsArg(2)
|
2014-01-27 11:26:58 -05:00
|
|
|
@UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
|
|
|
|
@HistoryManager.compressAndSaveRawUpdates @doc_id, @rawUpdates, @callback
|
|
|
|
|
|
|
|
it "should try to pop the last compressed op", ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.popLastCompressedUpdate
|
2014-01-27 11:26:58 -05:00
|
|
|
.calledWith(@doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should compress the last compressed op and the raw ops", ->
|
|
|
|
@UpdateCompressor.compressRawUpdates
|
|
|
|
.calledWith(@lastCompressedUpdate, @rawUpdates)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should save the compressed ops", ->
|
2014-02-24 12:43:27 -05:00
|
|
|
@MongoManager.insertCompressedUpdates
|
2014-01-27 11:26:58 -05:00
|
|
|
.calledWith(@doc_id, @compressedUpdates)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
|