DiffManagerTests.coffee 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.getUpdatesWithUserInfo = 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.getUpdatesWithUserInfo
  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. @fromVersion = 39
  50. @toVersion = 40
  51. @diffed_updates = @updates.slice(2)
  52. @rewound_content = "rewound-content"
  53. @diff = [ u: "mock-diff" ]
  54. describe "with matching versions", ->
  55. beforeEach ->
  56. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  57. @DiffGenerator.rewindUpdates = sinon.stub().returns(@rewound_content)
  58. @DiffGenerator.buildDiff = sinon.stub().returns(@diff)
  59. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  60. it "should get the latest doc and version with all recent updates", ->
  61. @DiffManager.getLatestDocAndUpdates
  62. .calledWith(@project_id, @doc_id, @fromVersion, null)
  63. .should.equal true
  64. it "should rewind the diff", ->
  65. @DiffGenerator.rewindUpdates
  66. .calledWith(@lines.join("\n"), @updates)
  67. .should.equal true
  68. it "should generate the diff", ->
  69. @DiffGenerator.buildDiff
  70. .calledWith(@rewound_content, @diffed_updates.reverse())
  71. .should.equal true
  72. it "should call the callback with the diff", ->
  73. @callback.calledWith(null, @diff).should.equal true
  74. describe "with mismatching versions", ->
  75. beforeEach ->
  76. @version = 50
  77. @updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
  78. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  79. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  80. it "should call the callback with an error", ->
  81. @callback
  82. .calledWith(new Error("latest update version, 40, does not match doc version, 42"))
  83. .should.equal true
  84. describe "when the updates are inconsistent", ->
  85. beforeEach ->
  86. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  87. @DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
  88. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  89. it "should call the callback with an error", ->
  90. @callback
  91. .calledWith(@error)
  92. .should.equal true