overleaf/services/document-updater/test/acceptance/coffee/FlushingDocsTests.coffee
2014-02-28 18:29:05 +00:00

97 lines
2.7 KiB
CoffeeScript

sinon = require "sinon"
chai = require("chai")
chai.should()
async = require "async"
MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient"
mongojs = require "../../../app/js/mongojs"
db = mongojs.db
ObjectId = mongojs.ObjectId
describe "Flushing a doc to Mongo", ->
before ->
@lines = ["one", "two", "three"]
@update =
doc: @doc_id
op: [{
i: "one and a half\n"
p: 4
}]
v: 0
@result = ["one", "one and a half", "two", "three"]
MockWebApi.insertDoc @project_id, @doc_id, {
lines: @lines
}
describe "when the updated doc exists in the doc updater", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {
lines: @lines
}
sinon.spy MockWebApi, "setDocumentLines"
DocUpdaterClient.sendUpdates @project_id, @doc_id, [@update], (error) =>
throw error if error?
setTimeout () =>
DocUpdaterClient.flushDoc @project_id, @doc_id, done
, 200
after ->
MockWebApi.setDocumentLines.restore()
it "should flush the updated document to the web api", ->
MockWebApi.setDocumentLines
.calledWith(@project_id, @doc_id, @result)
.should.equal true
it "should flush the doc ops to Mongo", (done) ->
db.docOps.find doc_id: ObjectId(@doc_id), (error, docs) =>
doc = docs[0]
doc.docOps[0].op.should.deep.equal @update.op
done()
describe "when the doc has a large number of ops to be flushed", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {
lines: @lines
}
@updates = []
for v in [0..999]
@updates.push
doc_id: @doc_id,
op: [i: v.toString(), p: 0]
v: v
DocUpdaterClient.sendUpdates @project_id, @doc_id, @updates, (error) =>
throw error if error?
setTimeout () =>
DocUpdaterClient.flushDoc @project_id, @doc_id, done
, 200
it "should flush the doc ops to Mongo in order", (done) ->
db.docOps.find doc_id: ObjectId(@doc_id), (error, docs) =>
doc = docs[0]
updates = @updates.slice(-100)
for update, i in doc.docOps
update.op.should.deep.equal updates[i].op
done()
describe "when the doc does not exist in the doc updater", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {
lines: @lines
}
sinon.spy MockWebApi, "setDocumentLines"
DocUpdaterClient.flushDoc @project_id, @doc_id, done
after ->
MockWebApi.setDocumentLines.restore()
it "should not flush the doc to the web api", ->
MockWebApi.setDocumentLines.called.should.equal false