ShareJsDBTests.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/ShareJsDB.js"
  6. SandboxedModule = require('sandboxed-module')
  7. Errors = require "../../../../app/js/Errors"
  8. describe "ShareJsDB", ->
  9. beforeEach ->
  10. @doc_id = "document-id"
  11. @project_id = "project-id"
  12. @doc_key = "#{@project_id}:#{@doc_id}"
  13. @callback = sinon.stub()
  14. @ShareJsDB = SandboxedModule.require modulePath, requires:
  15. "./RedisManager": @RedisManager = {}
  16. @version = 42
  17. @lines = ["one", "two", "three"]
  18. @db = new @ShareJsDB(@project_id, @doc_id, @lines, @version)
  19. describe "getSnapshot", ->
  20. describe "successfully", ->
  21. beforeEach ->
  22. @db.getSnapshot @doc_key, @callback
  23. it "should return the doc lines", ->
  24. @callback.args[0][1].snapshot.should.equal @lines.join("\n")
  25. it "should return the doc version", ->
  26. @callback.args[0][1].v.should.equal @version
  27. it "should return the type as text", ->
  28. @callback.args[0][1].type.should.equal "text"
  29. describe "when the key does not match", ->
  30. beforeEach ->
  31. @db.getSnapshot "bad:key", @callback
  32. it "should return the callback with a NotFoundError", ->
  33. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  34. describe "getOps", ->
  35. describe "with start == end", ->
  36. beforeEach ->
  37. @start = @end = 42
  38. @db.getOps @doc_key, @start, @end, @callback
  39. it "should return an empty array", ->
  40. @callback.calledWith(null, []).should.equal true
  41. describe "with a non empty range", ->
  42. beforeEach ->
  43. @start = 35
  44. @end = 42
  45. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  46. @db.getOps @doc_key, @start, @end, @callback
  47. it "should get the range from redis", ->
  48. @RedisManager.getPreviousDocOps
  49. .calledWith(@doc_id, @start, @end-1)
  50. .should.equal true
  51. it "should return the ops", ->
  52. @callback.calledWith(null, @ops).should.equal true
  53. describe "with no specified end", ->
  54. beforeEach ->
  55. @start = 35
  56. @end = null
  57. @RedisManager.getPreviousDocOps = sinon.stub().callsArgWith(3, null, @ops)
  58. @db.getOps @doc_key, @start, @end, @callback
  59. it "should get until the end of the list", ->
  60. @RedisManager.getPreviousDocOps
  61. .calledWith(@doc_id, @start, -1)
  62. .should.equal true
  63. describe "writeOps", ->
  64. describe "writing an op", ->
  65. beforeEach ->
  66. @opData =
  67. op: {p: 20, t: "foo"}
  68. meta: {source: "bar"}
  69. v: @version
  70. @db.writeOp @doc_key, @opData, @callback
  71. it "should write into appliedOps", ->
  72. expect(@db.appliedOps[@doc_key]).to.deep.equal [@opData]
  73. it "should call the callback without an error", ->
  74. @callback.called.should.equal true
  75. (@callback.args[0][0]?).should.equal false