DiffManagerTests.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "./UpdatesManager": @UpdatesManager = {}
  12. "./DocumentUpdaterManager": @DocumentUpdaterManager = {}
  13. "./DiffGenerator": @DiffGenerator = {}
  14. @callback = sinon.stub()
  15. @from = new Date()
  16. @to = new Date(Date.now() + 10000)
  17. @project_id = "mock-project-id"
  18. @doc_id = "mock-doc-id"
  19. describe "getLatestDocAndUpdates", ->
  20. beforeEach ->
  21. @lines = [ "hello", "world" ]
  22. @version = 42
  23. @updates = [ "mock-update-1", "mock-update-2" ]
  24. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(2, null, @lines, @version)
  25. @UpdatesManager.getUpdates = sinon.stub().callsArgWith(2, null, @updates)
  26. @DiffManager.getLatestDocAndUpdates @project_id, @doc_id, @from, @to, @callback
  27. it "should get the latest version of the doc", ->
  28. @DocumentUpdaterManager.getDocument
  29. .calledWith(@project_id, @doc_id)
  30. .should.equal true
  31. it "should get the latest updates", ->
  32. @UpdatesManager.getUpdates
  33. .calledWith(@doc_id, from: @from, to: @to)
  34. .should.equal true
  35. it "should call the callback with the lines, version and updates", ->
  36. @callback.calledWith(null, @lines, @version, @updates).should.equal true
  37. describe "getDiff", ->
  38. beforeEach ->
  39. @lines = [ "hello", "world" ]
  40. # Op versions are the version they were applied to, so doc is always one version
  41. # ahead.s
  42. @version = 43
  43. @updates = [
  44. { op: "mock-4", v: 42, meta: { start_ts: new Date(@to.getTime() + 20)} }
  45. { op: "mock-3", v: 41, meta: { start_ts: new Date(@to.getTime() + 10)} }
  46. { op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
  47. { op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
  48. ]
  49. @diffed_updates = @updates.slice(2)
  50. @rewound_content = "rewound-content"
  51. @diff = [ u: "mock-diff" ]
  52. describe "with matching versions", ->
  53. beforeEach ->
  54. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  55. @DiffGenerator.rewindUpdates = sinon.stub().returns(@rewound_content)
  56. @DiffGenerator.buildDiff = sinon.stub().returns(@diff)
  57. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
  58. it "should get the latest doc and version with all recent updates", ->
  59. @DiffManager.getLatestDocAndUpdates
  60. .calledWith(@project_id, @doc_id, @from, null)
  61. .should.equal true
  62. it "should rewind the diff", ->
  63. @DiffGenerator.rewindUpdates
  64. .calledWith(@lines.join("\n"), @updates)
  65. .should.equal true
  66. it "should generate the diff", ->
  67. @DiffGenerator.buildDiff
  68. .calledWith(@rewound_content, @diffed_updates.reverse())
  69. .should.equal true
  70. it "should call the callback with the diff", ->
  71. @callback.calledWith(null, @diff).should.equal true
  72. describe "with mismatching versions", ->
  73. beforeEach ->
  74. @version = 50
  75. @updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
  76. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  77. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
  78. it "should call the callback with an error", ->
  79. @callback
  80. .calledWith(new Error("latest update version, 40, does not match doc version, 42"))
  81. .should.equal true
  82. describe "when the updates are inconsistent", ->
  83. beforeEach ->
  84. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  85. @DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
  86. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @callback
  87. it "should call the callback with an error", ->
  88. @callback
  89. .calledWith(@error)
  90. .should.equal true