2014-02-26 10:56:52 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/TrackChangesManager'
|
|
|
|
|
|
|
|
describe "TrackChangesManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@TrackChangesManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"request": @request = {}
|
2016-06-17 07:17:22 -04:00
|
|
|
"settings-sharelatex": @Settings = {}
|
2014-03-04 07:39:02 -05:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
2016-06-17 07:17:22 -04:00
|
|
|
"./WebRedisManager": @WebRedisManager = {}
|
2014-03-19 11:56:44 -04:00
|
|
|
@project_id = "mock-project-id"
|
2014-02-26 10:56:52 -05:00
|
|
|
@doc_id = "mock-doc-id"
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "flushDocChanges", ->
|
|
|
|
beforeEach ->
|
|
|
|
@Settings.apis =
|
|
|
|
trackchanges: url: "http://trackchanges.example.com"
|
|
|
|
|
|
|
|
describe "successfully", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
|
2014-03-19 11:56:44 -04:00
|
|
|
@TrackChangesManager.flushDocChanges @project_id, @doc_id, @callback
|
2014-02-26 10:56:52 -05:00
|
|
|
|
|
|
|
it "should send a request to the track changes api", ->
|
|
|
|
@request.post
|
2014-03-19 11:56:44 -04:00
|
|
|
.calledWith("#{@Settings.apis.trackchanges.url}/project/#{@project_id}/doc/#{@doc_id}/flush")
|
2014-02-26 10:56:52 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the callback", ->
|
|
|
|
@callback.calledWith(null).should.equal true
|
|
|
|
|
|
|
|
describe "when the track changes api returns an error", ->
|
|
|
|
beforeEach ->
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
|
2014-03-19 11:56:44 -04:00
|
|
|
@TrackChangesManager.flushDocChanges @project_id, @doc_id, @callback
|
2014-02-26 10:56:52 -05:00
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
|
|
|
@callback.calledWith(new Error("track changes api return non-success code: 500")).should.equal true
|
2014-02-28 13:29:05 -05:00
|
|
|
|
2016-08-23 11:00:46 -04:00
|
|
|
describe "pushUncompressedHistoryOps", ->
|
2014-02-28 13:29:05 -05:00
|
|
|
beforeEach ->
|
2016-08-23 11:00:46 -04:00
|
|
|
@ops = ["mock-ops"]
|
2014-03-19 11:56:44 -04:00
|
|
|
@TrackChangesManager.flushDocChanges = sinon.stub().callsArg(2)
|
2014-02-28 13:29:05 -05:00
|
|
|
|
2014-03-21 08:41:05 -04:00
|
|
|
describe "pushing the op", ->
|
2014-02-28 13:29:05 -05:00
|
|
|
beforeEach ->
|
2016-08-23 11:00:46 -04:00
|
|
|
@WebRedisManager.pushUncompressedHistoryOps = sinon.stub().callsArgWith(3, null, 1)
|
|
|
|
@TrackChangesManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
|
2014-02-28 13:29:05 -05:00
|
|
|
|
2016-08-23 11:00:46 -04:00
|
|
|
it "should push the ops into redis", ->
|
|
|
|
@WebRedisManager.pushUncompressedHistoryOps
|
|
|
|
.calledWith(@project_id, @doc_id, @ops)
|
2014-03-21 08:41:05 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
2014-02-28 13:29:05 -05:00
|
|
|
|
|
|
|
it "should not try to flush the op", ->
|
|
|
|
@TrackChangesManager.flushDocChanges.called.should.equal false
|
|
|
|
|
2016-08-23 11:00:46 -04:00
|
|
|
describe "when we hit a multiple of FLUSH_EVERY_N_OPS ops", ->
|
2014-03-21 08:41:05 -04:00
|
|
|
beforeEach ->
|
2016-08-23 11:00:46 -04:00
|
|
|
@WebRedisManager.pushUncompressedHistoryOps =
|
2016-06-17 07:17:22 -04:00
|
|
|
sinon.stub().callsArgWith(3, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
|
2016-08-23 11:00:46 -04:00
|
|
|
@TrackChangesManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
|
|
|
|
|
|
|
|
it "should tell the track changes api to flush", ->
|
|
|
|
@TrackChangesManager.flushDocChanges
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when we go over a multiple of FLUSH_EVERY_N_OPS ops", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ops = ["op1", "op2", "op3"]
|
|
|
|
@WebRedisManager.pushUncompressedHistoryOps =
|
|
|
|
sinon.stub().callsArgWith(3, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS + 1)
|
|
|
|
@TrackChangesManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
|
2014-03-21 08:41:05 -04:00
|
|
|
|
|
|
|
it "should tell the track changes api to flush", ->
|
|
|
|
@TrackChangesManager.flushDocChanges
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when TrackChangesManager errors", ->
|
|
|
|
beforeEach ->
|
2016-08-23 11:00:46 -04:00
|
|
|
@WebRedisManager.pushUncompressedHistoryOps =
|
2016-06-17 07:17:22 -04:00
|
|
|
sinon.stub().callsArgWith(3, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
|
2014-03-21 08:41:05 -04:00
|
|
|
@TrackChangesManager.flushDocChanges = sinon.stub().callsArgWith(2, @error = new Error("oops"))
|
2016-08-23 11:00:46 -04:00
|
|
|
@TrackChangesManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, @callback
|
2014-03-21 08:41:05 -04:00
|
|
|
|
|
|
|
it "should log out the error", ->
|
|
|
|
@logger.error
|
|
|
|
.calledWith(
|
|
|
|
err: @error
|
|
|
|
doc_id: @doc_id
|
|
|
|
project_id: @project_id
|
|
|
|
"error flushing doc to track changes api"
|
|
|
|
)
|
|
|
|
.should.equal true
|
2016-09-08 06:41:59 -04:00
|
|
|
|
|
|
|
describe "with no ops", ->
|
|
|
|
beforeEach ->
|
|
|
|
@WebRedisManager.pushUncompressedHistoryOps = sinon.stub().callsArgWith(3, null, 1)
|
|
|
|
@TrackChangesManager.pushUncompressedHistoryOps @project_id, @doc_id, [], @callback
|
|
|
|
|
|
|
|
it "should not call WebRedisManager.pushUncompressedHistoryOps", ->
|
|
|
|
@WebRedisManager.pushUncompressedHistoryOps.called.should.equal false
|
|
|
|
|
2014-02-28 13:29:05 -05:00
|
|
|
|