EditorRealTimeControllerTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const modulePath = require('path').join(
  16. __dirname,
  17. '../../../../app/src/Features/Editor/EditorRealTimeController'
  18. )
  19. describe('EditorRealTimeController', function () {
  20. beforeEach(function () {
  21. this.rclient = { publish: sinon.stub() }
  22. this.Metrics = { summary: sinon.stub() }
  23. this.EditorRealTimeController = SandboxedModule.require(modulePath, {
  24. requires: {
  25. '../../infrastructure/RedisWrapper': {
  26. client: () => this.rclient,
  27. },
  28. '../../infrastructure/Server': {
  29. io: (this.io = {}),
  30. },
  31. '@overleaf/settings': { redis: {} },
  32. '@overleaf/metrics': this.Metrics,
  33. crypto: (this.crypto = {
  34. randomBytes: sinon
  35. .stub()
  36. .withArgs(4)
  37. .returns(Buffer.from([0x1, 0x2, 0x3, 0x4])),
  38. }),
  39. os: (this.os = { hostname: sinon.stub().returns('somehost') }),
  40. },
  41. })
  42. this.room_id = 'room-id'
  43. this.message = 'message-to-editor'
  44. return (this.payload = ['argument one', 42])
  45. })
  46. describe('emitToRoom', function () {
  47. beforeEach(function () {
  48. this.message_id = 'web:somehost:01020304-0'
  49. return this.EditorRealTimeController.emitToRoom(
  50. this.room_id,
  51. this.message,
  52. ...Array.from(this.payload)
  53. )
  54. })
  55. it('should publish the message to redis', function () {
  56. return this.rclient.publish
  57. .calledWith(
  58. 'editor-events',
  59. JSON.stringify({
  60. room_id: this.room_id,
  61. message: this.message,
  62. payload: this.payload,
  63. _id: this.message_id,
  64. })
  65. )
  66. .should.equal(true)
  67. })
  68. it('should track the payload size', function () {
  69. this.Metrics.summary
  70. .calledWith(
  71. 'redis.publish.editor-events',
  72. JSON.stringify({
  73. room_id: this.room_id,
  74. message: this.message,
  75. payload: this.payload,
  76. _id: this.message_id,
  77. }).length
  78. )
  79. .should.equal(true)
  80. })
  81. })
  82. describe('emitToAll', function () {
  83. beforeEach(function () {
  84. this.EditorRealTimeController.emitToRoom = sinon.stub()
  85. return this.EditorRealTimeController.emitToAll(
  86. this.message,
  87. ...Array.from(this.payload)
  88. )
  89. })
  90. it("should emit to the room 'all'", function () {
  91. return this.EditorRealTimeController.emitToRoom
  92. .calledWith('all', this.message, ...Array.from(this.payload))
  93. .should.equal(true)
  94. })
  95. })
  96. })