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

83 lines
2.5 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"
MockWebApi = require "./helpers/MockWebApi"
DocUpdaterClient = require "./helpers/DocUpdaterClient"
2018-02-15 11:28:40 -05:00
DocUpdaterApp = require "./helpers/DocUpdaterApp"
2014-02-12 05:40:42 -05:00
describe "Flushing a doc to Mongo", ->
2018-02-15 11:28:40 -05:00
before (done) ->
2014-02-12 05:40:42 -05:00
@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"]
2018-02-15 11:28:40 -05:00
DocUpdaterApp.ensureRunning(done)
2014-02-12 05:40:42 -05:00
describe "when the updated doc exists in the doc updater", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
sinon.spy MockWebApi, "setDocument"
2014-02-12 05:40:42 -05:00
MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
DocUpdaterClient.sendUpdates @project_id, @doc_id, [@update], (error) =>
2014-02-12 05:40:42 -05:00
throw error if error?
setTimeout () =>
DocUpdaterClient.flushDoc @project_id, @doc_id, done
, 200
2014-02-12 05:40:42 -05:00
after ->
MockWebApi.setDocument.restore()
2014-02-12 05:40:42 -05:00
it "should flush the updated doc lines and version to the web api", ->
MockWebApi.setDocument
.calledWith(@project_id, @doc_id, @result, @version + 1)
2014-02-12 05:40:42 -05:00
.should.equal true
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, "setDocument"
2014-02-12 05:40:42 -05:00
DocUpdaterClient.flushDoc @project_id, @doc_id, done
after ->
MockWebApi.setDocument.restore()
2014-02-12 05:40:42 -05:00
it "should not flush the doc to the web api", ->
MockWebApi.setDocument.called.should.equal false
2014-02-12 05:40:42 -05:00
describe "when the web api http request takes a long time on first request", ->
before (done) ->
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
MockWebApi.insertDoc @project_id, @doc_id, {
lines: @lines
version: @version
}
t = 30000
2017-05-11 07:00:34 -04:00
sinon.stub MockWebApi, "setDocument", (project_id, doc_id, lines, version, ranges, callback = (error) ->) ->
setTimeout callback, t
t = 0
DocUpdaterClient.preloadDoc @project_id, @doc_id, done
after ->
MockWebApi.setDocument.restore()
it "should still work", (done) ->
start = Date.now()
DocUpdaterClient.flushDoc @project_id, @doc_id, (error, res, doc) =>
res.statusCode.should.equal 204
delta = Date.now() - start
expect(delta).to.be.below 20000
done()