DiffManagerTests.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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-1", v: 41, meta: { end_ts: new Date(@to.getTime() - 10)} }
  49. { op: "mock-2", v: 42, meta: { end_ts: new Date(@to.getTime() + 10)} }
  50. ]
  51. @diffed_updates = @updates.slice(0,1)
  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, @from, @to, @callback
  60. it "should get the latest doc and version with all recent updates", ->
  61. @DiffManager.getLatestDocAndUpdates
  62. .calledWith(@project_id, @doc_id, @from, 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)
  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 = 42
  77. @updates = [ { op: "mock-1", v: 39 }, { op: "mock-1", v: 40 } ]
  78. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @lines, @version, @updates)
  79. @DiffManager.getDiff @project_id, @doc_id, @from, @to, @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, @from, @to, @callback
  89. it "should call the callback with an error", ->
  90. @callback
  91. .calledWith(@error)
  92. .should.equal true