ShareJsUpdateManagerTests.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "./RealTimeRedisManager": @RealTimeRedisManager = {}
  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. @model.applyOp = sinon.stub().callsArgWith(2, @error)
  65. @ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
  66. @callback(err, docLines, version)
  67. done()
  68. it "should call the callback with the error", ->
  69. @callback.calledWith(@error).should.equal true
  70. describe "when getSnapshot fails", ->
  71. beforeEach (done) ->
  72. @error = new Error("Something went wrong")
  73. @model.getSnapshot.callsArgWith(1, @error)
  74. @ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
  75. @callback(err, docLines, version)
  76. done()
  77. it "should call the callback with the error", ->
  78. @callback.calledWith(@error).should.equal true
  79. describe "_listenForOps", ->
  80. beforeEach ->
  81. @model = on: (event, callback) =>
  82. @callback = callback
  83. sinon.spy @model, "on"
  84. @ShareJsUpdateManager._listenForOps(@model)
  85. it "should listen to the model for updates", ->
  86. @model.on.calledWith("applyOp")
  87. .should.equal true
  88. describe "the callback", ->
  89. beforeEach ->
  90. @opData =
  91. op: {t: "foo", p: 1}
  92. meta: source: "bar"
  93. @RealTimeRedisManager.sendData = sinon.stub()
  94. @callback("#{@project_id}:#{@doc_id}", @opData)
  95. it "should publish the op to redis", ->
  96. @RealTimeRedisManager.sendData
  97. .calledWith({project_id: @project_id, doc_id: @doc_id, op: @opData})
  98. .should.equal true