pushDocOpTests.coffee 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RedisManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "RedisManager.pushDocOp", ->
  7. beforeEach ->
  8. @RedisManager = SandboxedModule.require modulePath, requires:
  9. "redis-sharelatex": createClient: () =>
  10. @rclient ?=
  11. auth: () ->
  12. multi: () => @rclient
  13. "logger-sharelatex": @logger = {log: sinon.stub()}
  14. @doc_id = "doc-id-123"
  15. @callback = sinon.stub()
  16. @rclient.rpush = sinon.stub()
  17. @rclient.expire = sinon.stub()
  18. @rclient.incr = sinon.stub()
  19. @rclient.ltrim = sinon.stub()
  20. describe "successfully", ->
  21. beforeEach ->
  22. @op = { op: [{ i: "foo", p: 4 }] }
  23. @version = 42
  24. _ = null
  25. @rclient.exec = sinon.stub().callsArgWith(0, null, [_, _, _, @version])
  26. @RedisManager.pushDocOp @doc_id, @op, @callback
  27. it "should push the doc op into the doc ops list", ->
  28. @rclient.rpush
  29. .calledWith("DocOps:#{@doc_id}", JSON.stringify(@op))
  30. .should.equal true
  31. it "should renew the expiry ttl on the doc ops array", ->
  32. @rclient.expire
  33. .calledWith("DocOps:#{@doc_id}", @RedisManager.DOC_OPS_TTL)
  34. .should.equal true
  35. it "should truncate the list to 100 members", ->
  36. @rclient.ltrim
  37. .calledWith("DocOps:#{@doc_id}", -@RedisManager.DOC_OPS_MAX_LENGTH, -1)
  38. .should.equal true
  39. it "should increment the version number", ->
  40. @rclient.incr
  41. .calledWith("DocVersion:#{@doc_id}")
  42. .should.equal true
  43. it "should call the callback with the version number", ->
  44. @callback.calledWith(null, parseInt(@version, 10)).should.equal true