DocumentUpdaterControllerTests.coffee 4.5 KB

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