WebRedisManagerTests.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/WebRedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. describe "WebRedisManager", ->
  8. beforeEach ->
  9. @rclient =
  10. auth: () ->
  11. exec: sinon.stub()
  12. @rclient.multi = () => @rclient
  13. @WebRedisManager = SandboxedModule.require modulePath, requires:
  14. "redis-sharelatex": createClient: () => @rclient
  15. "settings-sharelatex": redis: web: @settings = {"mock": "settings"}
  16. @doc_id = "doc-id-123"
  17. @project_id = "project-id-123"
  18. @callback = sinon.stub()
  19. describe "getPendingUpdatesForDoc", ->
  20. beforeEach ->
  21. @rclient.lrange = sinon.stub()
  22. @rclient.del = sinon.stub()
  23. describe "successfully", ->
  24. beforeEach ->
  25. @updates = [
  26. { op: [{ i: "foo", p: 4 }] }
  27. { op: [{ i: "foo", p: 4 }] }
  28. ]
  29. @jsonUpdates = @updates.map (update) -> JSON.stringify update
  30. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  31. @WebRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  32. it "should get the pending updates", ->
  33. @rclient.lrange
  34. .calledWith("PendingUpdates:#{@doc_id}", 0, -1)
  35. .should.equal true
  36. it "should delete the pending updates", ->
  37. @rclient.del
  38. .calledWith("PendingUpdates:#{@doc_id}")
  39. .should.equal true
  40. it "should call the callback with the updates", ->
  41. @callback.calledWith(null, @updates).should.equal true
  42. describe "when the JSON doesn't parse", ->
  43. beforeEach ->
  44. @jsonUpdates = [
  45. JSON.stringify { op: [{ i: "foo", p: 4 }] }
  46. "broken json"
  47. ]
  48. @rclient.exec = sinon.stub().callsArgWith(0, null, [@jsonUpdates])
  49. @WebRedisManager.getPendingUpdatesForDoc @doc_id, @callback
  50. it "should return an error to the callback", ->
  51. @callback.calledWith(new Error("JSON parse error")).should.equal true
  52. describe "getUpdatesLength", ->
  53. beforeEach ->
  54. @rclient.llen = sinon.stub().yields(null, @length = 3)
  55. @WebRedisManager.getUpdatesLength @doc_id, @callback
  56. it "should look up the length", ->
  57. @rclient.llen.calledWith("PendingUpdates:#{@doc_id}").should.equal true
  58. it "should return the length", ->
  59. @callback.calledWith(null, @length).should.equal true
  60. describe "pushUncompressedHistoryOp", ->
  61. beforeEach (done) ->
  62. @op = { op: [{ i: "foo", p: 4 }] }
  63. @rclient.rpush = sinon.stub().yields(null, @length = 42)
  64. @rclient.sadd = sinon.stub().yields()
  65. @WebRedisManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, (args...) =>
  66. @callback(args...)
  67. done()
  68. it "should push the doc op into the doc ops list", ->
  69. @rclient.rpush
  70. .calledWith("UncompressedHistoryOps:#{@doc_id}", JSON.stringify(@op))
  71. .should.equal true
  72. it "should add the doc_id to the set of which records the project docs", ->
  73. @rclient.sadd
  74. .calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
  75. .should.equal true
  76. it "should call the callback with the length", ->
  77. @callback.calledWith(null, @length).should.equal true