DocumentUpdaterControllerTests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. const modulePath = require('path').join(
  15. __dirname,
  16. '../../../app/js/DocumentUpdaterController'
  17. )
  18. const MockClient = require('./helpers/MockClient')
  19. describe('DocumentUpdaterController', function () {
  20. beforeEach(function () {
  21. this.project_id = 'project-id-123'
  22. this.doc_id = 'doc-id-123'
  23. this.callback = sinon.stub()
  24. this.io = { mock: 'socket.io' }
  25. this.rclient = []
  26. this.RoomEvents = { on: sinon.stub() }
  27. this.EditorUpdatesController = SandboxedModule.require(modulePath, {
  28. requires: {
  29. '@overleaf/settings': (this.settings = {
  30. redis: {
  31. documentupdater: {
  32. key_schema: {
  33. pendingUpdates({ doc_id: docId }) {
  34. return `PendingUpdates:${docId}`
  35. },
  36. },
  37. },
  38. pubsub: null,
  39. },
  40. }),
  41. './RedisClientManager': {
  42. createClientList: () => {
  43. this.redis = {
  44. createClient: name => {
  45. let rclientStub
  46. this.rclient.push((rclientStub = { name }))
  47. return rclientStub
  48. },
  49. }
  50. },
  51. },
  52. './SafeJsonParse': (this.SafeJsonParse = {
  53. parse: (data, cb) => cb(null, JSON.parse(data)),
  54. }),
  55. './EventLogger': (this.EventLogger = { checkEventOrder: sinon.stub() }),
  56. './HealthCheckManager': { check: sinon.stub() },
  57. '@overleaf/metrics': (this.metrics = { inc: sinon.stub() }),
  58. './RoomManager': (this.RoomManager = {
  59. eventSource: sinon.stub().returns(this.RoomEvents),
  60. }),
  61. './ChannelManager': (this.ChannelManager = {}),
  62. },
  63. })
  64. })
  65. describe('listenForUpdatesFromDocumentUpdater', function () {
  66. beforeEach(function () {
  67. this.rclient.length = 0 // clear any existing clients
  68. this.EditorUpdatesController.rclientList = [
  69. this.redis.createClient('first'),
  70. this.redis.createClient('second'),
  71. ]
  72. this.rclient[0].subscribe = sinon.stub()
  73. this.rclient[0].on = sinon.stub()
  74. this.rclient[1].subscribe = sinon.stub()
  75. this.rclient[1].on = sinon.stub()
  76. this.EditorUpdatesController.listenForUpdatesFromDocumentUpdater()
  77. })
  78. it('should subscribe to the doc-updater stream', function () {
  79. this.rclient[0].subscribe.calledWith('applied-ops').should.equal(true)
  80. })
  81. it('should register a callback to handle updates', function () {
  82. this.rclient[0].on.calledWith('message').should.equal(true)
  83. })
  84. it('should subscribe to any additional doc-updater stream', function () {
  85. this.rclient[1].subscribe.calledWith('applied-ops').should.equal(true)
  86. this.rclient[1].on.calledWith('message').should.equal(true)
  87. })
  88. })
  89. describe('_processMessageFromDocumentUpdater', function () {
  90. describe('with bad JSON', function () {
  91. beforeEach(function () {
  92. this.SafeJsonParse.parse = sinon
  93. .stub()
  94. .callsArgWith(1, new Error('oops'))
  95. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  96. this.io,
  97. 'applied-ops',
  98. 'blah'
  99. )
  100. })
  101. it('should log an error', function () {
  102. return this.logger.error.called.should.equal(true)
  103. })
  104. })
  105. describe('with update', function () {
  106. beforeEach(function () {
  107. this.message = {
  108. doc_id: this.doc_id,
  109. op: { t: 'foo', p: 12 },
  110. }
  111. this.EditorUpdatesController._applyUpdateFromDocumentUpdater =
  112. sinon.stub()
  113. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  114. this.io,
  115. 'applied-ops',
  116. JSON.stringify(this.message)
  117. )
  118. })
  119. it('should apply the update', function () {
  120. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater
  121. .calledWith(this.io, this.doc_id, this.message.op)
  122. .should.equal(true)
  123. })
  124. })
  125. describe('with error', function () {
  126. beforeEach(function () {
  127. this.message = {
  128. doc_id: this.doc_id,
  129. error: 'Something went wrong',
  130. }
  131. this.EditorUpdatesController._processErrorFromDocumentUpdater =
  132. sinon.stub()
  133. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  134. this.io,
  135. 'applied-ops',
  136. JSON.stringify(this.message)
  137. )
  138. })
  139. return it('should process the error', function () {
  140. return this.EditorUpdatesController._processErrorFromDocumentUpdater
  141. .calledWith(this.io, this.doc_id, this.message.error)
  142. .should.equal(true)
  143. })
  144. })
  145. })
  146. describe('_applyUpdateFromDocumentUpdater', function () {
  147. beforeEach(function () {
  148. this.sourceClient = new MockClient()
  149. this.otherClients = [new MockClient(), new MockClient()]
  150. this.update = {
  151. op: [{ t: 'foo', p: 12 }],
  152. meta: { source: this.sourceClient.publicId },
  153. v: (this.version = 42),
  154. doc: this.doc_id,
  155. }
  156. return (this.io.sockets = {
  157. clients: sinon
  158. .stub()
  159. .returns([
  160. this.sourceClient,
  161. ...Array.from(this.otherClients),
  162. this.sourceClient,
  163. ]),
  164. })
  165. }) // include a duplicate client
  166. describe('normally', function () {
  167. beforeEach(function () {
  168. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater(
  169. this.io,
  170. this.doc_id,
  171. this.update
  172. )
  173. })
  174. it('should send a version bump to the source client', function () {
  175. this.sourceClient.emit
  176. .calledWith('otUpdateApplied', { v: this.version, doc: this.doc_id })
  177. .should.equal(true)
  178. return this.sourceClient.emit.calledOnce.should.equal(true)
  179. })
  180. it('should get the clients connected to the document', function () {
  181. return this.io.sockets.clients
  182. .calledWith(this.doc_id)
  183. .should.equal(true)
  184. })
  185. return it('should send the full update to the other clients', function () {
  186. return Array.from(this.otherClients).map(client =>
  187. client.emit
  188. .calledWith('otUpdateApplied', this.update)
  189. .should.equal(true)
  190. )
  191. })
  192. })
  193. return describe('with a duplicate op', function () {
  194. beforeEach(function () {
  195. this.update.dup = true
  196. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater(
  197. this.io,
  198. this.doc_id,
  199. this.update
  200. )
  201. })
  202. it('should send a version bump to the source client as usual', function () {
  203. return this.sourceClient.emit
  204. .calledWith('otUpdateApplied', { v: this.version, doc: this.doc_id })
  205. .should.equal(true)
  206. })
  207. return it("should not send anything to the other clients (they've already had the op)", function () {
  208. return Array.from(this.otherClients).map(client =>
  209. client.emit.calledWith('otUpdateApplied').should.equal(false)
  210. )
  211. })
  212. })
  213. })
  214. return describe('_processErrorFromDocumentUpdater', function () {
  215. beforeEach(function () {
  216. this.clients = [new MockClient(), new MockClient()]
  217. this.io.sockets = { clients: sinon.stub().returns(this.clients) }
  218. return this.EditorUpdatesController._processErrorFromDocumentUpdater(
  219. this.io,
  220. this.doc_id,
  221. 'Something went wrong'
  222. )
  223. })
  224. it('should log a warning', function () {
  225. return this.logger.warn.called.should.equal(true)
  226. })
  227. return it('should disconnect all clients in that document', function () {
  228. this.io.sockets.clients.calledWith(this.doc_id).should.equal(true)
  229. return Array.from(this.clients).map(client =>
  230. client.disconnect.called.should.equal(true)
  231. )
  232. })
  233. })
  234. })