EditorRealTimeControllerTests.js 3.0 KB

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