LeaveDocTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const { expect } = require('chai')
  16. const sinon = require('sinon')
  17. const RealTimeClient = require('./helpers/RealTimeClient')
  18. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  19. const FixturesManager = require('./helpers/FixturesManager')
  20. const logger = require('@overleaf/logger')
  21. const async = require('async')
  22. describe('leaveDoc', function () {
  23. before(function () {
  24. this.lines = ['test', 'doc', 'lines']
  25. this.version = 42
  26. this.ops = ['mock', 'doc', 'ops']
  27. sinon.spy(logger, 'error')
  28. sinon.spy(logger, 'warn')
  29. sinon.spy(logger, 'debug')
  30. return (this.other_doc_id = FixturesManager.getRandomId())
  31. })
  32. after(function () {
  33. logger.error.restore() // remove the spy
  34. logger.warn.restore()
  35. return logger.debug.restore()
  36. })
  37. return describe('when joined to a doc', function () {
  38. beforeEach(function (done) {
  39. return async.series(
  40. [
  41. cb => {
  42. return FixturesManager.setUpProject(
  43. {
  44. privilegeLevel: 'readAndWrite',
  45. },
  46. (e, { project_id, user_id }) => {
  47. this.project_id = project_id
  48. this.user_id = user_id
  49. return cb(e)
  50. }
  51. )
  52. },
  53. cb => {
  54. return FixturesManager.setUpDoc(
  55. this.project_id,
  56. { lines: this.lines, version: this.version, ops: this.ops },
  57. (e, { doc_id }) => {
  58. this.doc_id = doc_id
  59. return cb(e)
  60. }
  61. )
  62. },
  63. cb => {
  64. this.client = RealTimeClient.connect()
  65. return this.client.on('connectionAccepted', cb)
  66. },
  67. cb => {
  68. return this.client.emit(
  69. 'joinProject',
  70. { project_id: this.project_id },
  71. cb
  72. )
  73. },
  74. cb => {
  75. return this.client.emit(
  76. 'joinDoc',
  77. this.doc_id,
  78. (error, ...rest) => {
  79. ;[...this.returnedArgs] = Array.from(rest)
  80. return cb(error)
  81. }
  82. )
  83. },
  84. ],
  85. done
  86. )
  87. })
  88. describe('then leaving the doc', function () {
  89. beforeEach(function (done) {
  90. return this.client.emit('leaveDoc', this.doc_id, error => {
  91. if (error != null) {
  92. throw error
  93. }
  94. return done()
  95. })
  96. })
  97. return it('should have left the doc room', function (done) {
  98. return RealTimeClient.getConnectedClient(
  99. this.client.socket.sessionid,
  100. (error, client) => {
  101. if (error) return done(error)
  102. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  103. false
  104. )
  105. return done()
  106. }
  107. )
  108. })
  109. })
  110. describe('then leaving an invalid doc', function () {
  111. beforeEach(function (done) {
  112. return this.client.emit('leaveDoc', 'bad-id', error => {
  113. this.error = error
  114. return done()
  115. })
  116. })
  117. return it('should return an error', function () {
  118. return expect(this.error).to.exist
  119. })
  120. })
  121. describe('when sending a leaveDoc request before the previous joinDoc request has completed', function () {
  122. beforeEach(function (done) {
  123. this.client.emit('leaveDoc', this.doc_id, () => {})
  124. this.client.emit('joinDoc', this.doc_id, () => {})
  125. return this.client.emit('leaveDoc', this.doc_id, error => {
  126. if (error != null) {
  127. throw error
  128. }
  129. return done()
  130. })
  131. })
  132. it('should not trigger an error', function () {
  133. return sinon.assert.neverCalledWith(
  134. logger.error,
  135. sinon.match.any,
  136. "not subscribed - shouldn't happen"
  137. )
  138. })
  139. return it('should have left the doc room', function (done) {
  140. return RealTimeClient.getConnectedClient(
  141. this.client.socket.sessionid,
  142. (error, client) => {
  143. if (error) return done(error)
  144. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(
  145. false
  146. )
  147. return done()
  148. }
  149. )
  150. })
  151. })
  152. return describe('when sending a leaveDoc for a room the client has not joined ', function () {
  153. beforeEach(function (done) {
  154. return this.client.emit('leaveDoc', this.other_doc_id, error => {
  155. if (error != null) {
  156. throw error
  157. }
  158. return done()
  159. })
  160. })
  161. return it('should trigger a low level message only', function () {
  162. return sinon.assert.calledWith(
  163. logger.debug,
  164. sinon.match.any,
  165. 'ignoring request from client to leave room it is not in'
  166. )
  167. })
  168. })
  169. })
  170. })