DocumentUpdaterControllerTests.coffee 4.5 KB

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