RealTimeRedisManagerTests.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RealTimeRedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. describe "RealTimeRedisManager", ->
  8. beforeEach ->
  9. @rclient =
  10. auth: () ->
  11. exec: sinon.stub()
  12. @rclient.multi = () => @rclient
  13. @RealTimeRedisManager = SandboxedModule.require modulePath, requires:
  14. "redis-sharelatex": createClient: () => @rclient
  15. "settings-sharelatex":
  16. redis:
  17. realtime: @settings =
  18. key_schema:
  19. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  20. "logger-sharelatex": { log: () -> }
  21. "crypto": @crypto = { randomBytes: sinon.stub().withArgs(4).returns(Buffer.from([0x1, 0x2, 0x3, 0x4])) }
  22. "os": @os = {hostname: sinon.stub().returns("somehost")}
  23. @doc_id = "doc-id-123"
  24. @project_id = "project-id-123"
  25. @callback = sinon.stub()
  26. describe "getPendingUpdatesForDoc", ->
  27. beforeEach ->
  28. @rclient.lrange = sinon.stub()
  29. @rclient.ltrim = sinon.stub()
  30. describe "successfully", ->
  31. beforeEach ->
  32. @updates = [
  33. { op: [{ i: "foo", p: 4 }] }
  34. { op: [{ i: "foo", p: 4 }] }
  35. ]
  36. @jsonUpdates = @updates.map (update) -> JSON.stringify update
  37. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  38. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  39. it "should get the pending updates", ->
  40. @rclient.lrange
  41. .calledWith("PendingUpdates:#{@doc_id}", 0, 7)
  42. .should.equal true
  43. it "should delete the pending updates", ->
  44. @rclient.ltrim
  45. .calledWith("PendingUpdates:#{@doc_id}", 8, -1)
  46. .should.equal true
  47. it "should call the callback with the updates", ->
  48. @callback.calledWith(null, @updates).should.equal true
  49. describe "when the JSON doesn't parse", ->
  50. beforeEach ->
  51. @jsonUpdates = [
  52. JSON.stringify { op: [{ i: "foo", p: 4 }] }
  53. "broken json"
  54. ]
  55. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  56. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  57. it "should return an error to the callback", ->
  58. @callback.calledWith(new Error("JSON parse error")).should.equal true
  59. describe "getUpdatesLength", ->
  60. beforeEach ->
  61. @rclient.llen = sinon.stub().yields(null, @length = 3)
  62. @RealTimeRedisManager.getUpdatesLength @doc_id, @callback
  63. it "should look up the length", ->
  64. @rclient.llen.calledWith("PendingUpdates:#{@doc_id}").should.equal true
  65. it "should return the length", ->
  66. @callback.calledWith(null, @length).should.equal true
  67. describe "sendData", ->
  68. beforeEach ->
  69. @message_id = "doc:somehost:01020304-0"
  70. @rclient.publish = sinon.stub()
  71. @RealTimeRedisManager.sendData({op: "thisop"})
  72. it "should send the op with a message id", ->
  73. @rclient.publish.calledWith("applied-ops", JSON.stringify({op:"thisop",_id:@message_id})).should.equal true