ShareJsUpdateManagerTests.coffee 4.5 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. globals:
  20. clearTimeout: @clearTimeout = sinon.stub()
  21. describe "applyUpdates", ->
  22. beforeEach ->
  23. @version = 34
  24. @model =
  25. applyOp: sinon.stub().callsArg(2)
  26. getSnapshot: sinon.stub()
  27. db:
  28. appliedOps: {}
  29. @ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
  30. @ShareJsUpdateManager._listenForOps = sinon.stub()
  31. @ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
  32. @updates = [
  33. {p: 4, t: "foo"}
  34. {p: 6, t: "bar"}
  35. ]
  36. @updatedDocLines = ["one", "two"]
  37. describe "successfully", ->
  38. beforeEach (done) ->
  39. @model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version})
  40. @model.db.appliedOps["#{@project_id}:#{@doc_id}"] = @appliedOps = ["mock-ops"]
  41. @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version, appliedOps) =>
  42. @callback(err, docLines, version, appliedOps)
  43. done()
  44. it "should create a new ShareJs model", ->
  45. @ShareJsUpdateManager.getNewShareJsModel
  46. .called.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 each update to ShareJs", ->
  52. for update in @updates
  53. @model.applyOp
  54. .calledWith("#{@project_id}:#{@doc_id}", update).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.applyUpdates @project_id, @doc_id, @updates, (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.applyUpdates @project_id, @doc_id, @updates, (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. @rclient.publish = sinon.stub()
  104. @callback("#{@project_id}:#{@doc_id}", @opData)
  105. it "should publish the op to redis", ->
  106. @rclient.publish
  107. .calledWith("applied-ops", JSON.stringify(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. @rclient.publish = sinon.stub()
  113. @ShareJsUpdateManager._sendError(@project_id, @doc_id, new Error(@error_text))
  114. it "should publish the error to the redis stream", ->
  115. @rclient.publish
  116. .calledWith("applied-ops", JSON.stringify(project_id: @project_id, doc_id: @doc_id, error: @error_text))
  117. .should.equal true