|
|
@@ -4,6 +4,7 @@ should = chai.should()
|
|
|
modulePath = "../../../../app/js/DocumentManager.js"
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
Errors = require "../../../../app/js/Errors"
|
|
|
+tk = require "timekeeper"
|
|
|
|
|
|
describe "DocumentManager", ->
|
|
|
beforeEach ->
|
|
|
@@ -26,6 +27,7 @@ describe "DocumentManager", ->
|
|
|
@lines = ["one", "two", "three"]
|
|
|
@version = 42
|
|
|
@ranges = { comments: "mock", entries: "mock" }
|
|
|
+ @unflushedTime = Date.now()
|
|
|
|
|
|
describe "flushAndDeleteDoc", ->
|
|
|
describe "successfully", ->
|
|
|
@@ -60,6 +62,7 @@ describe "DocumentManager", ->
|
|
|
describe "when the doc is in Redis", ->
|
|
|
beforeEach ->
|
|
|
@RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges)
|
|
|
+ @RedisManager.clearUnflushedTime = sinon.stub().callsArgWith(1, null)
|
|
|
@PersistenceManager.setDoc = sinon.stub().yields()
|
|
|
@DocumentManager.flushDocIfLoaded @project_id, @doc_id, @callback
|
|
|
|
|
|
@@ -147,7 +150,7 @@ describe "DocumentManager", ->
|
|
|
describe "getDoc", ->
|
|
|
describe "when the doc exists in Redis", ->
|
|
|
beforeEach ->
|
|
|
- @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges)
|
|
|
+ @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, @unflushedTime)
|
|
|
@DocumentManager.getDoc @project_id, @doc_id, @callback
|
|
|
|
|
|
it "should get the doc from Redis", ->
|
|
|
@@ -156,7 +159,7 @@ describe "DocumentManager", ->
|
|
|
.should.equal true
|
|
|
|
|
|
it "should call the callback with the doc info", ->
|
|
|
- @callback.calledWith(null, @lines, @version, @ranges, true).should.equal true
|
|
|
+ @callback.calledWith(null, @lines, @version, @ranges, @unflushedTime, true).should.equal true
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
@@ -184,7 +187,7 @@ describe "DocumentManager", ->
|
|
|
.should.equal true
|
|
|
|
|
|
it "should call the callback with the doc info", ->
|
|
|
- @callback.calledWith(null, @lines, @version, @ranges, false).should.equal true
|
|
|
+ @callback.calledWith(null, @lines, @version, @ranges, null, false).should.equal true
|
|
|
|
|
|
it "should time the execution", ->
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
@@ -195,7 +198,7 @@ describe "DocumentManager", ->
|
|
|
@beforeLines = ["before", "lines"]
|
|
|
@afterLines = ["after", "lines"]
|
|
|
@ops = [{ i: "foo", p: 4 }, { d: "bar", p: 42 }]
|
|
|
- @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, @ranges, true)
|
|
|
+ @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, @ranges, @unflushedTime, true)
|
|
|
@DiffCodec.diffAsShareJsOp = sinon.stub().callsArgWith(2, null, @ops)
|
|
|
@UpdateManager.applyUpdate = sinon.stub().callsArgWith(3, null)
|
|
|
@DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
|
|
|
@@ -246,7 +249,7 @@ describe "DocumentManager", ->
|
|
|
|
|
|
describe "when not already loaded", ->
|
|
|
beforeEach ->
|
|
|
- @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, false)
|
|
|
+ @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @beforeLines, @version, null, false)
|
|
|
@DocumentManager.setDoc @project_id, @doc_id, @afterLines, @source, @user_id, false, @callback
|
|
|
|
|
|
it "should flush and delete the doc from the doc updater", ->
|
|
|
@@ -373,4 +376,65 @@ describe "DocumentManager", ->
|
|
|
|
|
|
it "should call the callback with a not found error", ->
|
|
|
error = new Errors.NotFoundError("document not found: #{@doc_id}")
|
|
|
- @callback.calledWith(error).should.equal true
|
|
|
+ @callback.calledWith(error).should.equal true
|
|
|
+
|
|
|
+ describe "getDocAndFlushIfOld", ->
|
|
|
+ beforeEach ->
|
|
|
+ tk.freeze(new Date())
|
|
|
+ @DocumentManager.flushDocIfLoaded = sinon.stub().callsArg(2)
|
|
|
+
|
|
|
+ afterEach ->
|
|
|
+ tk.reset()
|
|
|
+
|
|
|
+ describe "when the doc is in Redis", ->
|
|
|
+ describe "and has changes to be flushed", ->
|
|
|
+ beforeEach ->
|
|
|
+ @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, Date.now() - 1e9, true)
|
|
|
+ @DocumentManager.getDocAndFlushIfOld @project_id, @doc_id, @callback
|
|
|
+
|
|
|
+ it "should get the doc", ->
|
|
|
+ @DocumentManager.getDoc
|
|
|
+ .calledWith(@project_id, @doc_id)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should flush the doc", ->
|
|
|
+ @DocumentManager.flushDocIfLoaded
|
|
|
+ .calledWith(@project_id, @doc_id)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should call the callback with the lines and versions", ->
|
|
|
+ @callback.calledWith(null, @lines, @version).should.equal true
|
|
|
+
|
|
|
+ describe "and has only changes that don't need to be flushed", ->
|
|
|
+ beforeEach ->
|
|
|
+ @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, Date.now() - 100, true)
|
|
|
+ @DocumentManager.getDocAndFlushIfOld @project_id, @doc_id, @callback
|
|
|
+
|
|
|
+ it "should get the doc", ->
|
|
|
+ @DocumentManager.getDoc
|
|
|
+ .calledWith(@project_id, @doc_id)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should not flush the doc", ->
|
|
|
+ @DocumentManager.flushDocIfLoaded
|
|
|
+ .called.should.equal false
|
|
|
+
|
|
|
+ it "should call the callback with the lines and versions", ->
|
|
|
+ @callback.calledWith(null, @lines, @version).should.equal true
|
|
|
+
|
|
|
+ describe "when the doc is not in Redis", ->
|
|
|
+ beforeEach ->
|
|
|
+ @DocumentManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, null, false)
|
|
|
+ @DocumentManager.getDocAndFlushIfOld @project_id, @doc_id, @callback
|
|
|
+
|
|
|
+ it "should get the doc", ->
|
|
|
+ @DocumentManager.getDoc
|
|
|
+ .calledWith(@project_id, @doc_id)
|
|
|
+ .should.equal true
|
|
|
+
|
|
|
+ it "should not flush the doc", ->
|
|
|
+ @DocumentManager.flushDocIfLoaded
|
|
|
+ .called.should.equal false
|
|
|
+
|
|
|
+ it "should call the callback with the lines and versions", ->
|
|
|
+ @callback.calledWith(null, @lines, @version).should.equal true
|