DiffManagerTests.coffee 3.9 KB

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