DocumentUpdaterControllerTests.coffee 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/DocumentUpdaterController'
  5. MockClient = require "./helpers/MockClient"
  6. describe "DocumentUpdaterController", ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @doc_id = "doc-id-123"
  10. @callback = sinon.stub()
  11. @io = { "mock": "socket.io" }
  12. @rclient = []
  13. @RoomEvents = { on: sinon.stub() }
  14. @EditorUpdatesController = SandboxedModule.require modulePath, requires:
  15. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
  16. "settings-sharelatex": @settings =
  17. redis:
  18. documentupdater:
  19. key_schema:
  20. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  21. pubsub: null
  22. "redis-sharelatex" : @redis =
  23. createClient: (name) =>
  24. @rclient.push(rclientStub = {name:name})
  25. return rclientStub
  26. "./SafeJsonParse": @SafeJsonParse =
  27. parse: (data, cb) => cb null, JSON.parse(data)
  28. "./EventLogger": @EventLogger = {checkEventOrder: sinon.stub()}
  29. "./HealthCheckManager": {check: sinon.stub()}
  30. "metrics-sharelatex": @metrics = {inc: sinon.stub()}
  31. "./RoomManager" : @RoomManager = { eventSource: sinon.stub().returns @RoomEvents}
  32. "./ChannelManager": @ChannelManager = {}
  33. describe "listenForUpdatesFromDocumentUpdater", ->
  34. beforeEach ->
  35. @rclient.length = 0 # clear any existing clients
  36. @EditorUpdatesController.rclientList = [@redis.createClient("first"), @redis.createClient("second")]
  37. @rclient[0].subscribe = sinon.stub()
  38. @rclient[0].on = sinon.stub()
  39. @rclient[1].subscribe = sinon.stub()
  40. @rclient[1].on = sinon.stub()
  41. @EditorUpdatesController.listenForUpdatesFromDocumentUpdater()
  42. it "should subscribe to the doc-updater stream", ->
  43. @rclient[0].subscribe.calledWith("applied-ops").should.equal true
  44. it "should register a callback to handle updates", ->
  45. @rclient[0].on.calledWith("message").should.equal true
  46. it "should subscribe to any additional doc-updater stream", ->
  47. @rclient[1].subscribe.calledWith("applied-ops").should.equal true
  48. @rclient[1].on.calledWith("message").should.equal true
  49. describe "_processMessageFromDocumentUpdater", ->
  50. describe "with bad JSON", ->
  51. beforeEach ->
  52. @SafeJsonParse.parse = sinon.stub().callsArgWith 1, new Error("oops")
  53. @EditorUpdatesController._processMessageFromDocumentUpdater @io, "applied-ops", "blah"
  54. it "should log an error", ->
  55. @logger.error.called.should.equal true
  56. describe "with update", ->
  57. beforeEach ->
  58. @message =
  59. doc_id: @doc_id
  60. op: {t: "foo", p: 12}
  61. @EditorUpdatesController._applyUpdateFromDocumentUpdater = sinon.stub()
  62. @EditorUpdatesController._processMessageFromDocumentUpdater @io, "applied-ops", JSON.stringify(@message)
  63. it "should apply the update", ->
  64. @EditorUpdatesController._applyUpdateFromDocumentUpdater
  65. .calledWith(@io, @doc_id, @message.op)
  66. .should.equal true
  67. describe "with error", ->
  68. beforeEach ->
  69. @message =
  70. doc_id: @doc_id
  71. error: "Something went wrong"
  72. @EditorUpdatesController._processErrorFromDocumentUpdater = sinon.stub()
  73. @EditorUpdatesController._processMessageFromDocumentUpdater @io, "applied-ops", JSON.stringify(@message)
  74. it "should process the error", ->
  75. @EditorUpdatesController._processErrorFromDocumentUpdater
  76. .calledWith(@io, @doc_id, @message.error)
  77. .should.equal true
  78. describe "_applyUpdateFromDocumentUpdater", ->
  79. beforeEach ->
  80. @sourceClient = new MockClient()
  81. @otherClients = [new MockClient(), new MockClient()]
  82. @update =
  83. op: [ t: "foo", p: 12 ]
  84. meta: source: @sourceClient.publicId
  85. v: @version = 42
  86. doc: @doc_id
  87. @io.sockets =
  88. clients: sinon.stub().returns([@sourceClient, @otherClients..., @sourceClient]) # include a duplicate client
  89. describe "normally", ->
  90. beforeEach ->
  91. @EditorUpdatesController._applyUpdateFromDocumentUpdater @io, @doc_id, @update
  92. it "should send a version bump to the source client", ->
  93. @sourceClient.emit
  94. .calledWith("otUpdateApplied", v: @version, doc: @doc_id)
  95. .should.equal true
  96. @sourceClient.emit.calledOnce.should.equal true
  97. it "should get the clients connected to the document", ->
  98. @io.sockets.clients
  99. .calledWith(@doc_id)
  100. .should.equal true
  101. it "should send the full update to the other clients", ->
  102. for client in @otherClients
  103. client.emit
  104. .calledWith("otUpdateApplied", @update)
  105. .should.equal true
  106. describe "with a duplicate op", ->
  107. beforeEach ->
  108. @update.dup = true
  109. @EditorUpdatesController._applyUpdateFromDocumentUpdater @io, @doc_id, @update
  110. it "should send a version bump to the source client as usual", ->
  111. @sourceClient.emit
  112. .calledWith("otUpdateApplied", v: @version, doc: @doc_id)
  113. .should.equal true
  114. it "should not send anything to the other clients (they've already had the op)", ->
  115. for client in @otherClients
  116. client.emit
  117. .calledWith("otUpdateApplied")
  118. .should.equal false
  119. describe "_processErrorFromDocumentUpdater", ->
  120. beforeEach ->
  121. @clients = [new MockClient(), new MockClient()]
  122. @io.sockets =
  123. clients: sinon.stub().returns(@clients)
  124. @EditorUpdatesController._processErrorFromDocumentUpdater @io, @doc_id, "Something went wrong"
  125. it "should log a warning", ->
  126. @logger.warn.called.should.equal true
  127. it "should disconnect all clients in that document", ->
  128. @io.sockets.clients.calledWith(@doc_id).should.equal true
  129. for client in @clients
  130. client.disconnect.called.should.equal true