DocumentUpdaterControllerTests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. 'settings-sharelatex': (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 = 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 = sinon.stub()
  132. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  133. this.io,
  134. 'applied-ops',
  135. JSON.stringify(this.message)
  136. )
  137. })
  138. return it('should process the error', function () {
  139. return this.EditorUpdatesController._processErrorFromDocumentUpdater
  140. .calledWith(this.io, this.doc_id, this.message.error)
  141. .should.equal(true)
  142. })
  143. })
  144. })
  145. describe('_applyUpdateFromDocumentUpdater', function () {
  146. beforeEach(function () {
  147. this.sourceClient = new MockClient()
  148. this.otherClients = [new MockClient(), new MockClient()]
  149. this.update = {
  150. op: [{ t: 'foo', p: 12 }],
  151. meta: { source: this.sourceClient.publicId },
  152. v: (this.version = 42),
  153. doc: this.doc_id
  154. }
  155. return (this.io.sockets = {
  156. clients: sinon
  157. .stub()
  158. .returns([
  159. this.sourceClient,
  160. ...Array.from(this.otherClients),
  161. this.sourceClient
  162. ])
  163. })
  164. }) // include a duplicate client
  165. describe('normally', function () {
  166. beforeEach(function () {
  167. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater(
  168. this.io,
  169. this.doc_id,
  170. this.update
  171. )
  172. })
  173. it('should send a version bump to the source client', function () {
  174. this.sourceClient.emit
  175. .calledWith('otUpdateApplied', { v: this.version, doc: this.doc_id })
  176. .should.equal(true)
  177. return this.sourceClient.emit.calledOnce.should.equal(true)
  178. })
  179. it('should get the clients connected to the document', function () {
  180. return this.io.sockets.clients
  181. .calledWith(this.doc_id)
  182. .should.equal(true)
  183. })
  184. return it('should send the full update to the other clients', function () {
  185. return Array.from(this.otherClients).map((client) =>
  186. client.emit
  187. .calledWith('otUpdateApplied', this.update)
  188. .should.equal(true)
  189. )
  190. })
  191. })
  192. return describe('with a duplicate op', function () {
  193. beforeEach(function () {
  194. this.update.dup = true
  195. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater(
  196. this.io,
  197. this.doc_id,
  198. this.update
  199. )
  200. })
  201. it('should send a version bump to the source client as usual', function () {
  202. return this.sourceClient.emit
  203. .calledWith('otUpdateApplied', { v: this.version, doc: this.doc_id })
  204. .should.equal(true)
  205. })
  206. return it("should not send anything to the other clients (they've already had the op)", function () {
  207. return Array.from(this.otherClients).map((client) =>
  208. client.emit.calledWith('otUpdateApplied').should.equal(false)
  209. )
  210. })
  211. })
  212. })
  213. return describe('_processErrorFromDocumentUpdater', function () {
  214. beforeEach(function () {
  215. this.clients = [new MockClient(), new MockClient()]
  216. this.io.sockets = { clients: sinon.stub().returns(this.clients) }
  217. return this.EditorUpdatesController._processErrorFromDocumentUpdater(
  218. this.io,
  219. this.doc_id,
  220. 'Something went wrong'
  221. )
  222. })
  223. it('should log a warning', function () {
  224. return this.logger.warn.called.should.equal(true)
  225. })
  226. return it('should disconnect all clients in that document', function () {
  227. this.io.sockets.clients.calledWith(this.doc_id).should.equal(true)
  228. return Array.from(this.clients).map((client) =>
  229. client.disconnect.called.should.equal(true)
  230. )
  231. })
  232. })
  233. })