2020-05-06 06:12:36 -04:00
|
|
|
// 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
|
|
|
|
* 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')
|
|
|
|
const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
|
|
|
|
const MockWebApi = require('./helpers/MockWebApi')
|
|
|
|
const DocUpdaterClient = require('./helpers/DocUpdaterClient')
|
|
|
|
const DocUpdaterApp = require('./helpers/DocUpdaterApp')
|
|
|
|
|
|
|
|
describe('Deleting a document', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.lines = ['one', 'two', 'three']
|
|
|
|
this.version = 42
|
|
|
|
this.update = {
|
|
|
|
doc: this.doc_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']
|
|
|
|
|
|
|
|
sinon.spy(MockProjectHistoryApi, 'flushProject')
|
2023-06-06 06:19:55 -04:00
|
|
|
DocUpdaterApp.ensureRunning(done)
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
MockProjectHistoryApi.flushProject.restore()
|
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')
|
|
|
|
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
|
|
|
})
|
2023-06-06 06:19:55 -04:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2023-06-06 06:19:55 -04:00
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
DocUpdaterClient.deleteDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
this.statusCode = res.statusCode
|
|
|
|
setTimeout(done, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
MockWebApi.setDocument.restore()
|
2023-06-06 06:19:55 -04:00
|
|
|
MockWebApi.getDocument.restore()
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return a 204 status code', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.statusCode.should.equal(204)
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should send the updated document and version to the web api', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
MockWebApi.setDocument
|
2020-05-06 06:12:47 -04:00
|
|
|
.calledWith(this.project_id, this.doc_id, this.result, this.version + 1)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should need to reload the doc if read again', function (done) {
|
2021-07-13 08:12:34 -04:00
|
|
|
MockWebApi.getDocument.resetHistory()
|
2021-07-13 07:35:32 -04:00
|
|
|
MockWebApi.getDocument.called.should.equals(false)
|
2023-06-06 06:19:55 -04:00
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
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
|
|
|
MockWebApi.getDocument
|
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
2023-06-06 06:19:55 -04:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should flush project history', function () {
|
|
|
|
MockProjectHistoryApi.flushProject
|
2020-05-06 06:12:47 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
describe('when the doc is not in the doc updater', function () {
|
2020-05-06 06:12:47 -04:00
|
|
|
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')
|
|
|
|
sinon.spy(MockWebApi, 'getDocument')
|
2023-06-06 06:19:55 -04:00
|
|
|
DocUpdaterClient.deleteDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, body) => {
|
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
|
2023-06-06 06:19:55 -04:00
|
|
|
setTimeout(done, 200)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
MockWebApi.setDocument.restore()
|
2023-06-06 06:19:55 -04:00
|
|
|
MockWebApi.getDocument.restore()
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return a 204 status code', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
this.statusCode.should.equal(204)
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not need to send the updated document to the web api', function () {
|
2023-06-06 06:19:55 -04:00
|
|
|
MockWebApi.setDocument.called.should.equal(false)
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should need to reload the doc if read again', function (done) {
|
2021-07-13 07:35:32 -04:00
|
|
|
MockWebApi.getDocument.called.should.equals(false)
|
2023-06-06 06:19:55 -04:00
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
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
|
|
|
MockWebApi.getDocument
|
|
|
|
.calledWith(this.project_id, this.doc_id)
|
|
|
|
.should.equal(true)
|
2023-06-06 06:19:55 -04:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-06-06 06:19:55 -04:00
|
|
|
it('should flush project history', function () {
|
|
|
|
MockProjectHistoryApi.flushProject
|
2020-05-06 06:12:47 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|