overleaf/services/track-changes/test/unit/coffee/RedisManager/RedisManagerTests.coffee

88 lines
2.9 KiB
CoffeeScript
Raw Normal View History

sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/RedisManager.js"
SandboxedModule = require('sandboxed-module')
describe "RedisManager", ->
beforeEach ->
@RedisManager = SandboxedModule.require modulePath, requires:
2014-09-26 12:51:41 -04:00
"redis-sharelatex" :
createClient: () => @rclient =
auth: sinon.stub()
multi: () => @rclient
2014-09-26 12:51:41 -04:00
"settings-sharelatex":
redis:
history:
key_schema:
uncompressedHistoryOps: ({doc_id}) -> "UncompressedHistoryOps:#{doc_id}"
docsWithHistoryOps: ({project_id}) -> "DocsWithHistoryOps:#{project_id}"
@doc_id = "doc-id-123"
@project_id = "project-id-123"
@batchSize = 100
@callback = sinon.stub()
2015-10-08 09:40:42 -04:00
describe "getOldestDocUpdates", ->
beforeEach ->
@rawUpdates = [ {v: 42, op: "mock-op-42"}, { v: 45, op: "mock-op-45" }]
@jsonUpdates = (JSON.stringify(update) for update in @rawUpdates)
@rclient.lrange = sinon.stub().callsArgWith(3, null, @jsonUpdates)
2015-10-08 09:40:42 -04:00
@RedisManager.getOldestDocUpdates @doc_id, @batchSize, @callback
it "should read the updates from redis", ->
@rclient.lrange
.calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @batchSize - 1)
.should.equal true
2015-10-08 09:40:42 -04:00
it "should call the callback with the unparsed ops", ->
@callback.calledWith(null, @jsonUpdates).should.equal true
2015-10-08 09:40:42 -04:00
describe "expandDocUpdates", ->
beforeEach ->
@RedisManager.expandDocUpdates @jsonUpdates, @callback
it "should call the callback with the parsed ops", ->
@callback.calledWith(null, @rawUpdates).should.equal true
describe "deleteAppliedDocUpdates", ->
beforeEach ->
@rclient.lrem = sinon.stub()
@rclient.srem = sinon.stub()
@rclient.exec = sinon.stub().callsArgWith(0)
@RedisManager.deleteAppliedDocUpdates @project_id, @doc_id, @jsonUpdates, @callback
it "should delete the first update from redis", ->
@rclient.lrem
.calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @jsonUpdates[0])
.should.equal true
it "should delete the second update from redis", ->
@rclient.lrem
.calledWith("UncompressedHistoryOps:#{@doc_id}", 0, @jsonUpdates[1])
.should.equal true
it "should delete the doc from the set of docs with history ops", ->
@rclient.srem
.calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
.should.equal true
2015-10-08 09:40:42 -04:00
it "should call the callback ", ->
@callback.called.should.equal true
describe "getDocIdsWithHistoryOps", ->
beforeEach ->
@doc_ids = ["mock-id-1", "mock-id-2"]
@rclient.smembers = sinon.stub().callsArgWith(1, null, @doc_ids)
@RedisManager.getDocIdsWithHistoryOps @project_id, @callback
it "should read the doc_ids from redis", ->
@rclient.smembers
.calledWith("DocsWithHistoryOps:#{@project_id}")
.should.equal true
it "should call the callback with the doc_ids", ->
2015-10-08 09:40:42 -04:00
@callback.calledWith(null, @doc_ids).should.equal true