DocumentUpdaterControllerTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. @EditorUpdatesController = SandboxedModule.require modulePath, requires:
  13. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
  14. "settings-sharelatex": @settings =
  15. redis:
  16. documentupdater:
  17. key_schema:
  18. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  19. "redis-sharelatex" :
  20. createClient: () =>
  21. @rclient = {}
  22. "./SafeJsonParse": @SafeJsonParse =
  23. parse: (data, cb) => cb null, JSON.parse(data)
  24. "./EventLogger": @EventLogger = {checkEventOrder: sinon.stub()}
  25. "./HealthCheckManager": {check: sinon.stub()}
  26. "metrics-sharelatex": @metrics = {inc: sinon.stub()}
  27. describe "listenForUpdatesFromDocumentUpdater", ->
  28. beforeEach ->
  29. @rclient.subscribe = sinon.stub()
  30. @rclient.on = sinon.stub()
  31. @EditorUpdatesController.listenForUpdatesFromDocumentUpdater()
  32. it "should subscribe to the doc-updater stream", ->
  33. @rclient.subscribe.calledWith("applied-ops").should.equal true
  34. it "should register a callback to handle updates", ->
  35. @rclient.on.calledWith("message").should.equal true
  36. describe "_processMessageFromDocumentUpdater", ->
  37. describe "with bad JSON", ->
  38. beforeEach ->
  39. @SafeJsonParse.parse = sinon.stub().callsArgWith 1, new Error("oops")
  40. @EditorUpdatesController._processMessageFromDocumentUpdater @io, "applied-ops", "blah"
  41. it "should log an error", ->
  42. @logger.error.called.should.equal true
  43. describe "with update", ->
  44. beforeEach ->
  45. @message =
  46. doc_id: @doc_id
  47. op: {t: "foo", p: 12}
  48. @EditorUpdatesController._applyUpdateFromDocumentUpdater = sinon.stub()
  49. @EditorUpdatesController._processMessageFromDocumentUpdater @io, "applied-ops", JSON.stringify(@message)
  50. it "should apply the update", ->
  51. @EditorUpdatesController._applyUpdateFromDocumentUpdater
  52. .calledWith(@io, @doc_id, @message.op)
  53. .should.equal true
  54. describe "with error", ->
  55. beforeEach ->
  56. @message =
  57. doc_id: @doc_id
  58. error: "Something went wrong"
  59. @EditorUpdatesController._processErrorFromDocumentUpdater = sinon.stub()
  60. @EditorUpdatesController._processMessageFromDocumentUpdater @io, "applied-ops", JSON.stringify(@message)
  61. it "should process the error", ->
  62. @EditorUpdatesController._processErrorFromDocumentUpdater
  63. .calledWith(@io, @doc_id, @message.error)
  64. .should.equal true
  65. describe "_applyUpdateFromDocumentUpdater", ->
  66. beforeEach ->
  67. @sourceClient = new MockClient()
  68. @otherClients = [new MockClient(), new MockClient()]
  69. @update =
  70. op: [ t: "foo", p: 12 ]
  71. meta: source: @sourceClient.id
  72. v: @version = 42
  73. doc: @doc_id
  74. @io.sockets =
  75. clients: sinon.stub().returns([@sourceClient, @otherClients..., @sourceClient]) # include a duplicate client
  76. describe "normally", ->
  77. beforeEach ->
  78. @EditorUpdatesController._applyUpdateFromDocumentUpdater @io, @doc_id, @update
  79. it "should send a version bump to the source client", ->
  80. @sourceClient.emit
  81. .calledWith("otUpdateApplied", v: @version, doc: @doc_id)
  82. .should.equal true
  83. @sourceClient.emit.calledOnce.should.equal true
  84. it "should get the clients connected to the document", ->
  85. @io.sockets.clients
  86. .calledWith(@doc_id)
  87. .should.equal true
  88. it "should send the full update to the other clients", ->
  89. for client in @otherClients
  90. client.emit
  91. .calledWith("otUpdateApplied", @update)
  92. .should.equal true
  93. describe "with a duplicate op", ->
  94. beforeEach ->
  95. @update.dup = true
  96. @EditorUpdatesController._applyUpdateFromDocumentUpdater @io, @doc_id, @update
  97. it "should send a version bump to the source client as usual", ->
  98. @sourceClient.emit
  99. .calledWith("otUpdateApplied", v: @version, doc: @doc_id)
  100. .should.equal true
  101. it "should not send anything to the other clients (they've already had the op)", ->
  102. for client in @otherClients
  103. client.emit
  104. .calledWith("otUpdateApplied")
  105. .should.equal false
  106. describe "_processErrorFromDocumentUpdater", ->
  107. beforeEach ->
  108. @clients = [new MockClient(), new MockClient()]
  109. @io.sockets =
  110. clients: sinon.stub().returns(@clients)
  111. @EditorUpdatesController._processErrorFromDocumentUpdater @io, @doc_id, "Something went wrong"
  112. it "should log a warning", ->
  113. @logger.warn.called.should.equal true
  114. it "should disconnect all clients in that document", ->
  115. @io.sockets.clients.calledWith(@doc_id).should.equal true
  116. for client in @clients
  117. client.disconnect.called.should.equal true