RealTimeRedisManagerTests.coffee 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. documentupdater: @settings =
  18. key_schema:
  19. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  20. "logger-sharelatex": { log: () -> }
  21. @doc_id = "doc-id-123"
  22. @project_id = "project-id-123"
  23. @callback = sinon.stub()
  24. describe "getPendingUpdatesForDoc", ->
  25. beforeEach ->
  26. @rclient.lrange = sinon.stub()
  27. @rclient.ltrim = sinon.stub()
  28. describe "successfully", ->
  29. beforeEach ->
  30. @updates = [
  31. { op: [{ i: "foo", p: 4 }] }
  32. { op: [{ i: "foo", p: 4 }] }
  33. ]
  34. @jsonUpdates = @updates.map (update) -> JSON.stringify update
  35. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  36. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  37. it "should get the pending updates", ->
  38. @rclient.lrange
  39. .calledWith("PendingUpdates:#{@doc_id}", 0, 7)
  40. .should.equal true
  41. it "should delete the pending updates", ->
  42. @rclient.ltrim
  43. .calledWith("PendingUpdates:#{@doc_id}", 8, -1)
  44. .should.equal true
  45. it "should call the callback with the updates", ->
  46. @callback.calledWith(null, @updates).should.equal true
  47. describe "when the JSON doesn't parse", ->
  48. beforeEach ->
  49. @jsonUpdates = [
  50. JSON.stringify { op: [{ i: "foo", p: 4 }] }
  51. "broken json"
  52. ]
  53. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  54. @RealTimeRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  55. it "should return an error to the callback", ->
  56. @callback.calledWith(new Error("JSON parse error")).should.equal true
  57. describe "getUpdatesLength", ->
  58. beforeEach ->
  59. @rclient.llen = sinon.stub().yields(null, @length = 3)
  60. @RealTimeRedisManager.getUpdatesLength @doc_id, @callback
  61. it "should look up the length", ->
  62. @rclient.llen.calledWith("PendingUpdates:#{@doc_id}").should.equal true
  63. it "should return the length", ->
  64. @callback.calledWith(null, @length).should.equal true