RoomManagerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. promise/param-names,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const chai = require('chai')
  14. const { expect } = chai
  15. const should = chai.should()
  16. const sinon = require('sinon')
  17. const modulePath = '../../../app/js/RoomManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. describe('RoomManager', function () {
  20. beforeEach(function () {
  21. this.project_id = 'project-id-123'
  22. this.doc_id = 'doc-id-456'
  23. this.other_doc_id = 'doc-id-789'
  24. this.client = { namespace: { name: '' }, id: 'first-client' }
  25. this.RoomManager = SandboxedModule.require(modulePath, {
  26. requires: {
  27. 'settings-sharelatex': (this.settings = {}),
  28. 'logger-sharelatex': (this.logger = {
  29. log: sinon.stub(),
  30. warn: sinon.stub(),
  31. error: sinon.stub()
  32. }),
  33. 'metrics-sharelatex': (this.metrics = { gauge: sinon.stub() })
  34. }
  35. })
  36. this.RoomManager._clientsInRoom = sinon.stub()
  37. this.RoomManager._clientAlreadyInRoom = sinon.stub()
  38. this.RoomEvents = this.RoomManager.eventSource()
  39. sinon.spy(this.RoomEvents, 'emit')
  40. return sinon.spy(this.RoomEvents, 'once')
  41. })
  42. describe('emitOnCompletion', function () {
  43. return describe('when a subscribe errors', function () {
  44. afterEach(function () {
  45. return process.removeListener('unhandledRejection', this.onUnhandled)
  46. })
  47. beforeEach(function (done) {
  48. this.onUnhandled = (error) => {
  49. this.unhandledError = error
  50. return done(new Error(`unhandledRejection: ${error.message}`))
  51. }
  52. process.on('unhandledRejection', this.onUnhandled)
  53. let reject
  54. const subscribePromise = new Promise((_, r) => (reject = r))
  55. const promises = [subscribePromise]
  56. const eventName = 'project-subscribed-123'
  57. this.RoomEvents.once(eventName, () => setTimeout(done, 100))
  58. this.RoomManager.emitOnCompletion(promises, eventName)
  59. return setTimeout(() => reject(new Error('subscribe failed')))
  60. })
  61. return it('should keep going', function () {
  62. return expect(this.unhandledError).to.not.exist
  63. })
  64. })
  65. })
  66. describe('joinProject', function () {
  67. describe('when the project room is empty', function () {
  68. beforeEach(function (done) {
  69. this.RoomManager._clientsInRoom
  70. .withArgs(this.client, this.project_id)
  71. .onFirstCall()
  72. .returns(0)
  73. this.client.join = sinon.stub()
  74. this.callback = sinon.stub()
  75. this.RoomEvents.on('project-active', (id) => {
  76. return setTimeout(() => {
  77. return this.RoomEvents.emit(`project-subscribed-${id}`)
  78. }, 100)
  79. })
  80. return this.RoomManager.joinProject(
  81. this.client,
  82. this.project_id,
  83. (err) => {
  84. this.callback(err)
  85. return done()
  86. }
  87. )
  88. })
  89. it("should emit a 'project-active' event with the id", function () {
  90. return this.RoomEvents.emit
  91. .calledWithExactly('project-active', this.project_id)
  92. .should.equal(true)
  93. })
  94. it("should listen for the 'project-subscribed-id' event", function () {
  95. return this.RoomEvents.once
  96. .calledWith(`project-subscribed-${this.project_id}`)
  97. .should.equal(true)
  98. })
  99. return it('should join the room using the id', function () {
  100. return this.client.join
  101. .calledWithExactly(this.project_id)
  102. .should.equal(true)
  103. })
  104. })
  105. return describe('when there are other clients in the project room', function () {
  106. beforeEach(function (done) {
  107. this.RoomManager._clientsInRoom
  108. .withArgs(this.client, this.project_id)
  109. .onFirstCall()
  110. .returns(123)
  111. .onSecondCall()
  112. .returns(124)
  113. this.client.join = sinon.stub()
  114. this.RoomManager.joinProject(this.client, this.project_id, done)
  115. })
  116. it('should join the room using the id', function () {
  117. return this.client.join.called.should.equal(true)
  118. })
  119. return it('should not emit any events', function () {
  120. return this.RoomEvents.emit.called.should.equal(false)
  121. })
  122. })
  123. })
  124. describe('joinDoc', function () {
  125. describe('when the doc room is empty', function () {
  126. beforeEach(function (done) {
  127. this.RoomManager._clientsInRoom
  128. .withArgs(this.client, this.doc_id)
  129. .onFirstCall()
  130. .returns(0)
  131. this.client.join = sinon.stub()
  132. this.callback = sinon.stub()
  133. this.RoomEvents.on('doc-active', (id) => {
  134. return setTimeout(() => {
  135. return this.RoomEvents.emit(`doc-subscribed-${id}`)
  136. }, 100)
  137. })
  138. return this.RoomManager.joinDoc(this.client, this.doc_id, (err) => {
  139. this.callback(err)
  140. return done()
  141. })
  142. })
  143. it("should emit a 'doc-active' event with the id", function () {
  144. return this.RoomEvents.emit
  145. .calledWithExactly('doc-active', this.doc_id)
  146. .should.equal(true)
  147. })
  148. it("should listen for the 'doc-subscribed-id' event", function () {
  149. return this.RoomEvents.once
  150. .calledWith(`doc-subscribed-${this.doc_id}`)
  151. .should.equal(true)
  152. })
  153. return it('should join the room using the id', function () {
  154. return this.client.join
  155. .calledWithExactly(this.doc_id)
  156. .should.equal(true)
  157. })
  158. })
  159. return describe('when there are other clients in the doc room', function () {
  160. beforeEach(function (done) {
  161. this.RoomManager._clientsInRoom
  162. .withArgs(this.client, this.doc_id)
  163. .onFirstCall()
  164. .returns(123)
  165. .onSecondCall()
  166. .returns(124)
  167. this.client.join = sinon.stub()
  168. this.RoomManager.joinDoc(this.client, this.doc_id, done)
  169. })
  170. it('should join the room using the id', function () {
  171. return this.client.join.called.should.equal(true)
  172. })
  173. return it('should not emit any events', function () {
  174. return this.RoomEvents.emit.called.should.equal(false)
  175. })
  176. })
  177. })
  178. describe('leaveDoc', function () {
  179. describe('when doc room will be empty after this client has left', function () {
  180. beforeEach(function () {
  181. this.RoomManager._clientAlreadyInRoom
  182. .withArgs(this.client, this.doc_id)
  183. .returns(true)
  184. this.RoomManager._clientsInRoom
  185. .withArgs(this.client, this.doc_id)
  186. .onCall(0)
  187. .returns(0)
  188. this.client.leave = sinon.stub()
  189. return this.RoomManager.leaveDoc(this.client, this.doc_id)
  190. })
  191. it('should leave the room using the id', function () {
  192. return this.client.leave
  193. .calledWithExactly(this.doc_id)
  194. .should.equal(true)
  195. })
  196. return it("should emit a 'doc-empty' event with the id", function () {
  197. return this.RoomEvents.emit
  198. .calledWithExactly('doc-empty', this.doc_id)
  199. .should.equal(true)
  200. })
  201. })
  202. describe('when there are other clients in the doc room', function () {
  203. beforeEach(function () {
  204. this.RoomManager._clientAlreadyInRoom
  205. .withArgs(this.client, this.doc_id)
  206. .returns(true)
  207. this.RoomManager._clientsInRoom
  208. .withArgs(this.client, this.doc_id)
  209. .onCall(0)
  210. .returns(123)
  211. this.client.leave = sinon.stub()
  212. return this.RoomManager.leaveDoc(this.client, this.doc_id)
  213. })
  214. it('should leave the room using the id', function () {
  215. return this.client.leave
  216. .calledWithExactly(this.doc_id)
  217. .should.equal(true)
  218. })
  219. return it('should not emit any events', function () {
  220. return this.RoomEvents.emit.called.should.equal(false)
  221. })
  222. })
  223. return describe('when the client is not in the doc room', function () {
  224. beforeEach(function () {
  225. this.RoomManager._clientAlreadyInRoom
  226. .withArgs(this.client, this.doc_id)
  227. .returns(false)
  228. this.RoomManager._clientsInRoom
  229. .withArgs(this.client, this.doc_id)
  230. .onCall(0)
  231. .returns(0)
  232. this.client.leave = sinon.stub()
  233. return this.RoomManager.leaveDoc(this.client, this.doc_id)
  234. })
  235. it('should not leave the room', function () {
  236. return this.client.leave.called.should.equal(false)
  237. })
  238. return it('should not emit any events', function () {
  239. return this.RoomEvents.emit.called.should.equal(false)
  240. })
  241. })
  242. })
  243. return describe('leaveProjectAndDocs', function () {
  244. return describe('when the client is connected to the project and multiple docs', function () {
  245. beforeEach(function () {
  246. this.RoomManager._roomsClientIsIn = sinon
  247. .stub()
  248. .returns([this.project_id, this.doc_id, this.other_doc_id])
  249. this.client.join = sinon.stub()
  250. return (this.client.leave = sinon.stub())
  251. })
  252. describe('when this is the only client connected', function () {
  253. beforeEach(function (done) {
  254. // first call is for the join,
  255. // second for the leave
  256. this.RoomManager._clientsInRoom
  257. .withArgs(this.client, this.doc_id)
  258. .onCall(0)
  259. .returns(0)
  260. .onCall(1)
  261. .returns(0)
  262. this.RoomManager._clientsInRoom
  263. .withArgs(this.client, this.other_doc_id)
  264. .onCall(0)
  265. .returns(0)
  266. .onCall(1)
  267. .returns(0)
  268. this.RoomManager._clientsInRoom
  269. .withArgs(this.client, this.project_id)
  270. .onCall(0)
  271. .returns(0)
  272. .onCall(1)
  273. .returns(0)
  274. this.RoomManager._clientAlreadyInRoom
  275. .withArgs(this.client, this.doc_id)
  276. .returns(true)
  277. .withArgs(this.client, this.other_doc_id)
  278. .returns(true)
  279. .withArgs(this.client, this.project_id)
  280. .returns(true)
  281. this.RoomEvents.on('project-active', (id) => {
  282. return setTimeout(() => {
  283. return this.RoomEvents.emit(`project-subscribed-${id}`)
  284. }, 100)
  285. })
  286. this.RoomEvents.on('doc-active', (id) => {
  287. return setTimeout(() => {
  288. return this.RoomEvents.emit(`doc-subscribed-${id}`)
  289. }, 100)
  290. })
  291. // put the client in the rooms
  292. return this.RoomManager.joinProject(
  293. this.client,
  294. this.project_id,
  295. () => {
  296. return this.RoomManager.joinDoc(this.client, this.doc_id, () => {
  297. return this.RoomManager.joinDoc(
  298. this.client,
  299. this.other_doc_id,
  300. () => {
  301. // now leave the project
  302. this.RoomManager.leaveProjectAndDocs(this.client)
  303. return done()
  304. }
  305. )
  306. })
  307. }
  308. )
  309. })
  310. it('should leave all the docs', function () {
  311. this.client.leave.calledWithExactly(this.doc_id).should.equal(true)
  312. return this.client.leave
  313. .calledWithExactly(this.other_doc_id)
  314. .should.equal(true)
  315. })
  316. it('should leave the project', function () {
  317. return this.client.leave
  318. .calledWithExactly(this.project_id)
  319. .should.equal(true)
  320. })
  321. it("should emit a 'doc-empty' event with the id for each doc", function () {
  322. this.RoomEvents.emit
  323. .calledWithExactly('doc-empty', this.doc_id)
  324. .should.equal(true)
  325. return this.RoomEvents.emit
  326. .calledWithExactly('doc-empty', this.other_doc_id)
  327. .should.equal(true)
  328. })
  329. return it("should emit a 'project-empty' event with the id for the project", function () {
  330. return this.RoomEvents.emit
  331. .calledWithExactly('project-empty', this.project_id)
  332. .should.equal(true)
  333. })
  334. })
  335. return describe('when other clients are still connected', function () {
  336. beforeEach(function () {
  337. this.RoomManager._clientsInRoom
  338. .withArgs(this.client, this.doc_id)
  339. .onFirstCall()
  340. .returns(123)
  341. .onSecondCall()
  342. .returns(122)
  343. this.RoomManager._clientsInRoom
  344. .withArgs(this.client, this.other_doc_id)
  345. .onFirstCall()
  346. .returns(123)
  347. .onSecondCall()
  348. .returns(122)
  349. this.RoomManager._clientsInRoom
  350. .withArgs(this.client, this.project_id)
  351. .onFirstCall()
  352. .returns(123)
  353. .onSecondCall()
  354. .returns(122)
  355. this.RoomManager._clientAlreadyInRoom
  356. .withArgs(this.client, this.doc_id)
  357. .returns(true)
  358. .withArgs(this.client, this.other_doc_id)
  359. .returns(true)
  360. .withArgs(this.client, this.project_id)
  361. .returns(true)
  362. return this.RoomManager.leaveProjectAndDocs(this.client)
  363. })
  364. it('should leave all the docs', function () {
  365. this.client.leave.calledWithExactly(this.doc_id).should.equal(true)
  366. return this.client.leave
  367. .calledWithExactly(this.other_doc_id)
  368. .should.equal(true)
  369. })
  370. it('should leave the project', function () {
  371. return this.client.leave
  372. .calledWithExactly(this.project_id)
  373. .should.equal(true)
  374. })
  375. return it('should not emit any events', function () {
  376. return this.RoomEvents.emit.called.should.equal(false)
  377. })
  378. })
  379. })
  380. })
  381. })