ShareJsUpdateManagerTests.js 4.7 KB

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