overleaf/services/document-updater/test/acceptance/coffee/ApplyingUpdatesToADocTests.coffee

303 lines
11 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:40:42 -05:00
sinon = require "sinon"
chai = require("chai")
chai.should()
expect = chai.expect
2014-02-12 05:40:42 -05:00
async = require "async"
Settings = require('settings-sharelatex')
rclient_history = require("redis-sharelatex").createClient(Settings.redis.history)
rclient_du = require("redis-sharelatex").createClient(Settings.redis.documentupdater)
Keys = Settings.redis.documentupdater.key_schema
HistoryKeys = Settings.redis.history.key_schema
2014-02-12 05:40:42 -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"]
@version = 42
2014-02-12 05:40:42 -05:00
@update =
doc: @doc_id
op: [{
i: "one and a half\n"
p: 4
}]
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"
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
2014-02-12 05:40:42 -05:00
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()
it "should push the applied updates to the track changes api", (done) ->
rclient_history.lrange HistoryKeys.uncompressedHistoryOps({@doc_id}), 0, -1, (error, updates) =>
throw error if error?
JSON.parse(updates[0]).op.should.deep.equal @update.op
rclient_history.sismember HistoryKeys.docsWithHistoryOps({@project_id}), @doc_id, (error, result) =>
throw error if error?
result.should.equal 1
done()
2014-02-12 05:40:42 -05:00
describe "when the document is loaded", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
2014-02-12 05:40:42 -05:00
throw error if error?
sinon.spy MockWebApi, "getDocument"
DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) ->
2014-02-12 05:40:42 -05:00
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()
it "should push the applied updates to the track changes api", (done) ->
rclient_history.lrange HistoryKeys.uncompressedHistoryOps({@doc_id}), 0, -1, (error, updates) =>
JSON.parse(updates[0]).op.should.deep.equal @update.op
rclient_history.sismember HistoryKeys.docsWithHistoryOps({@project_id}), @doc_id, (error, result) =>
result.should.equal 1
done()
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", ->
before (done) ->
2014-02-12 05:40:42 -05:00
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
lines = ["", "", ""]
MockWebApi.insertDoc @project_id, @doc_id, {lines: lines, version: 0}
@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) =>
doc.lines.should.deep.equal @my_result
2014-02-12 05:40:42 -05:00
done()
it "should push the applied updates to the track changes api", (done) ->
rclient_history.lrange HistoryKeys.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
rclient_history.sismember HistoryKeys.docsWithHistoryOps({@project_id}), @doc_id, (error, result) =>
result.should.equal 1
done()
it "should store the doc ops in the correct order", (done) ->
rclient_du.lrange Keys.docOps({doc_id: @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-12 05:40:42 -05:00
describe "when older ops come in after the delete", ->
before (done) ->
2014-02-12 05:40:42 -05:00
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
lines = ["", "", ""]
MockWebApi.insertDoc @project_id, @doc_id, {lines: lines, version: 0}
@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) =>
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()]
@broken_update = { doc_id: @doc_id, v: @version, op: [d: "not the correct content", p: 0 ] }
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
DocUpdaterClient.subscribeToAppliedOps @messageCallback = sinon.stub()
DocUpdaterClient.sendUpdate @project_id, @doc_id, @broken_update, (error) ->
2014-02-12 05:40:42 -05:00
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()
it "should send a message with an error", ->
@messageCallback.called.should.equal true
[channel, message] = @messageCallback.args[0]
channel.should.equal "applied-ops"
JSON.parse(message).should.deep.equal {
project_id: @project_id,
doc_id: @doc_id,
error:'Delete component \'not the correct content\' does not match deleted text \'one\ntwo\nthree\''
}
describe "with enough updates to flush to the track changes api", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
updates = []
2017-05-09 05:44:26 -04:00
for v in [0..199] # Should flush after 100 ops
updates.push
doc_id: @doc_id,
op: [i: v.toString(), p: 0]
v: v
sinon.spy MockTrackChangesApi, "flushDoc"
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: 0}
# Send updates in chunks to causes multiple flushes
actions = []
2017-05-09 05:44:26 -04:00
for i in [0..19]
do (i) =>
actions.push (cb) =>
DocUpdaterClient.sendUpdates @project_id, @doc_id, updates.slice(i*10, (i+1)*10), cb
async.series actions, (error) =>
throw error if error?
setTimeout done, 2000
after ->
MockTrackChangesApi.flushDoc.restore()
it "should flush the doc twice", ->
MockTrackChangesApi.flushDoc.calledTwice.should.equal true
describe "when there is no version in Mongo", ->
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()
describe "when the sending duplicate ops", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
DocUpdaterClient.subscribeToAppliedOps @messageCallback = sinon.stub()
# One user delete 'one', the next turns it into 'once'. The second becomes a NOP.
DocUpdaterClient.sendUpdate @project_id, @doc_id, {
doc: @doc_id
op: [{
i: "one and a half\n"
p: 4
}]
v: @version
meta:
source: "ikHceq3yfAdQYzBo4-xZ"
}, (error) =>
throw error if error?
setTimeout () =>
DocUpdaterClient.sendUpdate @project_id, @doc_id, {
doc: @doc_id
op: [{
i: "one and a half\n"
p: 4
}]
v: @version
dupIfSource: ["ikHceq3yfAdQYzBo4-xZ"]
meta:
source: "ikHceq3yfAdQYzBo4-xZ"
}, (error) =>
throw error if error?
setTimeout done, 200
, 200
it "should update the doc", (done) ->
DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
doc.lines.should.deep.equal @result
done()
it "should return a message about duplicate ops", ->
@messageCallback.calledTwice.should.equal true
@messageCallback.args[0][0].should.equal "applied-ops"
expect(JSON.parse(@messageCallback.args[0][1]).op.dup).to.be.undefined
@messageCallback.args[1][0].should.equal "applied-ops"
expect(JSON.parse(@messageCallback.args[1][1]).op.dup).to.equal true