DocumentUpdaterControllerTests.coffee 4.1 KB

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