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')
|
2016-11-28 05:14:42 -05:00
|
|
|
|
2020-08-25 08:29:58 -04:00
|
|
|
const { db, ObjectId } = require('../../../app/js/mongodb')
|
2020-05-06 06:12:47 -04:00
|
|
|
const MockWebApi = require('./helpers/MockWebApi')
|
|
|
|
const DocUpdaterClient = require('./helpers/DocUpdaterClient')
|
|
|
|
const DocUpdaterApp = require('./helpers/DocUpdaterApp')
|
2024-04-16 04:55:56 -04:00
|
|
|
const RangesManager = require('../../../app/js/RangesManager')
|
|
|
|
|
|
|
|
const sandbox = sinon.createSandbox()
|
2016-11-28 05:14:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('Ranges', function () {
|
|
|
|
before(function (done) {
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterApp.ensureRunning(done)
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('tracking changes from ops', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.id_seed = '587357bd35e64f6157'
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['aaa'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ i: '123', p: 1 }],
|
|
|
|
v: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id },
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ i: '456', p: 5 }],
|
|
|
|
v: 1,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id, tc: this.id_seed },
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ d: '12', p: 1 }],
|
|
|
|
v: 2,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id },
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
]
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
const jobs = []
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const update of this.updates) {
|
|
|
|
jobs.push(callback =>
|
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
update,
|
|
|
|
callback
|
2020-05-06 06:12:47 -04:00
|
|
|
)
|
2024-02-13 08:15:43 -05:00
|
|
|
)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2016-12-13 10:51:47 -05:00
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterApp.ensureRunning(error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
async.series(jobs, error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
})
|
2016-11-28 05:14:42 -05:00
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
it('should update the ranges', function (done) {
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
const change = ranges.changes[0]
|
|
|
|
change.op.should.deep.equal({ i: '456', p: 3 })
|
|
|
|
change.id.should.equal(this.id_seed + '000001')
|
|
|
|
change.metadata.user_id.should.equal(this.user_id)
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2017-01-09 08:41:18 -05:00
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
describe('Adding comments', function () {
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('standalone', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['foo bar baz'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [
|
2021-07-13 07:04:42 -04:00
|
|
|
{ c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) },
|
2020-05-06 06:12:47 -04:00
|
|
|
],
|
2021-07-13 07:04:42 -04:00
|
|
|
v: 0,
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
]
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
const jobs = []
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const update of this.updates) {
|
|
|
|
jobs.push(callback =>
|
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
update,
|
|
|
|
callback
|
2020-05-06 06:12:47 -04:00
|
|
|
)
|
2024-02-13 08:15:43 -05:00
|
|
|
)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
async.series(jobs, error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
setTimeout(done, 200)
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2017-01-24 09:57:11 -05:00
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
it('should update the ranges', function (done) {
|
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
const comment = ranges.comments[0]
|
|
|
|
comment.op.should.deep.equal({ c: 'bar', p: 4, t: this.tid })
|
|
|
|
comment.id.should.equal(this.tid)
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
describe('with conflicting ops needing OT', function () {
|
2020-05-06 06:12:47 -04:00
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['foo bar baz'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ i: 'ABC', p: 3 }],
|
|
|
|
v: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id },
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [
|
2021-07-13 07:04:42 -04:00
|
|
|
{ c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) },
|
2020-05-06 06:12:47 -04:00
|
|
|
],
|
2021-07-13 07:04:42 -04:00
|
|
|
v: 0,
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
]
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
const jobs = []
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const update of this.updates) {
|
|
|
|
jobs.push(callback =>
|
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
update,
|
|
|
|
callback
|
2020-05-06 06:12:47 -04:00
|
|
|
)
|
2024-02-13 08:15:43 -05:00
|
|
|
)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
async.series(jobs, error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
setTimeout(done, 200)
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
it('should update the comments with the OT shifted comment', function (done) {
|
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
const comment = ranges.comments[0]
|
|
|
|
comment.op.should.deep.equal({ c: 'bar', p: 7, t: this.tid })
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Loading ranges from persistence layer', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.id_seed = '587357bd35e64f6157'
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['a123aa'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.update = {
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ i: '456', p: 5 }],
|
|
|
|
v: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id, tc: this.id_seed },
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
|
|
|
version: 0,
|
|
|
|
ranges: {
|
|
|
|
changes: [
|
|
|
|
{
|
|
|
|
op: { i: '123', p: 1 },
|
|
|
|
metadata: {
|
|
|
|
user_id: this.user_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.update,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
setTimeout(done, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should have preloaded the existing ranges', function (done) {
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { changes } = data.ranges
|
|
|
|
changes[0].op.should.deep.equal({ i: '123', p: 1 })
|
|
|
|
changes[1].op.should.deep.equal({ i: '456', p: 5 })
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
it('should flush the ranges to the persistence layer again', function (done) {
|
|
|
|
DocUpdaterClient.flushDoc(this.project_id, this.doc.id, error => {
|
2021-07-13 07:04:42 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
MockWebApi.getDocument(this.project_id, this.doc.id, (error, doc) => {
|
|
|
|
if (error) return done(error)
|
|
|
|
const { changes } = doc.ranges
|
|
|
|
changes[0].op.should.deep.equal({ i: '123', p: 1 })
|
|
|
|
changes[1].op.should.deep.equal({ i: '456', p: 5 })
|
|
|
|
done()
|
|
|
|
})
|
2021-07-13 07:04:42 -04:00
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('accepting a change', function () {
|
2023-09-06 10:23:23 -04:00
|
|
|
beforeEach(function (done) {
|
2024-04-16 04:55:56 -04:00
|
|
|
sandbox.spy(MockWebApi, 'setDocument')
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.id_seed = '587357bd35e64f6157'
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['aaa'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.update = {
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ i: '456', p: 1 }],
|
|
|
|
v: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id, tc: this.id_seed },
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.update,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
const change = ranges.changes[0]
|
|
|
|
change.op.should.deep.equal({ i: '456', p: 1 })
|
|
|
|
change.id.should.equal(this.id_seed + '000001')
|
|
|
|
change.metadata.user_id.should.equal(this.user_id)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2023-09-06 10:23:23 -04:00
|
|
|
afterEach(function () {
|
2024-04-16 04:55:56 -04:00
|
|
|
sandbox.restore()
|
2023-09-06 10:23:23 -04:00
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
|
2023-09-06 10:23:23 -04:00
|
|
|
it('should remove the change after accepting', function (done) {
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.acceptChange(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.id_seed + '000001',
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
expect(data.ranges.changes).to.be.undefined
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2023-09-06 10:23:23 -04:00
|
|
|
|
|
|
|
it('should persist the ranges after accepting', function (done) {
|
|
|
|
DocUpdaterClient.flushDoc(this.project_id, this.doc.id, err => {
|
|
|
|
if (err) return done(err)
|
|
|
|
DocUpdaterClient.acceptChange(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.id_seed + '000001',
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
DocUpdaterClient.flushDoc(this.project_id, this.doc.id, err => {
|
|
|
|
if (err) return done(err)
|
|
|
|
DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
expect(data.ranges.changes).to.be.undefined
|
|
|
|
|
|
|
|
MockWebApi.setDocument
|
|
|
|
.calledWith(this.project_id, this.doc.id, ['a456aa'], 1, {})
|
|
|
|
.should.equal(true)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
2024-04-16 04:55:56 -04:00
|
|
|
describe('accepting multiple changes', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.getHistoryUpdatesSpy = sandbox.spy(
|
|
|
|
RangesManager,
|
|
|
|
'getHistoryUpdatesForAcceptedChanges'
|
|
|
|
)
|
|
|
|
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
|
|
|
lines: ['aaa', 'bbb', 'ccc', 'ddd', 'eee'],
|
|
|
|
}
|
|
|
|
|
|
|
|
DocUpdaterClient.enableHistoryRangesSupport(this.doc.id, (error, res) => {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
|
|
|
version: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
this.id_seed_1 = 'tc_1'
|
|
|
|
this.id_seed_2 = 'tc_2'
|
|
|
|
this.id_seed_3 = 'tc_3'
|
|
|
|
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ d: 'bbb', p: 4 }],
|
|
|
|
v: 0,
|
|
|
|
meta: {
|
|
|
|
user_id: this.user_id,
|
|
|
|
tc: this.id_seed_1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ d: 'ccc', p: 5 }],
|
|
|
|
v: 1,
|
|
|
|
meta: {
|
|
|
|
user_id: this.user_id,
|
|
|
|
tc: this.id_seed_2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ d: 'ddd', p: 6 }],
|
|
|
|
v: 2,
|
|
|
|
meta: {
|
|
|
|
user_id: this.user_id,
|
|
|
|
tc: this.id_seed_3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
DocUpdaterClient.sendUpdates(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.updates,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
const changeOps = ranges.changes
|
|
|
|
.map(change => change.op)
|
|
|
|
.flat()
|
|
|
|
changeOps.should.deep.equal([
|
|
|
|
{ d: 'bbb', p: 4 },
|
|
|
|
{ d: 'ccc', p: 5 },
|
|
|
|
{ d: 'ddd', p: 6 },
|
|
|
|
])
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('accepting changes in order', function (done) {
|
|
|
|
DocUpdaterClient.acceptChanges(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
[
|
|
|
|
this.id_seed_1 + '000001',
|
|
|
|
this.id_seed_2 + '000001',
|
|
|
|
this.id_seed_3 + '000001',
|
|
|
|
],
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
const historyUpdates = this.getHistoryUpdatesSpy.returnValues[0]
|
|
|
|
expect(historyUpdates[0]).to.deep.equal({
|
|
|
|
doc: this.doc.id,
|
|
|
|
meta: {
|
|
|
|
pathname: '/a/b/c.tex',
|
|
|
|
doc_length: 10,
|
|
|
|
history_doc_length: 19,
|
|
|
|
ts: historyUpdates[0].meta.ts,
|
|
|
|
user_id: this.user_id,
|
|
|
|
},
|
|
|
|
op: [{ p: 4, d: 'bbb' }],
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(historyUpdates[1]).to.deep.equal({
|
|
|
|
doc: this.doc.id,
|
|
|
|
meta: {
|
|
|
|
pathname: '/a/b/c.tex',
|
|
|
|
doc_length: 10,
|
|
|
|
history_doc_length: 16,
|
|
|
|
ts: historyUpdates[1].meta.ts,
|
|
|
|
user_id: this.user_id,
|
|
|
|
},
|
|
|
|
op: [{ p: 5, d: 'ccc' }],
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(historyUpdates[2]).to.deep.equal({
|
|
|
|
doc: this.doc.id,
|
|
|
|
meta: {
|
|
|
|
pathname: '/a/b/c.tex',
|
|
|
|
doc_length: 10,
|
|
|
|
history_doc_length: 13,
|
|
|
|
ts: historyUpdates[2].meta.ts,
|
|
|
|
user_id: this.user_id,
|
|
|
|
},
|
|
|
|
op: [{ p: 6, d: 'ddd' }],
|
|
|
|
})
|
|
|
|
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('accepting changes in reverse order', function (done) {
|
|
|
|
DocUpdaterClient.acceptChanges(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
[
|
|
|
|
this.id_seed_3 + '000001',
|
|
|
|
this.id_seed_2 + '000001',
|
|
|
|
this.id_seed_1 + '000001',
|
|
|
|
],
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
const historyUpdates = this.getHistoryUpdatesSpy.returnValues[0]
|
|
|
|
expect(historyUpdates[0]).to.deep.equal({
|
|
|
|
doc: this.doc.id,
|
|
|
|
meta: {
|
|
|
|
pathname: '/a/b/c.tex',
|
|
|
|
doc_length: 10,
|
|
|
|
history_doc_length: 19,
|
|
|
|
ts: historyUpdates[0].meta.ts,
|
|
|
|
user_id: this.user_id,
|
|
|
|
},
|
|
|
|
op: [{ p: 4, d: 'bbb' }],
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(historyUpdates[1]).to.deep.equal({
|
|
|
|
doc: this.doc.id,
|
|
|
|
meta: {
|
|
|
|
pathname: '/a/b/c.tex',
|
|
|
|
doc_length: 10,
|
|
|
|
history_doc_length: 16,
|
|
|
|
ts: historyUpdates[1].meta.ts,
|
|
|
|
user_id: this.user_id,
|
|
|
|
},
|
|
|
|
op: [{ p: 5, d: 'ccc' }],
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(historyUpdates[2]).to.deep.equal({
|
|
|
|
doc: this.doc.id,
|
|
|
|
meta: {
|
|
|
|
pathname: '/a/b/c.tex',
|
|
|
|
doc_length: 10,
|
|
|
|
history_doc_length: 13,
|
|
|
|
ts: historyUpdates[2].meta.ts,
|
|
|
|
user_id: this.user_id,
|
|
|
|
},
|
|
|
|
op: [{ p: 6, d: 'ddd' }],
|
|
|
|
})
|
|
|
|
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-06 06:12:47 -04:00
|
|
|
describe('deleting a comment range', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['foo bar'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.update = {
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ c: 'bar', p: 4, t: (this.tid = DocUpdaterClient.randomId()) }],
|
2021-07-13 07:04:42 -04:00
|
|
|
v: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.update,
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
const change = ranges.comments[0]
|
|
|
|
change.op.should.deep.equal({ c: 'bar', p: 4, t: this.tid })
|
|
|
|
change.id.should.equal(this.tid)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
it('should remove the comment range', function (done) {
|
|
|
|
DocUpdaterClient.removeComment(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
this.tid,
|
|
|
|
(error, res) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
expect(res.statusCode).to.equal(204)
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
expect(data.ranges.comments).to.be.undefined
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('tripping range size limit', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.id_seed = DocUpdaterClient.randomId()
|
|
|
|
this.doc = {
|
|
|
|
id: DocUpdaterClient.randomId(),
|
2021-07-13 07:04:42 -04:00
|
|
|
lines: ['aaa'],
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
this.i = new Array(3 * 1024 * 1024).join('a')
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
doc: this.doc.id,
|
|
|
|
op: [{ i: this.i, p: 1 }],
|
|
|
|
v: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id, tc: this.id_seed },
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
]
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc.id, {
|
|
|
|
lines: this.doc.lines,
|
2021-07-13 07:04:42 -04:00
|
|
|
version: 0,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
const jobs = []
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const update of this.updates) {
|
|
|
|
jobs.push(callback =>
|
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
update,
|
|
|
|
callback
|
2020-05-06 06:12:47 -04:00
|
|
|
)
|
2024-02-13 08:15:43 -05:00
|
|
|
)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
async.series(jobs, error => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
setTimeout(done, 200)
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
it('should not update the ranges', function (done) {
|
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc.id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
const { ranges } = data
|
|
|
|
expect(ranges.changes).to.be.undefined
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
describe('deleting text surrounding a comment', function () {
|
2020-05-06 06:12:47 -04:00
|
|
|
before(function (done) {
|
|
|
|
this.project_id = DocUpdaterClient.randomId()
|
|
|
|
this.user_id = DocUpdaterClient.randomId()
|
|
|
|
this.doc_id = DocUpdaterClient.randomId()
|
|
|
|
MockWebApi.insertDoc(this.project_id, this.doc_id, {
|
|
|
|
lines: ['foo bar baz'],
|
|
|
|
version: 0,
|
|
|
|
ranges: {
|
|
|
|
comments: [
|
|
|
|
{
|
|
|
|
op: {
|
|
|
|
c: 'a',
|
|
|
|
p: 5,
|
2021-07-13 07:04:42 -04:00
|
|
|
tid: (this.tid = DocUpdaterClient.randomId()),
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
metadata: {
|
|
|
|
user_id: this.user_id,
|
2021-07-13 07:04:42 -04:00
|
|
|
ts: new Date(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
this.updates = [
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
op: [{ d: 'foo ', p: 0 }],
|
|
|
|
v: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id },
|
2020-05-06 06:12:47 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
op: [{ d: 'bar ', p: 0 }],
|
|
|
|
v: 1,
|
2021-07-13 07:04:42 -04:00
|
|
|
meta: { user_id: this.user_id },
|
|
|
|
},
|
2020-05-06 06:12:47 -04:00
|
|
|
]
|
|
|
|
const jobs = []
|
2024-02-13 08:15:43 -05:00
|
|
|
for (const update of this.updates) {
|
|
|
|
jobs.push(callback =>
|
|
|
|
DocUpdaterClient.sendUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
update,
|
|
|
|
callback
|
2020-05-06 06:12:47 -04:00
|
|
|
)
|
2024-02-13 08:15:43 -05:00
|
|
|
)
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
async.series(jobs, function (error) {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}, 200)
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
|
|
|
|
2024-02-13 08:15:43 -05:00
|
|
|
it('should write a snapshot from before the destructive change', function (done) {
|
|
|
|
DocUpdaterClient.getDoc(
|
2020-05-06 06:12:47 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, data) => {
|
|
|
|
if (error != null) {
|
|
|
|
return done(error)
|
|
|
|
}
|
2020-08-25 08:29:58 -04:00
|
|
|
db.docSnapshots
|
|
|
|
.find({
|
2023-12-14 09:28:19 -05:00
|
|
|
project_id: new ObjectId(this.project_id),
|
|
|
|
doc_id: new ObjectId(this.doc_id),
|
2020-08-25 08:29:58 -04:00
|
|
|
})
|
|
|
|
.toArray((error, docSnapshots) => {
|
2020-05-06 06:12:47 -04:00
|
|
|
if (error != null) {
|
|
|
|
return done(error)
|
|
|
|
}
|
|
|
|
expect(docSnapshots.length).to.equal(1)
|
|
|
|
expect(docSnapshots[0].version).to.equal(1)
|
|
|
|
expect(docSnapshots[0].lines).to.deep.equal(['bar baz'])
|
|
|
|
expect(docSnapshots[0].ranges.comments[0].op).to.deep.equal({
|
|
|
|
c: 'a',
|
|
|
|
p: 1,
|
2021-07-13 07:04:42 -04:00
|
|
|
tid: this.tid,
|
2020-05-06 06:12:47 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
done()
|
2020-08-25 08:29:58 -04:00
|
|
|
})
|
2020-05-06 06:12:47 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|