ShareJsUpdateManagerTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. @ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
  28. @ShareJsUpdateManager._listenForOps = sinon.stub()
  29. @ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
  30. @updates = [
  31. {p: 4, t: "foo"}
  32. {p: 6, t: "bar"}
  33. ]
  34. @updatedDocLines = ["one", "two"]
  35. describe "successfully", ->
  36. beforeEach (done) ->
  37. @model.getSnapshot.callsArgWith(1, null, {snapshot: @updatedDocLines.join("\n"), v: @version})
  38. @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
  39. @callback(err, docLines, version)
  40. done()
  41. it "should create a new ShareJs model", ->
  42. @ShareJsUpdateManager.getNewShareJsModel
  43. .called.should.equal true
  44. it "should listen for ops on the model", ->
  45. @ShareJsUpdateManager._listenForOps
  46. .calledWith(@model)
  47. .should.equal true
  48. it "should send each update to ShareJs", ->
  49. for update in @updates
  50. @model.applyOp
  51. .calledWith("#{@project_id}:#{@doc_id}", update).should.equal true
  52. it "should get the updated doc lines", ->
  53. @model.getSnapshot
  54. .calledWith("#{@project_id}:#{@doc_id}")
  55. .should.equal true
  56. it "should return the updated doc lines", ->
  57. @callback.calledWith(null, @updatedDocLines, @version).should.equal true
  58. describe "when applyOp fails", ->
  59. beforeEach (done) ->
  60. @error = new Error("Something went wrong")
  61. @ShareJsUpdateManager._sendError = sinon.stub()
  62. @model.applyOp = sinon.stub().callsArgWith(2, @error)
  63. @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
  64. @callback(err, docLines, version)
  65. done()
  66. it "should call sendError with the error", ->
  67. @ShareJsUpdateManager._sendError
  68. .calledWith(@project_id, @doc_id, @error)
  69. .should.equal true
  70. it "should call the callback with the error", ->
  71. @callback.calledWith(@error).should.equal true
  72. describe "when getSnapshot fails", ->
  73. beforeEach (done) ->
  74. @error = new Error("Something went wrong")
  75. @ShareJsUpdateManager._sendError = sinon.stub()
  76. @model.getSnapshot.callsArgWith(1, @error)
  77. @ShareJsUpdateManager.applyUpdates @project_id, @doc_id, @updates, (err, docLines, version) =>
  78. @callback(err, docLines, version)
  79. done()
  80. it "should call sendError with the error", ->
  81. @ShareJsUpdateManager._sendError
  82. .calledWith(@project_id, @doc_id, @error)
  83. .should.equal true
  84. it "should call the callback with the error", ->
  85. @callback.calledWith(@error).should.equal true
  86. describe "_listenForOps", ->
  87. beforeEach ->
  88. @model = on: (event, callback) =>
  89. @callback = callback
  90. sinon.spy @model, "on"
  91. @ShareJsUpdateManager._listenForOps(@model)
  92. it "should listen to the model for updates", ->
  93. @model.on.calledWith("applyOp")
  94. .should.equal true
  95. describe "the callback", ->
  96. beforeEach ->
  97. @opData =
  98. op: {t: "foo", p: 1}
  99. meta: source: "bar"
  100. @rclient.publish = sinon.stub()
  101. @callback("#{@project_id}:#{@doc_id}", @opData)
  102. it "should publish the op to redis", ->
  103. @rclient.publish
  104. .calledWith("applied-ops", JSON.stringify(project_id: @project_id, doc_id: @doc_id, op: @opData))
  105. .should.equal true
  106. describe "_sendError", ->
  107. beforeEach ->
  108. @error_text = "Something went wrong"
  109. @rclient.publish = sinon.stub()
  110. @ShareJsUpdateManager._sendError(@project_id, @doc_id, new Error(@error_text))
  111. it "should publish the error to the redis stream", ->
  112. @rclient.publish
  113. .calledWith("applied-ops", JSON.stringify(project_id: @project_id, doc_id: @doc_id, error: @error_text))
  114. .should.equal true