DocumentUpdaterControllerTests.js 8.2 KB

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