DocumentUpdaterControllerTests.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. require('chai').should()
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../app/js/DocumentUpdaterController'
  19. )
  20. const MockClient = require('./helpers/MockClient')
  21. describe('DocumentUpdaterController', function () {
  22. beforeEach(function () {
  23. this.project_id = 'project-id-123'
  24. this.doc_id = 'doc-id-123'
  25. this.callback = sinon.stub()
  26. this.io = { mock: 'socket.io' }
  27. this.rclient = []
  28. this.RoomEvents = { on: sinon.stub() }
  29. return (this.EditorUpdatesController = SandboxedModule.require(modulePath, {
  30. requires: {
  31. 'logger-sharelatex': (this.logger = {
  32. error: sinon.stub(),
  33. log: sinon.stub(),
  34. warn: sinon.stub()
  35. }),
  36. 'settings-sharelatex': (this.settings = {
  37. redis: {
  38. documentupdater: {
  39. key_schema: {
  40. pendingUpdates({ doc_id }) {
  41. return `PendingUpdates:${doc_id}`
  42. }
  43. }
  44. },
  45. pubsub: null
  46. }
  47. }),
  48. 'redis-sharelatex': (this.redis = {
  49. createClient: (name) => {
  50. let rclientStub
  51. this.rclient.push((rclientStub = { name }))
  52. return rclientStub
  53. }
  54. }),
  55. './SafeJsonParse': (this.SafeJsonParse = {
  56. parse: (data, cb) => cb(null, JSON.parse(data))
  57. }),
  58. './EventLogger': (this.EventLogger = { checkEventOrder: sinon.stub() }),
  59. './HealthCheckManager': { check: sinon.stub() },
  60. 'metrics-sharelatex': (this.metrics = { inc: sinon.stub() }),
  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. return this.EditorUpdatesController.listenForUpdatesFromDocumentUpdater()
  80. })
  81. it('should subscribe to the doc-updater stream', function () {
  82. return this.rclient[0].subscribe
  83. .calledWith('applied-ops')
  84. .should.equal(true)
  85. })
  86. it('should register a callback to handle updates', function () {
  87. return this.rclient[0].on.calledWith('message').should.equal(true)
  88. })
  89. return it('should subscribe to any additional doc-updater stream', function () {
  90. this.rclient[1].subscribe.calledWith('applied-ops').should.equal(true)
  91. return this.rclient[1].on.calledWith('message').should.equal(true)
  92. })
  93. })
  94. describe('_processMessageFromDocumentUpdater', function () {
  95. describe('with bad JSON', function () {
  96. beforeEach(function () {
  97. this.SafeJsonParse.parse = sinon
  98. .stub()
  99. .callsArgWith(1, new Error('oops'))
  100. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  101. this.io,
  102. 'applied-ops',
  103. 'blah'
  104. )
  105. })
  106. return it('should log an error', function () {
  107. return this.logger.error.called.should.equal(true)
  108. })
  109. })
  110. describe('with update', function () {
  111. beforeEach(function () {
  112. this.message = {
  113. doc_id: this.doc_id,
  114. op: { t: 'foo', p: 12 }
  115. }
  116. this.EditorUpdatesController._applyUpdateFromDocumentUpdater = sinon.stub()
  117. return this.EditorUpdatesController._processMessageFromDocumentUpdater(
  118. this.io,
  119. 'applied-ops',
  120. JSON.stringify(this.message)
  121. )
  122. })
  123. return it('should apply the update', function () {
  124. return this.EditorUpdatesController._applyUpdateFromDocumentUpdater
  125. .calledWith(this.io, this.doc_id, this.message.op)
  126. .should.equal(true)
  127. })
  128. })
  129. return describe('with error', function () {
  130. beforeEach(function () {
  131. this.message = {
  132. doc_id: this.doc_id,
  133. error: 'Something went wrong'
  134. }
  135. this.EditorUpdatesController._processErrorFromDocumentUpdater = 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. })