overleaf/services/track-changes/test/acceptance/coffee/AppendingUpdatesTests.coffee

162 lines
4.5 KiB
CoffeeScript
Raw Normal View History

sinon = require "sinon"
chai = require("chai")
chai.should()
2014-02-26 07:11:45 -05:00
expect = chai.expect
mongojs = require "../../../app/js/mongojs"
db = mongojs.db
ObjectId = mongojs.ObjectId
Settings = require "settings-sharelatex"
request = require "request"
2014-02-26 07:11:45 -05:00
rclient = require("redis").createClient() # Only works locally for now
2014-02-26 07:38:47 -05:00
flushAndGetCompressedUpdates = (doc_id, callback = (error, updates) ->) ->
request.post {
url: "http://localhost:#{Settings.port}/doc/#{doc_id}/flush"
}, (error, response, body) =>
response.statusCode.should.equal 204
db.docHistory
.find(doc_id: ObjectId(doc_id))
.sort("meta.end_ts": 1)
.toArray callback
pushRawUpdates = (doc_id, updates, callback = (error) ->) ->
rclient.rpush "UncompressedHistoryOps:#{doc_id}", (JSON.stringify(u) for u in updates)..., callback
describe "Appending doc ops to the history", ->
describe "when the history does not exist yet", ->
before (done) ->
@doc_id = ObjectId().toString()
@user_id = ObjectId().toString()
2014-02-26 07:38:47 -05:00
pushRawUpdates @doc_id, [{
op: [{ i: "f", p: 3 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:11:45 -05:00
v: 3
}, {
op: [{ i: "o", p: 4 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:11:45 -05:00
v: 4
}, {
op: [{ i: "o", p: 5 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:11:45 -05:00
v: 5
2014-02-26 07:38:47 -05:00
}], (error) =>
throw error if error?
flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
throw error if error?
done()
2014-02-26 07:11:45 -05:00
it "should insert the compressed op into mongo", ->
2014-02-26 07:38:47 -05:00
expect(@updates[0].op).to.deep.equal {
2014-02-26 07:11:45 -05:00
p: 3, i: "foo"
}
2014-02-26 07:11:45 -05:00
it "should insert the correct version number into mongo", ->
2014-02-26 07:38:47 -05:00
expect(@updates[0].v).to.equal 5
2014-02-26 07:11:45 -05:00
describe "when the history has already been started", ->
beforeEach (done) ->
@doc_id = ObjectId().toString()
@user_id = ObjectId().toString()
2014-02-26 07:38:47 -05:00
pushRawUpdates @doc_id, [{
op: [{ i: "f", p: 3 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 3
}, {
op: [{ i: "o", p: 4 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 4
}, {
op: [{ i: "o", p: 5 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 5
}], (error) =>
throw error if error?
flushAndGetCompressedUpdates @doc_id, (error, updates) =>
throw error if error?
done()
describe "when the updates are recent and from the same user", ->
beforeEach (done) ->
2014-02-26 07:38:47 -05:00
pushRawUpdates @doc_id, [{
op: [{ i: "b", p: 6 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 6
}, {
op: [{ i: "a", p: 7 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 7
}, {
op: [{ i: "r", p: 8 }]
meta: { ts: Date.now(), user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 8
}], (error) =>
throw error if error?
flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
throw error if error?
done()
2014-02-26 07:38:47 -05:00
it "should combine all the updates into one", ->
expect(@updates[0].op).to.deep.equal {
p: 3, i: "foobar"
}
2014-02-26 07:38:47 -05:00
it "should insert the correct version number into mongo", ->
expect(@updates[0].v).to.equal 8
describe "when the updates are far apart", ->
beforeEach (done) ->
oneDay = 24 * 60 * 60 * 1000
2014-02-26 07:38:47 -05:00
pushRawUpdates @doc_id, [{
op: [{ i: "b", p: 6 }]
meta: { ts: Date.now() + oneDay, user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 6
}, {
op: [{ i: "a", p: 7 }]
meta: { ts: Date.now() + oneDay, user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 7
}, {
op: [{ i: "r", p: 8 }]
meta: { ts: Date.now() + oneDay, user_id: @user_id }
2014-02-26 07:38:47 -05:00
v: 8
}], (error) =>
throw error if error?
flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
throw error if error?
done()
2014-02-26 07:38:47 -05:00
it "should keep the updates separate", ->
expect(@updates[0].op).to.deep.equal {
p: 3, i: "foo"
}
expect(@updates[1].op).to.deep.equal {
p: 6, i: "bar"
}
describe "when the updates need processing in batches", ->
before (done) ->
@doc_id = ObjectId().toString()
@user_id = ObjectId().toString()
updates = []
@expectedOp = { p:0, i: "" }
for i in [0..250]
updates.push {
op: [{i: "a", p: 0}]
meta: { ts: Date.now(), user_id: @user_id }
v: i
}
@expectedOp.i = "a" + @expectedOp.i
pushRawUpdates @doc_id, updates, (error) =>
throw error if error?
flushAndGetCompressedUpdates @doc_id, (error, @updates) =>
throw error if error?
done()
it "should concat the compressed op into mongo", ->
expect(@updates[0].op).to.deep.equal @expectedOp
it "should insert the correct version number into mongo", ->
expect(@updates[0].v).to.equal 250