EditorUpdatesControllerTests.coffee 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorUpdatesController'
  5. MockClient = require "../helpers/MockClient"
  6. assert = require('assert')
  7. describe "EditorUpdatesController", ->
  8. beforeEach ->
  9. @project_id = "project-id-123"
  10. @doc_id = "doc-id-123"
  11. @client = new MockClient()
  12. @callback = sinon.stub()
  13. @EditorUpdatesController = SandboxedModule.require modulePath, requires:
  14. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub() }
  15. "./EditorRealTimeController" : @EditorRealTimeController = {}
  16. "../DocumentUpdater/DocumentUpdaterHandler" : @DocumentUpdaterHandler = {}
  17. "../../infrastructure/Metrics" : @metrics = { set: sinon.stub(), inc: sinon.stub() }
  18. "../../infrastructure/Server" : io: @io = {}
  19. "redis" :
  20. createClient: ()=>
  21. @rclient = {auth:->}
  22. describe "_applyUpdate", ->
  23. beforeEach ->
  24. @update = {op: {p: 12, t: "foo"}}
  25. @client.set("user_id", @user_id = "user-id-123")
  26. @DocumentUpdaterHandler.queueChange = sinon.stub().callsArg(3)
  27. describe "succesfully", ->
  28. beforeEach ->
  29. @EditorUpdatesController._applyUpdate @client, @project_id, @doc_id, @update, @callback
  30. it "should queue the update", ->
  31. @DocumentUpdaterHandler.queueChange
  32. .calledWith(@project_id, @doc_id, @update)
  33. .should.equal true
  34. it "should call the callback", ->
  35. @callback.called.should.equal true
  36. it "should update the active users metric", ->
  37. @metrics.set.calledWith("editor.active-users", @user_id).should.equal true
  38. it "should update the active projects metric", ->
  39. @metrics.set.calledWith("editor.active-projects", @project_id).should.equal true
  40. it "should increment the doc updates", ->
  41. @metrics.inc.calledWith("editor.doc-update").should.equal true
  42. describe "unsuccessfully", ->
  43. beforeEach ->
  44. @client.disconnect = sinon.stub()
  45. @DocumentUpdaterHandler.queueChange = sinon.stub().callsArgWith(3, new Error("Something went wrong"))
  46. @EditorUpdatesController._applyUpdate @client, @project_id, @doc_id, @update, @callback
  47. it "should disconnect the client", ->
  48. @client.disconnect.called.should.equal true
  49. it "should log an error", ->
  50. @logger.error.called.should.equal true
  51. describe "applyOtUpdate", ->
  52. beforeEach ->
  53. @client.id = "client-id"
  54. @client.set("user_id", @user_id = "user-id-123")
  55. @update = {op: {p: 12, t: "foo"}}
  56. @EditorUpdatesController._applyUpdate = sinon.stub()
  57. @EditorUpdatesController.applyOtUpdate @client, @project_id, @doc_id, @update
  58. it "should set the source of the update to the client id", ->
  59. @update.meta.source.should.equal @client.id
  60. it "should set the user_id of the update to the user id", ->
  61. @update.meta.user_id.should.equal @user_id
  62. it "should apply the update", ->
  63. @EditorUpdatesController._applyUpdate
  64. .calledWith(@client, @project_id, @doc_id, @update)
  65. .should.equal true
  66. describe "listenForUpdatesFromDocumentUpdater", ->
  67. beforeEach ->
  68. @rclient.subscribe = sinon.stub()
  69. @rclient.on = sinon.stub()
  70. @EditorUpdatesController.listenForUpdatesFromDocumentUpdater()
  71. it "should subscribe to the doc-updater stream", ->
  72. @rclient.subscribe.calledWith("applied-ops").should.equal true
  73. it "should register a callback to handle updates", ->
  74. @rclient.on.calledWith("message").should.equal true
  75. describe "_processMessageFromDocumentUpdater", ->
  76. describe "with update", ->
  77. beforeEach ->
  78. @message =
  79. doc_id: @doc_id
  80. op: {t: "foo", p: 12}
  81. @EditorUpdatesController._applyUpdateFromDocumentUpdater = sinon.stub()
  82. @EditorUpdatesController._processMessageFromDocumentUpdater "applied-ops", JSON.stringify(@message)
  83. it "should apply the update", ->
  84. @EditorUpdatesController._applyUpdateFromDocumentUpdater
  85. .calledWith(@doc_id, @message.op)
  86. .should.equal true
  87. describe "with error", ->
  88. beforeEach ->
  89. @message =
  90. doc_id: @doc_id
  91. error: "Something went wrong"
  92. @EditorUpdatesController._processErrorFromDocumentUpdater = sinon.stub()
  93. @EditorUpdatesController._processMessageFromDocumentUpdater "applied-ops", JSON.stringify(@message)
  94. it "should process the error", ->
  95. @EditorUpdatesController._processErrorFromDocumentUpdater
  96. .calledWith(@doc_id, @message.error)
  97. .should.equal true
  98. describe "_applyUpdateFromDocumentUpdater", ->
  99. beforeEach ->
  100. @sourceClient = new MockClient()
  101. @otherClients = [new MockClient(), new MockClient()]
  102. @update =
  103. op: [ t: "foo", p: 12 ]
  104. meta: source: @sourceClient.id
  105. v: @version = 42
  106. doc: @doc_id
  107. @io.sockets =
  108. clients: sinon.stub().returns([@sourceClient, @otherClients...])
  109. @EditorUpdatesController._applyUpdateFromDocumentUpdater @doc_id, @update
  110. it "should send a version bump to the source client", ->
  111. @sourceClient.emit
  112. .calledWith("otUpdateApplied", v: @version, doc: @doc_id)
  113. .should.equal true
  114. it "should get the clients connected to the document", ->
  115. @io.sockets.clients
  116. .calledWith(@doc_id)
  117. .should.equal true
  118. it "should send the full update to the other clients", ->
  119. for client in @otherClients
  120. client.emit
  121. .calledWith("otUpdateApplied", @update)
  122. .should.equal true
  123. describe "_processErrorFromDocumentUpdater", ->
  124. beforeEach ->
  125. @clients = [new MockClient(), new MockClient()]
  126. @io.sockets =
  127. clients: sinon.stub().returns(@clients)
  128. @EditorUpdatesController._processErrorFromDocumentUpdater @doc_id, "Something went wrong"
  129. it "should log out an error", ->
  130. @logger.error.called.should.equal true
  131. it "should disconnect all clients in that document", ->
  132. @io.sockets.clients.calledWith(@doc_id).should.equal true
  133. for client in @clients
  134. client.disconnect.called.should.equal true