ShareJsUpdateManagerTests.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "./Metrics": @metrics = { inc: sinon.stub() }
  21. globals:
  22. clearTimeout: @clearTimeout = sinon.stub()
  23. describe "applyUpdate", ->
  24. beforeEach ->
  25. @lines = ["one", "two"]
  26. @version = 34
  27. @update = {p: 4, t: "foo"}
  28. @updatedDocLines = ["onefoo", "two"]
  29. @model =
  30. applyOp: sinon.stub().callsArg(2)
  31. getSnapshot: sinon.stub()
  32. db:
  33. appliedOps: {}
  34. @ShareJsUpdateManager.getNewShareJsModel = sinon.stub().returns(@model)
  35. @ShareJsUpdateManager._listenForOps = sinon.stub()
  36. @ShareJsUpdateManager.removeDocFromCache = sinon.stub().callsArg(1)
  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.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version, appliedOps) =>
  42. @callback(err, docLines, version, appliedOps)
  43. done()
  44. it "should create a new ShareJs model", ->
  45. @ShareJsUpdateManager.getNewShareJsModel
  46. .calledWith(@project_id, @doc_id, @lines, @version)
  47. .should.equal true
  48. it "should listen for ops on the model", ->
  49. @ShareJsUpdateManager._listenForOps
  50. .calledWith(@model)
  51. .should.equal true
  52. it "should send the update to ShareJs", ->
  53. @model.applyOp
  54. .calledWith("#{@project_id}:#{@doc_id}", @update)
  55. .should.equal true
  56. it "should get the updated doc lines", ->
  57. @model.getSnapshot
  58. .calledWith("#{@project_id}:#{@doc_id}")
  59. .should.equal true
  60. it "should return the updated doc lines, version and ops", ->
  61. @callback.calledWith(null, @updatedDocLines, @version, @appliedOps).should.equal true
  62. describe "when applyOp fails", ->
  63. beforeEach (done) ->
  64. @error = new Error("Something went wrong")
  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 the callback with the error", ->
  70. @callback.calledWith(@error).should.equal true
  71. describe "when getSnapshot fails", ->
  72. beforeEach (done) ->
  73. @error = new Error("Something went wrong")
  74. @model.getSnapshot.callsArgWith(1, @error)
  75. @ShareJsUpdateManager.applyUpdate @project_id, @doc_id, @update, @lines, @version, (err, docLines, version) =>
  76. @callback(err, docLines, version)
  77. done()
  78. it "should call the callback with the error", ->
  79. @callback.calledWith(@error).should.equal true
  80. describe "_listenForOps", ->
  81. beforeEach ->
  82. @model = on: (event, callback) =>
  83. @callback = callback
  84. sinon.spy @model, "on"
  85. @ShareJsUpdateManager._listenForOps(@model)
  86. it "should listen to the model for updates", ->
  87. @model.on.calledWith("applyOp")
  88. .should.equal true
  89. describe "the callback", ->
  90. beforeEach ->
  91. @opData =
  92. op: {t: "foo", p: 1}
  93. meta: source: "bar"
  94. @RealTimeRedisManager.sendData = sinon.stub()
  95. @callback("#{@project_id}:#{@doc_id}", @opData)
  96. it "should publish the op to redis", ->
  97. @RealTimeRedisManager.sendData
  98. .calledWith({project_id: @project_id, doc_id: @doc_id, op: @opData})
  99. .should.equal true