2014-02-12 05:40:42 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
|
|
|
async = require "async"
|
2014-02-26 10:56:52 -05:00
|
|
|
rclient = require("redis").createClient()
|
2014-05-14 08:28:17 -04:00
|
|
|
{db, ObjectId} = require "../../../app/js/mongojs"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2014-02-28 13:29:05 -05:00
|
|
|
MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
|
2014-02-12 05:40:42 -05:00
|
|
|
MockWebApi = require "./helpers/MockWebApi"
|
|
|
|
DocUpdaterClient = require "./helpers/DocUpdaterClient"
|
|
|
|
|
|
|
|
describe "Applying updates to a doc", ->
|
|
|
|
before ->
|
|
|
|
@lines = ["one", "two", "three"]
|
2014-02-10 10:17:08 -05:00
|
|
|
@version = 42
|
2014-02-12 05:40:42 -05:00
|
|
|
@update =
|
|
|
|
doc: @doc_id
|
|
|
|
op: [{
|
|
|
|
i: "one and a half\n"
|
|
|
|
p: 4
|
|
|
|
}]
|
2014-02-10 10:17:08 -05:00
|
|
|
v: @version
|
2014-02-12 05:40:42 -05:00
|
|
|
@result = ["one", "one and a half", "two", "three"]
|
|
|
|
|
|
|
|
describe "when the document is not loaded", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
sinon.spy MockWebApi, "getDocument"
|
2014-05-15 06:13:16 -04:00
|
|
|
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
|
|
|
|
db.docOps.insert {
|
|
|
|
doc_id: ObjectId(@doc_id)
|
|
|
|
version: @version
|
|
|
|
}, (error) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
throw error if error?
|
2014-05-15 06:13:16 -04:00
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
setTimeout done, 200
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.getDocument.restore()
|
|
|
|
|
|
|
|
it "should load the document from the web API", ->
|
|
|
|
MockWebApi.getDocument
|
|
|
|
.calledWith(@project_id, @doc_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should update the doc", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @result
|
|
|
|
done()
|
|
|
|
|
2014-02-26 10:56:52 -05:00
|
|
|
it "should push the applied updates to the track changes api", (done) ->
|
|
|
|
rclient.lrange "UncompressedHistoryOps:#{@doc_id}", 0, -1, (error, updates) =>
|
2014-03-21 08:41:05 -04:00
|
|
|
throw error if error?
|
2014-02-26 10:56:52 -05:00
|
|
|
JSON.parse(updates[0]).op.should.deep.equal @update.op
|
2014-03-21 08:41:05 -04:00
|
|
|
rclient.sismember "DocsWithHistoryOps:#{@project_id}", @doc_id, (error, result) =>
|
|
|
|
throw error if error?
|
|
|
|
result.should.equal 1
|
|
|
|
done()
|
|
|
|
|
2014-02-26 10:56:52 -05:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
describe "when the document is loaded", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2014-05-15 06:13:16 -04:00
|
|
|
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
|
|
|
|
db.docOps.insert doc_id: ObjectId(@doc_id), version: @version, (error) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
throw error if error?
|
2014-05-15 06:13:16 -04:00
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
throw error if error?
|
2014-05-15 06:13:16 -04:00
|
|
|
sinon.spy MockWebApi, "getDocument"
|
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
setTimeout done, 200
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
after ->
|
|
|
|
MockWebApi.getDocument.restore()
|
|
|
|
|
|
|
|
it "should not need to call the web api", ->
|
|
|
|
MockWebApi.getDocument.called.should.equal false
|
|
|
|
|
|
|
|
it "should update the doc", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @result
|
|
|
|
done()
|
|
|
|
|
2014-02-26 10:56:52 -05:00
|
|
|
it "should push the applied updates to the track changes api", (done) ->
|
|
|
|
rclient.lrange "UncompressedHistoryOps:#{@doc_id}", 0, -1, (error, updates) =>
|
|
|
|
JSON.parse(updates[0]).op.should.deep.equal @update.op
|
2014-03-21 08:41:05 -04:00
|
|
|
rclient.sismember "DocsWithHistoryOps:#{@project_id}", @doc_id, (error, result) =>
|
|
|
|
result.should.equal 1
|
|
|
|
done()
|
2014-02-26 10:56:52 -05:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
describe "when the document has been deleted", ->
|
|
|
|
describe "when the ops come in a single linear order", ->
|
2014-05-15 06:13:16 -04:00
|
|
|
before (done) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2014-05-14 08:28:17 -04:00
|
|
|
lines = ["", "", ""]
|
2014-05-15 06:13:16 -04:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: lines
|
|
|
|
db.docOps.insert doc_id: ObjectId(@doc_id), version: 0, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
@updates = [
|
|
|
|
{ doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
|
|
|
|
{ doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
|
|
|
|
{ doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
|
|
|
|
{ doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
|
|
|
|
{ doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
|
|
|
|
{ doc_id: @doc_id, v: 5, op: [i: " ", p: 5 ] }
|
|
|
|
{ doc_id: @doc_id, v: 6, op: [i: "w", p: 6 ] }
|
|
|
|
{ doc_id: @doc_id, v: 7, op: [i: "o", p: 7 ] }
|
|
|
|
{ doc_id: @doc_id, v: 8, op: [i: "r", p: 8 ] }
|
|
|
|
{ doc_id: @doc_id, v: 9, op: [i: "l", p: 9 ] }
|
|
|
|
{ doc_id: @doc_id, v: 10, op: [i: "d", p: 10] }
|
|
|
|
]
|
|
|
|
@my_result = ["hello world", "", ""]
|
|
|
|
done()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
it "should be able to continue applying updates when the project has been deleted", (done) ->
|
|
|
|
actions = []
|
|
|
|
for update in @updates.slice(0,6)
|
|
|
|
do (update) =>
|
|
|
|
actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
|
|
|
|
actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
|
|
|
|
for update in @updates.slice(6)
|
|
|
|
do (update) =>
|
|
|
|
actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
|
|
|
|
|
|
|
|
async.series actions, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
2014-05-14 08:28:17 -04:00
|
|
|
doc.lines.should.deep.equal @my_result
|
2014-02-12 05:40:42 -05:00
|
|
|
done()
|
|
|
|
|
2014-02-26 10:56:52 -05:00
|
|
|
it "should push the applied updates to the track changes api", (done) ->
|
|
|
|
rclient.lrange "UncompressedHistoryOps:#{@doc_id}", 0, -1, (error, updates) =>
|
|
|
|
updates = (JSON.parse(u) for u in updates)
|
|
|
|
for appliedUpdate, i in @updates
|
|
|
|
appliedUpdate.op.should.deep.equal updates[i].op
|
2014-03-21 08:41:05 -04:00
|
|
|
|
|
|
|
rclient.sismember "DocsWithHistoryOps:#{@project_id}", @doc_id, (error, result) =>
|
|
|
|
result.should.equal 1
|
|
|
|
done()
|
2016-08-23 11:00:46 -04:00
|
|
|
|
|
|
|
it "should store the doc ops in the correct order", (done) ->
|
|
|
|
rclient.lrange "DocOps:#{@doc_id}", 0, -1, (error, updates) =>
|
|
|
|
updates = (JSON.parse(u) for u in updates)
|
|
|
|
for appliedUpdate, i in @updates
|
|
|
|
appliedUpdate.op.should.deep.equal updates[i].op
|
|
|
|
done()
|
2014-02-26 10:56:52 -05:00
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
describe "when older ops come in after the delete", ->
|
2014-05-15 06:13:16 -04:00
|
|
|
before (done) ->
|
2014-02-12 05:40:42 -05:00
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2014-05-14 08:28:17 -04:00
|
|
|
lines = ["", "", ""]
|
2014-05-15 06:13:16 -04:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: lines
|
|
|
|
db.docOps.insert doc_id: ObjectId(@doc_id), version: 0, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
|
|
|
|
@updates = [
|
|
|
|
{ doc_id: @doc_id, v: 0, op: [i: "h", p: 0 ] }
|
|
|
|
{ doc_id: @doc_id, v: 1, op: [i: "e", p: 1 ] }
|
|
|
|
{ doc_id: @doc_id, v: 2, op: [i: "l", p: 2 ] }
|
|
|
|
{ doc_id: @doc_id, v: 3, op: [i: "l", p: 3 ] }
|
|
|
|
{ doc_id: @doc_id, v: 4, op: [i: "o", p: 4 ] }
|
|
|
|
{ doc_id: @doc_id, v: 0, op: [i: "world", p: 1 ] }
|
|
|
|
]
|
|
|
|
@my_result = ["hello", "world", ""]
|
|
|
|
|
|
|
|
done()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
it "should be able to continue applying updates when the project has been deleted", (done) ->
|
|
|
|
actions = []
|
|
|
|
for update in @updates.slice(0,5)
|
|
|
|
do (update) =>
|
|
|
|
actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
|
|
|
|
actions.push (callback) => DocUpdaterClient.deleteDoc @project_id, @doc_id, callback
|
|
|
|
for update in @updates.slice(5)
|
|
|
|
do (update) =>
|
|
|
|
actions.push (callback) => DocUpdaterClient.sendUpdate @project_id, @doc_id, update, callback
|
|
|
|
|
|
|
|
async.series actions, (error) =>
|
|
|
|
throw error if error?
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
2014-05-14 08:28:17 -04:00
|
|
|
doc.lines.should.deep.equal @my_result
|
2014-02-12 05:40:42 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "with a broken update", ->
|
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2014-05-15 06:13:16 -04:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
|
|
|
|
db.docOps.insert doc_id: ObjectId(@doc_id), version: @version, (error) =>
|
2014-02-12 05:40:42 -05:00
|
|
|
throw error if error?
|
2014-05-15 06:13:16 -04:00
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, @undefined, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
setTimeout done, 200
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
it "should not update the doc", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @lines
|
|
|
|
done()
|
2014-02-28 13:29:05 -05:00
|
|
|
|
|
|
|
describe "with enough updates to flush to the track changes api", ->
|
2014-05-14 08:28:17 -04:00
|
|
|
before (done) ->
|
2014-02-28 13:29:05 -05:00
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
2014-05-14 08:28:17 -04:00
|
|
|
updates = []
|
2014-02-28 13:29:05 -05:00
|
|
|
for v in [0..99] # Should flush after 50 ops
|
2014-05-14 08:28:17 -04:00
|
|
|
updates.push
|
2014-02-28 13:29:05 -05:00
|
|
|
doc_id: @doc_id,
|
|
|
|
op: [i: v.toString(), p: 0]
|
|
|
|
v: v
|
|
|
|
|
|
|
|
sinon.spy MockTrackChangesApi, "flushDoc"
|
|
|
|
|
2014-05-15 06:13:16 -04:00
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
|
|
|
|
db.docOps.insert doc_id: ObjectId(@doc_id), version: 0, (error) =>
|
2014-03-21 08:41:05 -04:00
|
|
|
throw error if error?
|
2016-08-23 11:00:46 -04:00
|
|
|
|
|
|
|
# Send updates in chunks to causes multiple flushes
|
|
|
|
actions = []
|
|
|
|
for i in [0..9]
|
|
|
|
do (i) =>
|
|
|
|
actions.push (cb) =>
|
|
|
|
DocUpdaterClient.sendUpdates @project_id, @doc_id, updates.slice(i*10, (i+1)*10), cb
|
|
|
|
async.series actions, (error) =>
|
2014-05-15 06:13:16 -04:00
|
|
|
throw error if error?
|
2016-07-08 11:05:36 -04:00
|
|
|
setTimeout done, 2000
|
2014-03-21 08:41:05 -04:00
|
|
|
|
2014-05-14 08:28:17 -04:00
|
|
|
after ->
|
2014-02-28 13:29:05 -05:00
|
|
|
MockTrackChangesApi.flushDoc.restore()
|
|
|
|
|
2014-03-21 08:41:05 -04:00
|
|
|
it "should flush the doc twice", ->
|
|
|
|
MockTrackChangesApi.flushDoc.calledTwice.should.equal true
|
2014-02-28 14:09:29 -05:00
|
|
|
|
2014-05-15 06:13:16 -04:00
|
|
|
describe "when there is no version in Mongo", ->
|
2014-05-14 08:28:17 -04:00
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {
|
|
|
|
lines: @lines
|
|
|
|
}
|
|
|
|
|
|
|
|
update =
|
|
|
|
doc: @doc_id
|
|
|
|
op: @update.op
|
|
|
|
v: 0
|
|
|
|
DocUpdaterClient.sendUpdate @project_id, @doc_id, update, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
setTimeout done, 200
|
|
|
|
|
|
|
|
it "should update the doc (using version = 0)", (done) ->
|
|
|
|
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
|
|
|
|
doc.lines.should.deep.equal @result
|
|
|
|
done()
|