2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
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:
|
|
|
|
* 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
|
|
|
|
*/
|
2021-03-18 16:19:31 -04:00
|
|
|
const { expect } = require('chai')
|
2020-06-23 13:30:45 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
|
|
|
|
const RealTimeClient = require('./helpers/RealTimeClient')
|
|
|
|
const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
|
|
|
|
const FixturesManager = require('./helpers/FixturesManager')
|
2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-06-23 13:30:45 -04:00
|
|
|
|
|
|
|
const async = require('async')
|
|
|
|
|
|
|
|
describe('leaveDoc', function () {
|
|
|
|
before(function () {
|
|
|
|
this.lines = ['test', 'doc', 'lines']
|
|
|
|
this.version = 42
|
|
|
|
this.ops = ['mock', 'doc', 'ops']
|
|
|
|
sinon.spy(logger, 'error')
|
|
|
|
sinon.spy(logger, 'warn')
|
2021-09-14 04:36:24 -04:00
|
|
|
sinon.spy(logger, 'debug')
|
2020-06-23 13:30:45 -04:00
|
|
|
return (this.other_doc_id = FixturesManager.getRandomId())
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
logger.error.restore() // remove the spy
|
|
|
|
logger.warn.restore()
|
2021-09-14 04:36:24 -04:00
|
|
|
return logger.debug.restore()
|
2020-06-23 13:30:45 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when joined to a doc', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
return async.series(
|
|
|
|
[
|
2021-07-13 07:04:45 -04:00
|
|
|
cb => {
|
2020-06-23 13:30:45 -04:00
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
2021-07-13 07:04:45 -04:00
|
|
|
privilegeLevel: 'readAndWrite',
|
2020-06-23 13:30:45 -04:00
|
|
|
},
|
2023-03-20 10:10:40 -04:00
|
|
|
(e, { project_id: projectId, user_id: userId }) => {
|
|
|
|
this.project_id = projectId
|
|
|
|
this.user_id = userId
|
2020-06-23 13:30:45 -04:00
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
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.client = 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.client.emit(
|
|
|
|
'joinDoc',
|
|
|
|
this.doc_id,
|
|
|
|
(error, ...rest) => {
|
|
|
|
;[...this.returnedArgs] = Array.from(rest)
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
)
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:30:45 -04:00
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('then leaving the doc', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-07-13 07:04:45 -04:00
|
|
|
return this.client.emit('leaveDoc', this.doc_id, error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should have left the doc room', function (done) {
|
|
|
|
return RealTimeClient.getConnectedClient(
|
|
|
|
this.client.socket.sessionid,
|
|
|
|
(error, client) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-06-23 13:30:45 -04:00
|
|
|
expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-01 05:10:10 -04:00
|
|
|
describe('then leaving an invalid doc', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
return this.client.emit('leaveDoc', 'bad-id', error => {
|
|
|
|
this.error = error
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return an error', function () {
|
|
|
|
return expect(this.error).to.exist
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
describe('when sending a leaveDoc request before the previous joinDoc request has completed', function () {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.client.emit('leaveDoc', this.doc_id, () => {})
|
|
|
|
this.client.emit('joinDoc', this.doc_id, () => {})
|
2021-07-13 07:04:45 -04:00
|
|
|
return this.client.emit('leaveDoc', this.doc_id, error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not trigger an error', function () {
|
|
|
|
return sinon.assert.neverCalledWith(
|
|
|
|
logger.error,
|
|
|
|
sinon.match.any,
|
|
|
|
"not subscribed - shouldn't happen"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should have left the doc room', function (done) {
|
|
|
|
return RealTimeClient.getConnectedClient(
|
|
|
|
this.client.socket.sessionid,
|
|
|
|
(error, client) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-06-23 13:30:45 -04:00
|
|
|
expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
|
|
|
|
false
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when sending a leaveDoc for a room the client has not joined ', function () {
|
|
|
|
beforeEach(function (done) {
|
2021-07-13 07:04:45 -04:00
|
|
|
return this.client.emit('leaveDoc', this.other_doc_id, error => {
|
2020-06-23 13:30:45 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should trigger a low level message only', function () {
|
|
|
|
return sinon.assert.calledWith(
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug,
|
2020-06-23 13:30:45 -04:00
|
|
|
sinon.match.any,
|
|
|
|
'ignoring request from client to leave room it is not in'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|