HistoryManagerTests.coffee 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/HistoryManager'
  5. describe "HistoryManager", ->
  6. beforeEach ->
  7. @HistoryManager = SandboxedModule.require modulePath, requires:
  8. "request": @request = {}
  9. "settings-sharelatex": @Settings = {
  10. apis:
  11. project_history:
  12. enabled: true
  13. url: "http://project_history.example.com"
  14. trackchanges:
  15. url: "http://trackchanges.example.com"
  16. }
  17. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  18. "./DocumentManager": @DocumentManager = {}
  19. "./HistoryRedisManager": @HistoryRedisManager = {}
  20. "./RedisManager": @RedisManager = {}
  21. "./ProjectHistoryRedisManager": @ProjectHistoryRedisManager = {}
  22. @project_id = "mock-project-id"
  23. @doc_id = "mock-doc-id"
  24. @callback = sinon.stub()
  25. describe "flushDocChangesAsync", ->
  26. beforeEach ->
  27. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  28. @HistoryManager.flushDocChangesAsync @project_id, @doc_id
  29. it "should send a request to the track changes api", ->
  30. @request.post
  31. .calledWith("#{@Settings.apis.trackchanges.url}/project/#{@project_id}/doc/#{@doc_id}/flush")
  32. .should.equal true
  33. describe "flushProjectChangesAsync", ->
  34. beforeEach ->
  35. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  36. @HistoryManager.flushProjectChangesAsync @project_id
  37. it "should send a request to the project history api", ->
  38. @request.post
  39. .calledWith({url: "#{@Settings.apis.project_history.url}/project/#{@project_id}/flush", qs:{background:true}})
  40. .should.equal true
  41. describe "recordAndFlushHistoryOps", ->
  42. beforeEach ->
  43. @ops = [ 'mock-ops' ]
  44. @project_ops_length = 10
  45. @doc_ops_length = 5
  46. @HistoryManager.flushProjectChangesAsync = sinon.stub()
  47. @HistoryRedisManager.recordDocHasHistoryOps = sinon.stub().callsArg(3)
  48. @HistoryManager.flushDocChangesAsync = sinon.stub()
  49. describe "with no ops", ->
  50. beforeEach ->
  51. @HistoryManager.recordAndFlushHistoryOps(
  52. @project_id, @doc_id, [], @doc_ops_length, @project_ops_length, @callback
  53. )
  54. it "should not flush project changes", ->
  55. @HistoryManager.flushProjectChangesAsync.called.should.equal false
  56. it "should not record doc has history ops", ->
  57. @HistoryRedisManager.recordDocHasHistoryOps.called.should.equal false
  58. it "should not flush doc changes", ->
  59. @HistoryManager.flushDocChangesAsync.called.should.equal false
  60. it "should call the callback", ->
  61. @callback.called.should.equal true
  62. describe "with enough ops to flush project changes", ->
  63. beforeEach ->
  64. @HistoryManager.shouldFlushHistoryOps = sinon.stub()
  65. @HistoryManager.shouldFlushHistoryOps.withArgs(@project_ops_length).returns(true)
  66. @HistoryManager.shouldFlushHistoryOps.withArgs(@doc_ops_length).returns(false)
  67. @HistoryManager.recordAndFlushHistoryOps(
  68. @project_id, @doc_id, @ops, @doc_ops_length, @project_ops_length, @callback
  69. )
  70. it "should flush project changes", ->
  71. @HistoryManager.flushProjectChangesAsync
  72. .calledWith(@project_id)
  73. .should.equal true
  74. it "should record doc has history ops", ->
  75. @HistoryRedisManager.recordDocHasHistoryOps
  76. .calledWith(@project_id, @doc_id, @ops)
  77. it "should not flush doc changes", ->
  78. @HistoryManager.flushDocChangesAsync.called.should.equal false
  79. it "should call the callback", ->
  80. @callback.called.should.equal true
  81. describe "with enough ops to flush doc changes", ->
  82. beforeEach ->
  83. @HistoryManager.shouldFlushHistoryOps = sinon.stub()
  84. @HistoryManager.shouldFlushHistoryOps.withArgs(@project_ops_length).returns(false)
  85. @HistoryManager.shouldFlushHistoryOps.withArgs(@doc_ops_length).returns(true)
  86. @HistoryManager.recordAndFlushHistoryOps(
  87. @project_id, @doc_id, @ops, @doc_ops_length, @project_ops_length, @callback
  88. )
  89. it "should not flush project changes", ->
  90. @HistoryManager.flushProjectChangesAsync.called.should.equal false
  91. it "should record doc has history ops", ->
  92. @HistoryRedisManager.recordDocHasHistoryOps
  93. .calledWith(@project_id, @doc_id, @ops)
  94. it "should flush doc changes", ->
  95. @HistoryManager.flushDocChangesAsync
  96. .calledWith(@project_id, @doc_id)
  97. .should.equal true
  98. it "should call the callback", ->
  99. @callback.called.should.equal true
  100. describe "when recording doc has history ops errors", ->
  101. beforeEach ->
  102. @error = new Error("error")
  103. @HistoryRedisManager.recordDocHasHistoryOps =
  104. sinon.stub().callsArgWith(3, @error)
  105. @HistoryManager.recordAndFlushHistoryOps(
  106. @project_id, @doc_id, @ops, @doc_ops_length, @project_ops_length, @callback
  107. )
  108. it "should not flush doc changes", ->
  109. @HistoryManager.flushDocChangesAsync.called.should.equal false
  110. it "should call the callback with the error", ->
  111. @callback.calledWith(@error).should.equal true
  112. describe "shouldFlushHistoryOps", ->
  113. it "should return false if the number of ops is not known", ->
  114. @HistoryManager.shouldFlushHistoryOps(null, ['a', 'b', 'c'].length, 1).should.equal false
  115. it "should return false if the updates didn't take us past the threshold", ->
  116. # Currently there are 14 ops
  117. # Previously we were on 11 ops
  118. # We didn't pass over a multiple of 5
  119. @HistoryManager.shouldFlushHistoryOps(14, ['a', 'b', 'c'].length, 5).should.equal false
  120. it "should return true if the updates took to the threshold", ->
  121. # Currently there are 15 ops
  122. # Previously we were on 12 ops
  123. # We've reached a new multiple of 5
  124. @HistoryManager.shouldFlushHistoryOps(15, ['a', 'b', 'c'].length, 5).should.equal true
  125. it "should return true if the updates took past the threshold", ->
  126. # Currently there are 19 ops
  127. # Previously we were on 16 ops
  128. # We didn't pass over a multiple of 5
  129. @HistoryManager.shouldFlushHistoryOps(17, ['a', 'b', 'c'].length, 5).should.equal true
  130. describe "resyncProjectHistory", ->
  131. beforeEach ->
  132. @projectHistoryId = 'history-id-1234'
  133. @docs = [
  134. doc: @doc_id
  135. path: 'main.tex'
  136. ]
  137. @files = [
  138. file: 'mock-file-id'
  139. path: 'universe.png'
  140. url: "www.filestore.test/#{@project_id}/mock-file-id"
  141. ]
  142. @ProjectHistoryRedisManager.queueResyncProjectStructure = sinon.stub().yields()
  143. @DocumentManager.resyncDocContentsWithLock = sinon.stub().yields()
  144. @HistoryManager.resyncProjectHistory @project_id, @projectHistoryId, @docs, @files, @callback
  145. it "should queue a project structure reync", ->
  146. @ProjectHistoryRedisManager.queueResyncProjectStructure
  147. .calledWith(@project_id, @projectHistoryId, @docs, @files)
  148. .should.equal true
  149. it "should queue doc content reyncs", ->
  150. @DocumentManager
  151. .resyncDocContentsWithLock
  152. .calledWith(@project_id, @doc_id)
  153. .should.equal true
  154. it "should call the callback", ->
  155. @callback.called.should.equal true