DiffManagerTests.coffee 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/DiffManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "DiffManager", ->
  8. beforeEach ->
  9. @DiffManager = SandboxedModule.require modulePath, requires:
  10. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  11. "./HistoryManager": @HistoryManager = {}
  12. "./DocumentUpdaterManager": @DocumentUpdaterManager = {}
  13. "./MongoManager": @MongoManager = {}
  14. "./DiffGenerator": @DiffGenerator = {}
  15. @callback = sinon.stub()
  16. @from = new Date()
  17. @to = new Date(Date.now() + 10000)
  18. @project_id = "mock-project-id"
  19. @doc_id = "mock-doc-id"
  20. describe "getLatestDocAndUpdates", ->
  21. beforeEach ->
  22. @lines = [ "hello", "world" ]
  23. @version = 42
  24. @updates = [ "mock-update-1", "mock-update-2" ]
  25. @HistoryManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(1)
  26. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(2, null, @lines, @version)
  27. @MongoManager.getUpdatesBetweenDates = sinon.stub().callsArgWith(3, null, @updates)
  28. @DiffManager.getLatestDocAndUpdates @project_id, @doc_id, @from, @to, @callback
  29. it "should ensure the latest updates have been compressed", ->
  30. @HistoryManager.processUncompressedUpdatesWithLock
  31. .calledWith(@doc_id)
  32. .should.equal true
  33. it "should get the latest version of the doc", ->
  34. @DocumentUpdaterManager.getDocument
  35. .calledWith(@project_id, @doc_id)
  36. .should.equal true
  37. it "should get the requested updates from Mongo", ->
  38. @MongoManager.getUpdatesBetweenDates
  39. .calledWith(@doc_id, @from, @to)
  40. .should.equal true
  41. it "should call the callback with the lines, version and updates", ->
  42. @callback.calledWith(null, @lines, @version, @updates).should.equal true
  43. describe "getDiff", ->
  44. beforeEach ->
  45. @lines = [ "hello", "world" ]
  46. @version = 42
  47. @updates = [
  48. { op: "mock-4", v: 42, meta: { start_ts: new Date(@to.getTime() + 20)} }
  49. { op: "mock-3", v: 41, meta: { start_ts: new Date(@to.getTime() + 10)} }
  50. { op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
  51. { op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
  52. ]
  53. @diffed_updates = @updates.slice(2)
  54. @rewound_content = "rewound-content"
  55. @diff = [ u: "mock-diff" ]
  56. describe "with matching versions", ->
  57. beforeEach ->
  58. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  59. @DiffGenerator.rewindUpdates = sinon.stub().returns(@rewound_content)
  60. @DiffGenerator.buildDiff = sinon.stub().returns(@diff)
  61. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
  62. it "should get the latest doc and version with all recent updates", ->
  63. @DiffManager.getLatestDocAndUpdates
  64. .calledWith(@project_id, @doc_id, @from, null)
  65. .should.equal true
  66. it "should rewind the diff", ->
  67. @DiffGenerator.rewindUpdates
  68. .calledWith(@lines.join("\n"), @updates)
  69. .should.equal true
  70. it "should generate the diff", ->
  71. @DiffGenerator.buildDiff
  72. .calledWith(@rewound_content, @diffed_updates.reverse())
  73. .should.equal true
  74. it "should call the callback with the diff", ->
  75. @callback.calledWith(null, @diff).should.equal true
  76. describe "with mismatching versions", ->
  77. beforeEach ->
  78. @version = 42
  79. @updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
  80. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  81. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
  82. it "should call the callback with an error", ->
  83. @callback
  84. .calledWith(new Error("latest update version, 40, does not match doc version, 42"))
  85. .should.equal true
  86. describe "when the updates are inconsistent", ->
  87. beforeEach ->
  88. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  89. @DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
  90. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
  91. it "should call the callback with an error", ->
  92. @callback
  93. .calledWith(@error)
  94. .should.equal true