LeaveDocTests.js 5.0 KB

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