ShareJsUpdateManagerTests.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ShareJsUpdateManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "ShareJsUpdateManager", ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @doc_id = "document-id-123"
  10. @callback = sinon.stub()
  11. @ShareJsUpdateManager = SandboxedModule.require modulePath,
  12. requires:
  13. "./sharejs/server/model":
  14. class Model
  15. constructor: (@db) ->
  16. "./ShareJsDB" : @ShareJsDB = { mockDB: true }
  17. "redis-sharelatex" : createClient: () => @rclient = auth:->
  18. "logger-sharelatex": @logger = { log: sinon.stub() }
  19. "./WebRedisManager": @WebRedisManager = {}
  20. globals:
  21. clearTimeout: @clearTimeout = sinon.stub()
  22. describe "applyUpdate", ->
  23. beforeEach ->
  24. @lines = ["one", "two"]
  25. @version = 34
  26. @update = {p: 4, t: "foo"}
  27. @updatedDocLines = ["onefoo", "two"]
  28. @model =
  29. applyOp: sinon.stub().callsArg(2)
  30. getSnapshot: sinon.stub()
  31. db:
  32. appliedOps: {}
  33. @ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
  34. @ShareJsUpdateManager._listenForOps = sinon.stub()
  35. @ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
  36. describe "successfully", ->
  37. beforeEach (done) ->
  38. @model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version})
  39. @model.db.appliedOps["#{@project_id}:#{@doc_id}"] = @appliedOps = ["mock-ops"]
  40. @ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version, appliedOps) =>
  41. @callback(err, docLines, version, appliedOps)
  42. done()
  43. it "should create a new ShareJs model", ->
  44. @ShareJsUpdateManager.getNewShareJsModel
  45. .calledWith(@project_id, @doc_id, @lines, @version)
  46. .should.equal true
  47. it "should listen for ops on the model", ->
  48. @ShareJsUpdateManager._listenForOps
  49. .calledWith(@model)
  50. .should.equal true
  51. it "should send the update to ShareJs", ->
  52. @model.applyOp
  53. .calledWith("#{@project_id}:#{@doc_id}", @update)
  54. .should.equal true
  55. it "should get the updated doc lines", ->
  56. @model.getSnapshot
  57. .calledWith("#{@project_id}:#{@doc_id}")
  58. .should.equal true
  59. it "should return the updated doc lines, version and ops", ->
  60. @callback.calledWith(null, @updatedDocLines, @version, @appliedOps).should.equal true
  61. describe "when applyOp fails", ->
  62. beforeEach (done) ->
  63. @error = new Error("Something went wrong")
  64. @ShareJsUpdateManager._sendError = sinon.stub()
  65. @model.applyOp = sinon.stub().callsArgWith(2, @error)
  66. @ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
  67. @callback(err, docLines, version)
  68. done()
  69. it "should call sendError with the error", ->
  70. @ShareJsUpdateManager._sendError
  71. .calledWith(@project_id, @doc_id, @error)
  72. .should.equal true
  73. it "should call the callback with the error", ->
  74. @callback.calledWith(@error).should.equal true
  75. describe "when getSnapshot fails", ->
  76. beforeEach (done) ->
  77. @error = new Error("Something went wrong")
  78. @ShareJsUpdateManager._sendError = sinon.stub()
  79. @model.getSnapshot.callsArgWith(1, @error)
  80. @ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
  81. @callback(err, docLines, version)
  82. done()
  83. it "should call sendError with the error", ->
  84. @ShareJsUpdateManager._sendError
  85. .calledWith(@project_id, @doc_id, @error)
  86. .should.equal true
  87. it "should call the callback with the error", ->
  88. @callback.calledWith(@error).should.equal true
  89. describe "_listenForOps", ->
  90. beforeEach ->
  91. @model = on: (event, callback) =>
  92. @callback = callback
  93. sinon.spy @model, "on"
  94. @ShareJsUpdateManager._listenForOps(@model)
  95. it "should listen to the model for updates", ->
  96. @model.on.calledWith("applyOp")
  97. .should.equal true
  98. describe "the callback", ->
  99. beforeEach ->
  100. @opData =
  101. op: {t: "foo", p: 1}
  102. meta: source: "bar"
  103. @WebRedisManager.sendData = sinon.stub()
  104. @callback("#{@project_id}:#{@doc_id}", @opData)
  105. it "should publish the op to redis", ->
  106. @WebRedisManager.sendData
  107. .calledWith({project_id: @project_id, doc_id: @doc_id, op: @opData})
  108. .should.equal true
  109. describe "_sendError", ->
  110. beforeEach ->
  111. @error_text = "Something went wrong"
  112. @WebRedisManager.sendData = sinon.stub()
  113. @ShareJsUpdateManager._sendError(@project_id, @doc_id, new Error(@error_text))
  114. it "should publish the error to the redis stream", ->
  115. @WebRedisManager.sendData
  116. .calledWith({project_id: @project_id, doc_id: @doc_id, error: @error_text})
  117. .should.equal true