HistoryManagerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/HistoryManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "HistoryManager", ->
  8. beforeEach ->
  9. @HistoryManager = SandboxedModule.require modulePath, requires:
  10. "./UpdateCompressor": @UpdateCompressor = {}
  11. "./MongoManager" : @MongoManager = {}
  12. "logger-sharelatex": { log: sinon.stub() }
  13. @doc_id = "doc-id-123"
  14. @callback = sinon.stub()
  15. describe "when there are no raw ops", ->
  16. beforeEach ->
  17. @MongoManager.popLastCompressedUpdate = sinon.stub()
  18. @MongoManager.insertCompressedUpdates = sinon.stub()
  19. @HistoryManager.compressAndSaveRawUpdates @doc_id, [], @callback
  20. it "should not need to access the database", ->
  21. @MongoManager.popLastCompressedUpdate.called.should.equal false
  22. @MongoManager.insertCompressedUpdates.called.should.equal false
  23. it "should call the callback", ->
  24. @callback.called.should.equal true
  25. describe "when there is no compressed history to begin with", ->
  26. beforeEach ->
  27. @rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  28. @compressedUpdates = { v: 13, op: "compressed-op-12" }
  29. @MongoManager.popLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
  30. @MongoManager.insertCompressedUpdates = sinon.stub().callsArg(2)
  31. @UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
  32. @HistoryManager.compressAndSaveRawUpdates @doc_id, @rawUpdates, @callback
  33. it "should try to pop the last compressed op", ->
  34. @MongoManager.popLastCompressedUpdate
  35. .calledWith(@doc_id)
  36. .should.equal true
  37. it "should compress the raw ops", ->
  38. @UpdateCompressor.compressRawUpdates
  39. .calledWith(null, @rawUpdates)
  40. .should.equal true
  41. it "should save the compressed ops", ->
  42. @MongoManager.insertCompressedUpdates
  43. .calledWith(@doc_id, @compressedUpdates)
  44. .should.equal true
  45. it "should call the callback", ->
  46. @callback.called.should.equal true
  47. describe "when the raw ops need appending to existing history", ->
  48. beforeEach ->
  49. @lastCompressedUpdate = { v: 11, op: "compressed-op-11" }
  50. @compressedUpdates = { v: 13, op: "compressed-op-12" }
  51. @MongoManager.popLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @lastCompressedUpdate)
  52. @MongoManager.insertCompressedUpdates = sinon.stub().callsArg(2)
  53. @UpdateCompressor.compressRawUpdates = sinon.stub().returns(@compressedUpdates)
  54. describe "when the raw ops start where the existing history ends", ->
  55. beforeEach ->
  56. @rawUpdates = [{ v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  57. @HistoryManager.compressAndSaveRawUpdates @doc_id, @rawUpdates, @callback
  58. it "should try to pop the last compressed op", ->
  59. @MongoManager.popLastCompressedUpdate
  60. .calledWith(@doc_id)
  61. .should.equal true
  62. it "should compress the last compressed op and the raw ops", ->
  63. @UpdateCompressor.compressRawUpdates
  64. .calledWith(@lastCompressedUpdate, @rawUpdates)
  65. .should.equal true
  66. it "should save the compressed ops", ->
  67. @MongoManager.insertCompressedUpdates
  68. .calledWith(@doc_id, @compressedUpdates)
  69. .should.equal true
  70. it "should call the callback", ->
  71. @callback.called.should.equal true
  72. describe "when some raw ops are passed that have already been compressed", ->
  73. beforeEach ->
  74. @rawUpdates = [{ v: 10, op: "mock-op-10" }, { v: 11, op: "mock-op-11"}, { v: 12, op: "mock-op-12" }, { v: 13, op: "mock-op-13" }]
  75. @HistoryManager.compressAndSaveRawUpdates @doc_id, @rawUpdates, @callback
  76. it "should only compress the more recent raw ops", ->
  77. @UpdateCompressor.compressRawUpdates
  78. .calledWith(@lastCompressedUpdate, @rawUpdates.slice(-2))
  79. .should.equal true
  80. describe "when the raw ops do not follow from the last compressed op version", ->
  81. beforeEach ->
  82. @rawUpdates = [{ v: 13, op: "mock-op-13" }]
  83. @HistoryManager.compressAndSaveRawUpdates @doc_id, @rawUpdates, @callback
  84. it "should call the callback with an error", ->
  85. @callback
  86. .calledWith(new Error("Tried to apply raw op at version 13 to last compressed update with version 11"))
  87. .should.equal true