DocumentUpdaterControllerTests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 = {
  58. inc: sinon.stub(),
  59. histogram: sinon.stub(),
  60. }),
  61. './RoomManager': (this.RoomManager = {
  62. eventSource: sinon.stub().returns(this.RoomEvents),
  63. }),
  64. './ChannelManager': (this.ChannelManager = {}),
  65. },
  66. })
  67. })
  68. describe('listenForUpdatesFromDocumentUpdater', function () {
  69. beforeEach(function () {
  70. this.rclient.length = 0 // clear any existing clients
  71. this.EditorUpdatesController.rclientList = [
  72. this.redis.createClient('first'),
  73. this.redis.createClient('second'),
  74. ]
  75. this.rclient[0].subscribe = sinon.stub()
  76. this.rclient[0].on = sinon.stub()
  77. this.rclient[1].subscribe = sinon.stub()
  78. this.rclient[1].on = sinon.stub()
  79. this.EditorUpdatesController.listenForUpdatesFromDocumentUpdater()
  80. })
  81. it('should subscribe to the doc-updater stream', function () {
  82. this.rclient[0].subscribe.calledWith('applied-ops').should.equal(true)
  83. })
  84. it('should register a callback to handle updates', function () {
  85. this.rclient[0].on.calledWith('message').should.equal(true)
  86. })
  87. it('should subscribe to any additional doc-updater stream', function () {
  88. this.rclient[1].subscribe.calledWith('applied-ops').should.equal(true)
  89. this.rclient[1].on.calledWith('message').should.equal(true)
  90. })
  91. })
  92. describe('_processMessageFromDocumentUpdater', function () {
  93. describe('with bad JSON', function () {
  94. beforeEach(function () {
  95. this.SafeJsonParse.parse = sinon
  96. .stub()
  97. .callsArgWith(1, new Error('oops'))
  98. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  99. this.io,
  100. 'applied-ops',
  101. 'blah'
  102. )
  103. })
  104. it('should log an error', function () {
  105. return this.logger.error.called.should.equal(true)
  106. })
  107. })
  108. describe('with update', function () {
  109. beforeEach(function () {
  110. this.message = {
  111. doc_id: this.doc_id,
  112. op: { t: 'foo', p: 12 },
  113. }
  114. this.EditorUpdatesController._applyUpdateFromDocumentUpdater =
  115. sinon.stub()
  116. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  117. this.io,
  118. 'applied-ops',
  119. JSON.stringify(this.message)
  120. )
  121. })
  122. it('should apply the update', function () {
  123. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater
  124. .calledWith(this.io, this.doc_id, this.message.op)
  125. .should.equal(true)
  126. })
  127. })
  128. describe('with error', function () {
  129. beforeEach(function () {
  130. this.message = {
  131. doc_id: this.doc_id,
  132. error: 'Something went wrong',
  133. }
  134. this.EditorUpdatesController._processErrorFromDocumentUpdater =
  135. sinon.stub()
  136. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  137. this.io,
  138. 'applied-ops',
  139. JSON.stringify(this.message)
  140. )
  141. })
  142. return it('should process the error', function () {
  143. return this.EditorUpdatesController._processErrorFromDocumentUpdater
  144. .calledWith(this.io, this.doc_id, this.message.error)
  145. .should.equal(true)
  146. })
  147. })
  148. })
  149. describe('_applyUpdateFromDocumentUpdater', function () {
  150. beforeEach(function () {
  151. this.sourceClient = new MockClient()
  152. this.otherClients = [new MockClient(), new MockClient()]
  153. this.update = {
  154. op: [{ t: 'foo', p: 12 }],
  155. meta: { source: this.sourceClient.publicId },
  156. v: (this.version = 42),
  157. doc: this.doc_id,
  158. }
  159. return (this.io.sockets = {
  160. clients: sinon
  161. .stub()
  162. .returns([
  163. this.sourceClient,
  164. ...Array.from(this.otherClients),
  165. this.sourceClient,
  166. ]),
  167. })
  168. }) // include a duplicate client
  169. describe('normally', function () {
  170. beforeEach(function () {
  171. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater(
  172. this.io,
  173. this.doc_id,
  174. this.update
  175. )
  176. })
  177. it('should send a version bump to the source client', function () {
  178. this.sourceClient.emit
  179. .calledWith('otUpdateApplied', { v: this.version, doc: this.doc_id })
  180. .should.equal(true)
  181. return this.sourceClient.emit.calledOnce.should.equal(true)
  182. })
  183. it('should get the clients connected to the document', function () {
  184. return this.io.sockets.clients
  185. .calledWith(this.doc_id)
  186. .should.equal(true)
  187. })
  188. return it('should send the full update to the other clients', function () {
  189. return Array.from(this.otherClients).map(client =>
  190. client.emit
  191. .calledWith('otUpdateApplied', this.update)
  192. .should.equal(true)
  193. )
  194. })
  195. })
  196. return describe('with a duplicate op', function () {
  197. beforeEach(function () {
  198. this.update.dup = true
  199. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater(
  200. this.io,
  201. this.doc_id,
  202. this.update
  203. )
  204. })
  205. it('should send a version bump to the source client as usual', function () {
  206. return this.sourceClient.emit
  207. .calledWith('otUpdateApplied', { v: this.version, doc: this.doc_id })
  208. .should.equal(true)
  209. })
  210. return it("should not send anything to the other clients (they've already had the op)", function () {
  211. return Array.from(this.otherClients).map(client =>
  212. client.emit.calledWith('otUpdateApplied').should.equal(false)
  213. )
  214. })
  215. })
  216. })
  217. return describe('_processErrorFromDocumentUpdater', function () {
  218. beforeEach(function () {
  219. this.clients = [new MockClient(), new MockClient()]
  220. this.io.sockets = { clients: sinon.stub().returns(this.clients) }
  221. return this.EditorUpdatesController._processErrorFromDocumentUpdater(
  222. this.io,
  223. this.doc_id,
  224. 'Something went wrong'
  225. )
  226. })
  227. it('should log a warning', function () {
  228. return this.logger.warn.called.should.equal(true)
  229. })
  230. return it('should disconnect all clients in that document', function () {
  231. this.io.sockets.clients.calledWith(this.doc_id).should.equal(true)
  232. return Array.from(this.clients).map(client =>
  233. client.disconnect.called.should.equal(true)
  234. )
  235. })
  236. })
  237. })