DiffManagerTests.coffee 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. @content = "hello world"
  22. @version = 42
  23. @updates = [ "mock-update-1", "mock-update-2" ]
  24. @DocumentUpdaterManager.getDocument = sinon.stub().callsArgWith(2, null, @content, @version)
  25. @UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(3, 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(@project_id, @doc_id, from: @from, to: @to)
  34. .should.equal true
  35. it "should call the callback with the content, version and updates", ->
  36. @callback.calledWith(null, @content, @version, @updates).should.equal true
  37. describe "getDiff", ->
  38. beforeEach ->
  39. @content = "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.getDocumentBeforeVersion = sinon.stub().callsArgWith(3, null, @rewound_content, @updates)
  57. @DiffGenerator.buildDiff = sinon.stub().returns(@diff)
  58. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  59. it "should get the latest doc and version with all recent updates", ->
  60. @DiffManager.getDocumentBeforeVersion
  61. .calledWith(@project_id, @doc_id, @fromVersion)
  62. .should.equal true
  63. it "should generate the diff", ->
  64. @DiffGenerator.buildDiff
  65. .calledWith(@rewound_content, @diffed_updates.slice().reverse())
  66. .should.equal true
  67. it "should call the callback with the diff", ->
  68. @callback.calledWith(null, @diff).should.equal true
  69. describe "when the updates are inconsistent", ->
  70. beforeEach ->
  71. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  72. @DiffGenerator.buildDiff = sinon.stub().throws(@error = new Error("inconsistent!"))
  73. @DiffManager.getDiff @project_id, @doc_id, @fromVersion, @toVersion, @callback
  74. it "should call the callback with an error", ->
  75. @callback
  76. .calledWith(@error)
  77. .should.equal true
  78. describe "getDocumentBeforeVersion", ->
  79. beforeEach ->
  80. @content = "hello world"
  81. # Op versions are the version they were applied to, so doc is always one version
  82. # ahead.s
  83. @version = 43
  84. @updates = [
  85. { op: "mock-4", v: 42, meta: { start_ts: new Date(@to.getTime() + 20)} }
  86. { op: "mock-3", v: 41, meta: { start_ts: new Date(@to.getTime() + 10)} }
  87. { op: "mock-2", v: 40, meta: { start_ts: new Date(@to.getTime() - 10)} }
  88. { op: "mock-1", v: 39, meta: { start_ts: new Date(@to.getTime() - 20)} }
  89. ]
  90. @fromVersion = 39
  91. @rewound_content = "rewound-content"
  92. @diff = [ u: "mock-diff" ]
  93. describe "with matching versions", ->
  94. beforeEach ->
  95. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  96. @DiffGenerator.rewindUpdates = sinon.stub().returns(@rewound_content)
  97. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @fromVersion, @callback
  98. it "should get the latest doc and version with all recent updates", ->
  99. @DiffManager.getLatestDocAndUpdates
  100. .calledWith(@project_id, @doc_id, @fromVersion, null)
  101. .should.equal true
  102. it "should rewind the diff", ->
  103. @DiffGenerator.rewindUpdates
  104. .calledWith(@content, @updates.slice().reverse())
  105. .should.equal true
  106. it "should call the callback with the rewound document and updates", ->
  107. @callback.calledWith(null, @rewound_content, @updates).should.equal true
  108. describe "with mismatching versions", ->
  109. beforeEach ->
  110. @version = 50
  111. @updates = [ { op: "mock-1", v: 40 }, { op: "mock-1", v: 39 } ]
  112. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  113. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @fromVersion, @callback
  114. it "should call the callback with an error", ->
  115. @callback
  116. .calledWith(new Error("latest update version, 40, does not match doc version, 42"))
  117. .should.equal true
  118. describe "when the updates are inconsistent", ->
  119. beforeEach ->
  120. @DiffManager.getLatestDocAndUpdates = sinon.stub().callsArgWith(4, null, @content, @version, @updates)
  121. @DiffGenerator.rewindUpdates = sinon.stub().throws(@error = new Error("inconsistent!"))
  122. @DiffManager.getDocumentBeforeVersion @project_id, @doc_id, @fromVersion, @callback
  123. it "should call the callback with an error", ->
  124. @callback
  125. .calledWith(@error)
  126. .should.equal true