2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:30:29 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* 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
|
|
|
|
*/
|
2021-03-18 16:19:31 -04:00
|
|
|
const { expect } = require('chai')
|
2020-06-23 13:30:45 -04:00
|
|
|
|
|
|
|
const RealTimeClient = require('./helpers/RealTimeClient')
|
|
|
|
const MockWebServer = require('./helpers/MockWebServer')
|
|
|
|
const FixturesManager = require('./helpers/FixturesManager')
|
|
|
|
|
|
|
|
const async = require('async')
|
|
|
|
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-11-10 06:32:06 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-06-23 13:30:45 -04:00
|
|
|
const rclient = redis.createClient(settings.redis.pubsub)
|
|
|
|
|
|
|
|
describe('receiveUpdate', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.lines = ['test', 'doc', 'lines']
|
|
|
|
this.version = 42
|
|
|
|
this.ops = ['mock', 'doc', 'ops']
|
|
|
|
|
|
|
|
return async.series(
|
|
|
|
[
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'owner',
|
2021-07-13 07:04:45 -04:00
|
|
|
project: { name: 'Test Project' },
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(error, { user_id: userId, project_id: projectId }) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2023-03-20 10:10:40 -04:00
|
|
|
this.user_id = userId
|
|
|
|
this.project_id = projectId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { doc_id: docId }) => {
|
|
|
|
this.doc_id = docId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientA = RealTimeClient.connect(this.project_id, cb)
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientB = RealTimeClient.connect(this.project_id, cb)
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return this.clientA.emit('joinDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return this.clientB.emit('joinDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'owner',
|
2021-07-13 07:04:45 -04:00
|
|
|
project: { name: 'Test Project' },
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(error, { user_id: userIdSecond, project_id: projectIdSecond }) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2023-03-20 10:10:40 -04:00
|
|
|
this.user_id_second = userIdSecond
|
|
|
|
this.project_id_second = projectIdSecond
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id_second,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { doc_id: docIdSecond }) => {
|
|
|
|
this.doc_id_second = docIdSecond
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2023-11-29 09:50:21 -05:00
|
|
|
this.clientC = RealTimeClient.connect(this.project_id_second, cb)
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return this.clientC.emit('joinDoc', this.doc_id_second, cb)
|
|
|
|
},
|
|
|
|
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientAUpdates = []
|
2021-07-13 07:04:45 -04:00
|
|
|
this.clientA.on('otUpdateApplied', update =>
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientAUpdates.push(update)
|
|
|
|
)
|
|
|
|
this.clientBUpdates = []
|
2021-07-13 07:04:45 -04:00
|
|
|
this.clientB.on('otUpdateApplied', update =>
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientBUpdates.push(update)
|
|
|
|
)
|
|
|
|
this.clientCUpdates = []
|
2021-07-13 07:04:45 -04:00
|
|
|
this.clientC.on('otUpdateApplied', update =>
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientCUpdates.push(update)
|
|
|
|
)
|
|
|
|
|
|
|
|
this.clientAErrors = []
|
2021-07-13 07:04:45 -04:00
|
|
|
this.clientA.on('otUpdateError', error =>
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientAErrors.push(error)
|
|
|
|
)
|
|
|
|
this.clientBErrors = []
|
2021-07-13 07:04:45 -04:00
|
|
|
this.clientB.on('otUpdateError', error =>
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientBErrors.push(error)
|
|
|
|
)
|
|
|
|
this.clientCErrors = []
|
2021-07-13 07:04:45 -04:00
|
|
|
this.clientC.on('otUpdateError', error =>
|
2020-06-23 13:30:45 -04:00
|
|
|
this.clientCErrors.push(error)
|
|
|
|
)
|
|
|
|
return cb()
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
if (this.clientA != null) {
|
|
|
|
this.clientA.disconnect()
|
|
|
|
}
|
|
|
|
if (this.clientB != null) {
|
|
|
|
this.clientB.disconnect()
|
|
|
|
}
|
|
|
|
return this.clientC != null ? this.clientC.disconnect() : undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an update from clientA', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.update = {
|
|
|
|
doc_id: this.doc_id,
|
|
|
|
op: {
|
|
|
|
meta: {
|
2021-07-13 07:04:45 -04:00
|
|
|
source: this.clientA.publicId,
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
v: this.version,
|
|
|
|
doc: this.doc_id,
|
2021-07-13 07:04:45 -04:00
|
|
|
op: [{ i: 'foo', p: 50 }],
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
rclient.publish('applied-ops', JSON.stringify(this.update))
|
|
|
|
return setTimeout(done, 200)
|
|
|
|
}) // Give clients time to get message
|
|
|
|
|
|
|
|
it('should send the full op to clientB', function () {
|
|
|
|
return this.clientBUpdates.should.deep.equal([this.update.op])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send an ack to clientA', function () {
|
|
|
|
return this.clientAUpdates.should.deep.equal([
|
|
|
|
{
|
|
|
|
v: this.version,
|
2021-07-13 07:04:45 -04:00
|
|
|
doc: this.doc_id,
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should send nothing to clientC', function () {
|
|
|
|
return this.clientCUpdates.should.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an update from clientC', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.update = {
|
|
|
|
doc_id: this.doc_id_second,
|
|
|
|
op: {
|
|
|
|
meta: {
|
2021-07-13 07:04:45 -04:00
|
|
|
source: this.clientC.publicId,
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
v: this.version,
|
|
|
|
doc: this.doc_id_second,
|
2021-07-13 07:04:45 -04:00
|
|
|
op: [{ i: 'update from clientC', p: 50 }],
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
rclient.publish('applied-ops', JSON.stringify(this.update))
|
|
|
|
return setTimeout(done, 200)
|
|
|
|
}) // Give clients time to get message
|
|
|
|
|
|
|
|
it('should send nothing to clientA', function () {
|
|
|
|
return this.clientAUpdates.should.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send nothing to clientB', function () {
|
|
|
|
return this.clientBUpdates.should.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should send an ack to clientC', function () {
|
|
|
|
return this.clientCUpdates.should.deep.equal([
|
|
|
|
{
|
|
|
|
v: this.version,
|
2021-07-13 07:04:45 -04:00
|
|
|
doc: this.doc_id_second,
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an update from a remote client for project 1', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.update = {
|
|
|
|
doc_id: this.doc_id,
|
|
|
|
op: {
|
|
|
|
meta: {
|
2021-07-13 07:04:45 -04:00
|
|
|
source: 'this-is-a-remote-client-id',
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
|
|
|
v: this.version,
|
|
|
|
doc: this.doc_id,
|
2021-07-13 07:04:45 -04:00
|
|
|
op: [{ i: 'foo', p: 50 }],
|
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
}
|
|
|
|
rclient.publish('applied-ops', JSON.stringify(this.update))
|
|
|
|
return setTimeout(done, 200)
|
|
|
|
}) // Give clients time to get message
|
|
|
|
|
|
|
|
it('should send the full op to clientA', function () {
|
|
|
|
return this.clientAUpdates.should.deep.equal([this.update.op])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send the full op to clientB', function () {
|
|
|
|
return this.clientBUpdates.should.deep.equal([this.update.op])
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should send nothing to clientC', function () {
|
|
|
|
return this.clientCUpdates.should.deep.equal([])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an error for the first project', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
rclient.publish(
|
|
|
|
'applied-ops',
|
|
|
|
JSON.stringify({
|
|
|
|
doc_id: this.doc_id,
|
2021-07-13 07:04:45 -04:00
|
|
|
error: (this.error = 'something went wrong'),
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
return setTimeout(done, 200)
|
|
|
|
}) // Give clients time to get message
|
|
|
|
|
|
|
|
it('should send the error to the clients in the first project', function () {
|
|
|
|
this.clientAErrors.should.deep.equal([this.error])
|
|
|
|
return this.clientBErrors.should.deep.equal([this.error])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not send any errors to the client in the second project', function () {
|
|
|
|
return this.clientCErrors.should.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should disconnect the clients of the first project', function () {
|
|
|
|
this.clientA.socket.connected.should.equal(false)
|
|
|
|
return this.clientB.socket.connected.should.equal(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should not disconnect the client in the second project', function () {
|
|
|
|
return this.clientC.socket.connected.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('with an error for the second project', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
rclient.publish(
|
|
|
|
'applied-ops',
|
|
|
|
JSON.stringify({
|
|
|
|
doc_id: this.doc_id_second,
|
2021-07-13 07:04:45 -04:00
|
|
|
error: (this.error = 'something went wrong'),
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
return setTimeout(done, 200)
|
|
|
|
}) // Give clients time to get message
|
|
|
|
|
|
|
|
it('should not send any errors to the clients in the first project', function () {
|
|
|
|
this.clientAErrors.should.deep.equal([])
|
|
|
|
return this.clientBErrors.should.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send the error to the client in the second project', function () {
|
|
|
|
return this.clientCErrors.should.deep.equal([this.error])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not disconnect the clients of the first project', function () {
|
|
|
|
this.clientA.socket.connected.should.equal(true)
|
|
|
|
return this.clientB.socket.connected.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should disconnect the client in the second project', function () {
|
|
|
|
return this.clientC.socket.connected.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|