2020-05-06 06:12:36 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
|
|
|
// 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:17 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
const MockWebApi = require('./helpers/MockWebApi')
|
|
|
|
const DocUpdaterClient = require('./helpers/DocUpdaterClient')
|
|
|
|
const DocUpdaterApp = require('./helpers/DocUpdaterApp')
|
|
|
|
|
|
|
|
describe('Getting a document', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.lines = ['one', 'two', 'three']
|
|
|
|
this.version = 42
|
|
|
|
return DocUpdaterApp.ensureRunning(done)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the document is not loaded', 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, 'getDocument')
|
|
|
|
|
|
|
|
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.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, returnedDoc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
this.returnedDoc = returnedDoc
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
return MockWebApi.getDocument.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should load the document from the web API', function () {
|
|
|
|
return MockWebApi.getDocument
|
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the document lines', function () {
|
|
|
|
return this.returnedDoc.lines.should.deep.equal(this.lines)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the document at its current version', function () {
|
|
|
|
return this.returnedDoc.version.should.equal(this.version)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the document is already loaded', 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
|
|
|
})
|
|
|
|
return DocUpdaterClient.preloadDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
sinon.spy(MockWebApi, 'getDocument')
|
|
|
|
return DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, returnedDoc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
this.returnedDoc = returnedDoc
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
return MockWebApi.getDocument.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not load the document from the web API', function () {
|
|
|
|
return MockWebApi.getDocument.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the document lines', function () {
|
|
|
|
return this.returnedDoc.lines.should.deep.equal(this.lines)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the request asks for some recent ops', 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 = ['one', 'two', 'three']),
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
2021-07-13 07:04:42 -04:00
|
|
|
this.updates = __range__(0, 199, true).map(v => ({
|
2020-05-06 06:12:47 -04:00
|
|
|
doc_id: this.doc_id,
|
|
|
|
op: [{ i: v.toString(), p: 0 }],
|
2021-07-13 07:04:42 -04:00
|
|
|
v,
|
2020-05-06 06:12:47 -04:00
|
|
|
}))
|
|
|
|
|
|
|
|
return DocUpdaterClient.sendUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.updates,
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
sinon.spy(MockWebApi, 'getDocument')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
return MockWebApi.getDocument.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the ops are loaded', function () {
|
|
|
|
before(function (done) {
|
|
|
|
return DocUpdaterClient.getDocAndRecentOps(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
190,
|
|
|
|
(error, res, returnedDoc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
this.returnedDoc = returnedDoc
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the recent ops', function () {
|
|
|
|
this.returnedDoc.ops.length.should.equal(10)
|
|
|
|
return Array.from(this.updates.slice(190, -1)).map((update, i) =>
|
|
|
|
this.returnedDoc.ops[i].op.should.deep.equal(update.op)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when the ops are not all loaded', function () {
|
|
|
|
before(function (done) {
|
|
|
|
// We only track 100 ops
|
|
|
|
return DocUpdaterClient.getDocAndRecentOps(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
10,
|
|
|
|
(error, res, returnedDoc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
this.res = res
|
|
|
|
this.returnedDoc = returnedDoc
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return UnprocessableEntity', function () {
|
|
|
|
return this.res.statusCode.should.equal(422)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the document does not exist', 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
|
|
|
])
|
|
|
|
return DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, doc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
this.statusCode = res.statusCode
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return 404', function () {
|
|
|
|
return this.statusCode.should.equal(404)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the web api returns an error', 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
|
|
|
])
|
2020-05-15 14:29:49 -04:00
|
|
|
sinon
|
|
|
|
.stub(MockWebApi, 'getDocument')
|
|
|
|
.callsFake((project_id, doc_id, callback) => {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-15 14:29:49 -04:00
|
|
|
}
|
|
|
|
return callback(new Error('oops'))
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
return DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, doc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
this.statusCode = res.statusCode
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
return MockWebApi.getDocument.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return 500', function () {
|
|
|
|
return this.statusCode.should.equal(500)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when the web api http request takes a long time', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.timeout = 10000
|
|
|
|
;[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
|
|
|
])
|
2020-05-15 14:29:49 -04:00
|
|
|
sinon
|
|
|
|
.stub(MockWebApi, 'getDocument')
|
|
|
|
.callsFake((project_id, doc_id, callback) => {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-15 14:29:49 -04:00
|
|
|
}
|
|
|
|
return setTimeout(callback, 30000)
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
return done()
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
return MockWebApi.getDocument.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return quickly(ish)', function (done) {
|
|
|
|
const start = Date.now()
|
|
|
|
return DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, doc) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-05-06 06:12:47 -04:00
|
|
|
res.statusCode.should.equal(500)
|
|
|
|
const delta = Date.now() - start
|
|
|
|
expect(delta).to.be.below(20000)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:17 -04:00
|
|
|
|
|
|
|
function __range__(left, right, inclusive) {
|
2020-05-06 06:12:47 -04:00
|
|
|
const range = []
|
|
|
|
const ascending = left < right
|
|
|
|
const end = !inclusive ? right : ascending ? right + 1 : right - 1
|
2020-05-06 06:12:17 -04:00
|
|
|
for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
|
2020-05-06 06:12:47 -04:00
|
|
|
range.push(i)
|
2020-05-06 06:12:17 -04:00
|
|
|
}
|
2020-05-06 06:12:47 -04:00
|
|
|
return range
|
|
|
|
}
|