overleaf/services/document-updater/test/unit/coffee/ShareJsUpdateManager/ShareJsUpdateManagerTests.js

132 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/ShareJsUpdateManager.js"
2014-02-12 05:40:42 -05:00
SandboxedModule = require('sandboxed-module')
2019-06-11 11:33:14 -04:00
crypto = require('crypto')
2014-02-12 05:40:42 -05:00
describe "ShareJsUpdateManager", ->
beforeEach ->
@project_id = "project-id-123"
@doc_id = "document-id-123"
@callback = sinon.stub()
@ShareJsUpdateManager = SandboxedModule.require modulePath,
requires:
"./sharejs/server/model":
class Model
constructor: (@db) ->
"./ShareJsDB" : @ShareJsDB = { mockDB: true }
2014-10-07 07:08:36 -04:00
"redis-sharelatex" : createClient: () => @rclient = auth:->
2014-02-12 05:40:42 -05:00
"logger-sharelatex": @logger = { log: sinon.stub() }
"./RealTimeRedisManager": @RealTimeRedisManager = {}
"./Metrics": @metrics = { inc: sinon.stub() }
2014-02-12 05:40:42 -05:00
globals:
clearTimeout: @clearTimeout = sinon.stub()
describe "applyUpdate", ->
2014-02-12 05:40:42 -05:00
beforeEach ->
2016-12-01 13:11:03 -05:00
@lines = ["one", "two"]
2014-02-12 05:40:42 -05:00
@version = 34
2016-12-01 13:11:03 -05:00
@updatedDocLines = ["onefoo", "two"]
2019-06-11 11:33:14 -04:00
content = @updatedDocLines.join("\n")
@hash = crypto.createHash('sha1').update("blob " + content.length + "\x00").update(content, 'utf8').digest('hex')
@update = {p: 4, t: "foo", v:@version, hash:@hash}
2014-02-12 05:40:42 -05:00
@model =
applyOp: sinon.stub().callsArg(2)
getSnapshot: sinon.stub()
db:
appliedOps: {}
2014-02-12 05:40:42 -05:00
@ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
@ShareJsUpdateManager._listenForOps = sinon.stub()
@ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
describe "successfully", ->
beforeEach (done) ->
@model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version})
@model.db.appliedOps["#{@project_id}:#{@doc_id}"] = @appliedOps = ["mock-ops"]
2016-12-01 13:11:03 -05:00
@ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version, appliedOps) =>
@callback(err, docLines, version, appliedOps)
done()
it "should create a new ShareJs model", ->
@ShareJsUpdateManager.getNewShareJsModel
2016-12-01 13:11:03 -05:00
.calledWith(@project_id, @doc_id, @lines, @version)
.should.equal true
it "should listen for ops on the model", ->
@ShareJsUpdateManager._listenForOps
.calledWith(@model)
.should.equal true
it "should send the update to ShareJs", ->
@model.applyOp
.calledWith("#{@project_id}:#{@doc_id}", @update)
.should.equal true
it "should get the updated doc lines", ->
@model.getSnapshot
.calledWith("#{@project_id}:#{@doc_id}")
.should.equal true
it "should return the updated doc lines, version and ops", ->
@callback.calledWith(null, @updatedDocLines, @version, @appliedOps).should.equal true
describe "when applyOp fails", ->
beforeEach (done) ->
@error = new Error("Something went wrong")
@model.applyOp = sinon.stub().callsArgWith(2, @error)
2016-12-01 13:11:03 -05:00
@ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
@callback(err, docLines, version)
done()
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
describe "when getSnapshot fails", ->
beforeEach (done) ->
@error = new Error("Something went wrong")
@model.getSnapshot.callsArgWith(1, @error)
2016-12-01 13:11:03 -05:00
@ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
@callback(err, docLines, version)
done()
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
2014-02-12 05:40:42 -05:00
2019-06-11 11:33:14 -04:00
describe "with an invalid hash", ->
beforeEach (done) ->
@error = new Error("invalid hash")
@model.getSnapshot.callsArgWith(1, null, {snapshot: "unexpected content", v: @version})
@model.db.appliedOps["#{@project_id}:#{@doc_id}"] = @appliedOps = ["mock-ops"]
@ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version, appliedOps) =>
@callback(err, docLines, version, appliedOps)
done()
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true
2014-02-12 05:40:42 -05:00
describe "_listenForOps", ->
beforeEach ->
@model = on: (event, callback) =>
@callback = callback
sinon.spy @model, "on"
@ShareJsUpdateManager._listenForOps(@model)
it "should listen to the model for updates", ->
@model.on.calledWith("applyOp")
.should.equal true
describe "the callback", ->
beforeEach ->
@opData =
op: {t: "foo", p: 1}
meta: source: "bar"
@RealTimeRedisManager.sendData = sinon.stub()
2014-02-12 05:40:42 -05:00
@callback("#{@project_id}:#{@doc_id}", @opData)
it "should publish the op to redis", ->
@RealTimeRedisManager.sendData
2016-12-01 13:11:03 -05:00
.calledWith({project_id: @project_id, doc_id: @doc_id, op: @opData})
2014-02-12 05:40:42 -05:00
.should.equal true