DocumentUpdaterController.test.js 8.0 KB

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