2020-05-06 06:12:36 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:12:17 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:12:47 -04:00
|
|
|
const sinon = require('sinon')
|
2021-04-01 15:51:00 -04:00
|
|
|
const { expect } = require('chai')
|
2020-05-06 06:12:47 -04:00
|
|
|
const async = require('async')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
const MockWebApi = require('./helpers/MockWebApi')
|
|
|
|
const DocUpdaterClient = require('./helpers/DocUpdaterClient')
|
|
|
|
const DocUpdaterApp = require('./helpers/DocUpdaterApp')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('Flushing a doc to Mongo', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.lines = ['one', 'two', 'three']
|
|
|
|
this.version = 42
|
|
|
|
this.update = {
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: { user_id: 'last-author-fake-id' },
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
i: 'one and a half\n',
|
2021-07-13 07:04:42 -04:00
|
|
|
p: 4,
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
],
|
2021-07-13 07:04:42 -04:00
|
|
|
v: this.version,
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.result = ['one', 'one and a half', 'two', 'three']
|
|
|
|
return DocUpdaterApp.ensureRunning(done)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('when the updated doc exists in the doc updater', function () {
|
|
|
|
before(function (done) {
|
|
|
|
;[this.project_id, this.doc_id] = Array.from([
|
|
|
|
DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
DocUpdaterClient.randomId(),
|
2020-05-06 06:12:47 -04:00
|
|
|
])
|
|
|
|
sinon.spy(MockWebApi, 'setDocument')
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc_id, {
|
|
|
|
lines: this.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: this.version,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
return DocUpdaterClient.sendUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
[this.update],
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return setTimeout(() => {
|
|
|
|
return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, done)
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
after(function () {
|
|
|
|
return MockWebApi.setDocument.restore()
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
it('should flush the updated doc lines and version to the web api', function () {
|
|
|
|
return MockWebApi.setDocument
|
|
|
|
.calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
return it('should flush the last update author and time to the web api', function () {
|
|
|
|
const lastUpdatedAt = MockWebApi.setDocument.lastCall.args[5]
|
|
|
|
parseInt(lastUpdatedAt).should.be.closeTo(new Date().getTime(), 30000)
|
2019-04-22 20:02:48 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
const lastUpdatedBy = MockWebApi.setDocument.lastCall.args[6]
|
|
|
|
return lastUpdatedBy.should.equal('last-author-fake-id')
|
|
|
|
})
|
|
|
|
})
|
2019-04-22 20:02:48 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('when the doc does not exist in the doc updater', function () {
|
|
|
|
before(function (done) {
|
|
|
|
;[this.project_id, this.doc_id] = Array.from([
|
|
|
|
DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
DocUpdaterClient.randomId(),
|
2020-05-06 06:12:47 -04:00
|
|
|
])
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc_id, {
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: this.lines,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
sinon.spy(MockWebApi, 'setDocument')
|
|
|
|
return DocUpdaterClient.flushDoc(this.project_id, this.doc_id, done)
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
after(function () {
|
|
|
|
return MockWebApi.setDocument.restore()
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
return it('should not flush the doc to the web api', function () {
|
|
|
|
return MockWebApi.setDocument.called.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
return describe('when the web api http request takes a long time on first request', function () {
|
|
|
|
before(function (done) {
|
|
|
|
;[this.project_id, this.doc_id] = Array.from([
|
|
|
|
DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
DocUpdaterClient.randomId(),
|
2020-05-06 06:12:47 -04:00
|
|
|
])
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc_id, {
|
|
|
|
lines: this.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: this.version,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
let t = 30000
|
2020-05-15 14:29:49 -04:00
|
|
|
sinon
|
|
|
|
.stub(MockWebApi, 'setDocument')
|
|
|
|
.callsFake(
|
|
|
|
(
|
|
|
|
project_id,
|
|
|
|
doc_id,
|
|
|
|
lines,
|
|
|
|
version,
|
|
|
|
ranges,
|
|
|
|
lastUpdatedAt,
|
|
|
|
lastUpdatedBy,
|
|
|
|
callback
|
|
|
|
) => {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function (error) {}
|
|
|
|
}
|
|
|
|
setTimeout(callback, t)
|
|
|
|
return (t = 0)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2020-05-15 14:29:49 -04:00
|
|
|
)
|
2020-05-06 06:12:47 -04:00
|
|
|
return DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, done)
|
|
|
|
})
|
2016-04-12 12:10:39 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
after(function () {
|
|
|
|
return MockWebApi.setDocument.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should still work', function (done) {
|
|
|
|
const start = Date.now()
|
|
|
|
return DocUpdaterClient.flushDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, doc) => {
|
|
|
|
res.statusCode.should.equal(204)
|
|
|
|
const delta = Date.now() - start
|
|
|
|
expect(delta).to.be.below(20000)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|