LeaveProjectTests.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* eslint-disable
  2. no-throw-literal,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import RealTimeClient from './helpers/RealTimeClient.js'
  13. import MockDocUpdaterServer from './helpers/MockDocUpdaterServer.js'
  14. import FixturesManager from './helpers/FixturesManager.js'
  15. import async from 'async'
  16. import settings from '@overleaf/settings'
  17. import redis from '@overleaf/redis-wrapper'
  18. const rclient = redis.createClient(settings.redis.pubsub)
  19. describe('leaveProject', function () {
  20. before(function (done) {
  21. return MockDocUpdaterServer.run(done)
  22. })
  23. describe('with other clients in the project', function () {
  24. before(function (done) {
  25. return async.series(
  26. [
  27. cb => {
  28. return FixturesManager.setUpProject(
  29. {
  30. privilegeLevel: 'owner',
  31. project: {
  32. name: 'Test Project',
  33. },
  34. },
  35. (e, { project_id: projectId, user_id: userId }) => {
  36. this.project_id = projectId
  37. this.user_id = userId
  38. return cb()
  39. }
  40. )
  41. },
  42. cb => {
  43. this.clientA = RealTimeClient.connect(this.project_id, cb)
  44. },
  45. cb => {
  46. this.clientB = RealTimeClient.connect(this.project_id, cb)
  47. this.clientBDisconnectMessages = []
  48. return this.clientB.on(
  49. 'clientTracking.clientDisconnected',
  50. data => {
  51. return this.clientBDisconnectMessages.push(data)
  52. }
  53. )
  54. },
  55. cb => {
  56. return FixturesManager.setUpDoc(
  57. this.project_id,
  58. { lines: this.lines, version: this.version, ops: this.ops },
  59. (e, { doc_id: docId }) => {
  60. this.doc_id = docId
  61. return cb(e)
  62. }
  63. )
  64. },
  65. cb => {
  66. return this.clientA.emit('joinDoc', this.doc_id, cb)
  67. },
  68. cb => {
  69. return this.clientB.emit('joinDoc', this.doc_id, cb)
  70. },
  71. cb => {
  72. // leaveProject is called when the client disconnects
  73. this.clientA.on('disconnect', () => cb())
  74. return this.clientA.disconnect()
  75. },
  76. cb => {
  77. // The API waits a little while before flushing changes
  78. return setTimeout(done, 1000)
  79. },
  80. ],
  81. done
  82. )
  83. })
  84. it('should emit a disconnect message to the room', function () {
  85. return this.clientBDisconnectMessages.should.deep.equal([
  86. this.clientA.publicId,
  87. ])
  88. })
  89. it('should no longer list the client in connected users', function (done) {
  90. return this.clientB.emit(
  91. 'clientTracking.getConnectedUsers',
  92. (error, users) => {
  93. if (error) return done(error)
  94. for (const user of Array.from(users)) {
  95. if (user.client_id === this.clientA.publicId) {
  96. throw 'Expected clientA to not be listed in connected users'
  97. }
  98. }
  99. return done()
  100. }
  101. )
  102. })
  103. it('should not flush the project to the document updater', function () {
  104. return MockDocUpdaterServer.deleteProject
  105. .calledWith(this.project_id)
  106. .should.equal(false)
  107. })
  108. it('should remain subscribed to the editor-events channels', function (done) {
  109. rclient.pubsub('CHANNELS', (err, resp) => {
  110. if (err) {
  111. return done(err)
  112. }
  113. resp.should.include(`editor-events:${this.project_id}`)
  114. return done()
  115. })
  116. return null
  117. })
  118. return it('should remain subscribed to the applied-ops channels', function (done) {
  119. rclient.pubsub('CHANNELS', (err, resp) => {
  120. if (err) {
  121. return done(err)
  122. }
  123. resp.should.include(`applied-ops:${this.doc_id}`)
  124. return done()
  125. })
  126. return null
  127. })
  128. })
  129. return describe('with no other clients in the project', function () {
  130. before(function (done) {
  131. return async.series(
  132. [
  133. cb => {
  134. return FixturesManager.setUpProject(
  135. {
  136. privilegeLevel: 'owner',
  137. project: {
  138. name: 'Test Project',
  139. },
  140. },
  141. (e, { project_id: projectId, user_id: userId }) => {
  142. this.project_id = projectId
  143. this.user_id = userId
  144. return cb()
  145. }
  146. )
  147. },
  148. cb => {
  149. this.clientA = RealTimeClient.connect(
  150. this.project_id,
  151. (error, project, privilegeLevel, protocolVersion) => {
  152. this.project = project
  153. this.privilegeLevel = privilegeLevel
  154. this.protocolVersion = protocolVersion
  155. return cb(error)
  156. }
  157. )
  158. },
  159. cb => {
  160. return FixturesManager.setUpDoc(
  161. this.project_id,
  162. { lines: this.lines, version: this.version, ops: this.ops },
  163. (e, { doc_id: docId }) => {
  164. this.doc_id = docId
  165. return cb(e)
  166. }
  167. )
  168. },
  169. cb => {
  170. return this.clientA.emit('joinDoc', this.doc_id, cb)
  171. },
  172. cb => {
  173. // leaveProject is called when the client disconnects
  174. this.clientA.on('disconnect', () => cb())
  175. return this.clientA.disconnect()
  176. },
  177. cb => {
  178. // The API waits a little while before flushing changes
  179. return setTimeout(done, 1000)
  180. },
  181. ],
  182. done
  183. )
  184. })
  185. it('should flush the project to the document updater', function () {
  186. return MockDocUpdaterServer.deleteProject
  187. .calledWith(this.project_id)
  188. .should.equal(true)
  189. })
  190. it('should not subscribe to the editor-events channels anymore', function (done) {
  191. rclient.pubsub('CHANNELS', (err, resp) => {
  192. if (err) {
  193. return done(err)
  194. }
  195. resp.should.not.include(`editor-events:${this.project_id}`)
  196. return done()
  197. })
  198. return null
  199. })
  200. return it('should not subscribe to the applied-ops channels anymore', function (done) {
  201. rclient.pubsub('CHANNELS', (err, resp) => {
  202. if (err) {
  203. return done(err)
  204. }
  205. resp.should.not.include(`applied-ops:${this.doc_id}`)
  206. return done()
  207. })
  208. return null
  209. })
  210. })
  211. })