WebRedisManagerTests.coffee 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "pushUncompressedHistoryOps", ->
  61. beforeEach ->
  62. @ops = [{ op: [{ i: "foo", p: 4 }] },{ op: [{ i: "bar", p: 56 }] }]
  63. @rclient.rpush = sinon.stub().yields(null, @length = 42)
  64. @rclient.sadd = sinon.stub().yields()
  65. describe "with ops", ->
  66. beforeEach (done) ->
  67. @WebRedisManager.pushUncompressedHistoryOps @project_id, @doc_id, @ops, (args...) =>
  68. @callback(args...)
  69. done()
  70. it "should push the doc op into the doc ops list as JSON", ->
  71. @rclient.rpush
  72. .calledWith("UncompressedHistoryOps:#{@doc_id}", JSON.stringify(@ops[0]), JSON.stringify(@ops[1]))
  73. .should.equal true
  74. it "should add the doc_id to the set of which records the project docs", ->
  75. @rclient.sadd
  76. .calledWith("DocsWithHistoryOps:#{@project_id}", @doc_id)
  77. .should.equal true
  78. it "should call the callback with the length", ->
  79. @callback.calledWith(null, @length).should.equal true
  80. describe "with no ops", ->
  81. beforeEach (done) ->
  82. @WebRedisManager.pushUncompressedHistoryOps @project_id, @doc_id, [], (args...) =>
  83. @callback(args...)
  84. done()
  85. it "should not push the doc op into the doc ops list as JSON", ->
  86. @rclient.rpush
  87. .called
  88. .should.equal false
  89. it "should not add the doc_id to the set of which records the project docs", ->
  90. @rclient.sadd
  91. .called
  92. .should.equal false
  93. it "should call the callback with an error", ->
  94. @callback.calledWith(new Error("cannot push no ops")).should.equal true