2014-02-12 05:40:42 -05:00
|
|
|
sinon = require "sinon"
|
|
|
|
chai = require("chai")
|
|
|
|
chai.should()
|
2016-04-12 12:10:39 -04:00
|
|
|
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"]
|
2014-02-10 10:17:08 -05:00
|
|
|
@version = 42
|
2014-02-12 05:40:42 -05:00
|
|
|
@update =
|
|
|
|
doc: @doc_id
|
2019-04-22 20:02:48 -04:00
|
|
|
meta: { user_id: 'last-author-fake-id' }
|
2014-02-12 05:40:42 -05:00
|
|
|
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"]
|
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()]
|
2016-11-29 12:06:23 -05:00
|
|
|
sinon.spy MockWebApi, "setDocument"
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-11-29 12:06:23 -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?
|
2016-11-29 12:06:23 -05:00
|
|
|
setTimeout () =>
|
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, done
|
|
|
|
, 200
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
after ->
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.setDocument.restore()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2016-11-29 12:06:23 -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
|
|
|
|
|
2019-04-22 20:02:48 -04:00
|
|
|
it "should flush the last update author and time to the web api", ->
|
|
|
|
lastUpdatedAt = MockWebApi.setDocument.lastCall.args[5]
|
|
|
|
parseInt(lastUpdatedAt).should.be.closeTo((new Date()).getTime(), 30000)
|
|
|
|
|
|
|
|
lastUpdatedBy = MockWebApi.setDocument.lastCall.args[6]
|
|
|
|
lastUpdatedBy.should.equal 'last-author-fake-id'
|
|
|
|
|
2014-02-12 05:40:42 -05:00
|
|
|
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
|
|
|
|
}
|
2016-11-29 12:06:23 -05:00
|
|
|
sinon.spy MockWebApi, "setDocument"
|
2014-02-12 05:40:42 -05:00
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, done
|
|
|
|
|
|
|
|
after ->
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.setDocument.restore()
|
2014-02-12 05:40:42 -05:00
|
|
|
|
|
|
|
it "should not flush the doc to the web api", ->
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.setDocument.called.should.equal false
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2017-07-17 06:13:21 -04:00
|
|
|
describe "when the web api http request takes a long time on first request", ->
|
2016-04-12 12:10:39 -04:00
|
|
|
before (done) ->
|
|
|
|
[@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
|
|
|
|
MockWebApi.insertDoc @project_id, @doc_id, {
|
|
|
|
lines: @lines
|
2016-11-29 12:06:23 -05:00
|
|
|
version: @version
|
2016-04-12 12:10:39 -04:00
|
|
|
}
|
2017-07-17 06:13:21 -04:00
|
|
|
t = 30000
|
2019-04-22 20:02:48 -04:00
|
|
|
sinon.stub MockWebApi, "setDocument", (project_id, doc_id, lines, version, ranges, lastUpdatedAt, lastUpdatedBy, callback = (error) ->) ->
|
2017-07-17 06:13:21 -04:00
|
|
|
setTimeout callback, t
|
|
|
|
t = 0
|
2016-11-29 12:06:23 -05:00
|
|
|
DocUpdaterClient.preloadDoc @project_id, @doc_id, done
|
2016-04-12 12:10:39 -04:00
|
|
|
|
|
|
|
after ->
|
2016-11-29 12:06:23 -05:00
|
|
|
MockWebApi.setDocument.restore()
|
2016-04-12 12:10:39 -04:00
|
|
|
|
2017-07-17 06:13:21 -04:00
|
|
|
it "should still work", (done) ->
|
2016-04-12 12:10:39 -04:00
|
|
|
start = Date.now()
|
|
|
|
DocUpdaterClient.flushDoc @project_id, @doc_id, (error, res, doc) =>
|
2017-07-17 06:13:21 -04:00
|
|
|
res.statusCode.should.equal 204
|
2016-04-12 12:10:39 -04:00
|
|
|
delta = Date.now() - start
|
|
|
|
expect(delta).to.be.below 20000
|
2017-07-17 06:13:21 -04:00
|
|
|
done()
|