HistoryManagerTests.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* eslint-disable
  2. mocha/no-nested-tests,
  3. */
  4. const SandboxedModule = require('sandboxed-module')
  5. const sinon = require('sinon')
  6. const modulePath = require('path').join(
  7. __dirname,
  8. '../../../../app/js/HistoryManager'
  9. )
  10. describe('HistoryManager', function () {
  11. beforeEach(function () {
  12. this.HistoryManager = SandboxedModule.require(modulePath, {
  13. requires: {
  14. request: (this.request = {}),
  15. '@overleaf/settings': (this.Settings = {
  16. apis: {
  17. project_history: {
  18. url: 'http://project_history.example.com',
  19. },
  20. },
  21. }),
  22. './DocumentManager': (this.DocumentManager = {}),
  23. './RedisManager': (this.RedisManager = {}),
  24. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  25. './Metrics': (this.metrics = { inc: sinon.stub() }),
  26. },
  27. })
  28. this.project_id = 'mock-project-id'
  29. this.callback = sinon.stub()
  30. })
  31. describe('flushProjectChangesAsync', function () {
  32. beforeEach(function () {
  33. this.request.post = sinon
  34. .stub()
  35. .callsArgWith(1, null, { statusCode: 204 })
  36. this.HistoryManager.flushProjectChangesAsync(this.project_id)
  37. })
  38. it('should send a request to the project history api', function () {
  39. this.request.post
  40. .calledWith({
  41. url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`,
  42. qs: { background: true },
  43. })
  44. .should.equal(true)
  45. })
  46. })
  47. describe('flushProjectChanges', function () {
  48. describe('in the normal case', function () {
  49. beforeEach(function (done) {
  50. this.request.post = sinon
  51. .stub()
  52. .callsArgWith(1, null, { statusCode: 204 })
  53. this.HistoryManager.flushProjectChanges(
  54. this.project_id,
  55. {
  56. background: true,
  57. },
  58. done
  59. )
  60. })
  61. it('should send a request to the project history api', function () {
  62. this.request.post
  63. .calledWith({
  64. url: `${this.Settings.apis.project_history.url}/project/${this.project_id}/flush`,
  65. qs: { background: true },
  66. })
  67. .should.equal(true)
  68. })
  69. })
  70. describe('with the skip_history_flush option', function () {
  71. beforeEach(function (done) {
  72. this.request.post = sinon.stub()
  73. this.HistoryManager.flushProjectChanges(
  74. this.project_id,
  75. {
  76. skip_history_flush: true,
  77. },
  78. done
  79. )
  80. })
  81. it('should not send a request to the project history api', function () {
  82. this.request.post.called.should.equal(false)
  83. })
  84. })
  85. })
  86. describe('recordAndFlushHistoryOps', function () {
  87. beforeEach(function () {
  88. this.ops = ['mock-ops']
  89. this.project_ops_length = 10
  90. this.HistoryManager.flushProjectChangesAsync = sinon.stub()
  91. })
  92. describe('with no ops', function () {
  93. beforeEach(function () {
  94. this.HistoryManager.recordAndFlushHistoryOps(
  95. this.project_id,
  96. [],
  97. this.project_ops_length
  98. )
  99. })
  100. it('should not flush project changes', function () {
  101. this.HistoryManager.flushProjectChangesAsync.called.should.equal(false)
  102. })
  103. })
  104. describe('with enough ops to flush project changes', function () {
  105. beforeEach(function () {
  106. this.HistoryManager.shouldFlushHistoryOps = sinon.stub()
  107. this.HistoryManager.shouldFlushHistoryOps
  108. .withArgs(this.project_ops_length)
  109. .returns(true)
  110. this.HistoryManager.recordAndFlushHistoryOps(
  111. this.project_id,
  112. this.ops,
  113. this.project_ops_length
  114. )
  115. })
  116. it('should flush project changes', function () {
  117. this.HistoryManager.flushProjectChangesAsync
  118. .calledWith(this.project_id)
  119. .should.equal(true)
  120. })
  121. })
  122. describe('with enough ops to flush doc changes', function () {
  123. beforeEach(function () {
  124. this.HistoryManager.shouldFlushHistoryOps = sinon.stub()
  125. this.HistoryManager.shouldFlushHistoryOps
  126. .withArgs(this.project_ops_length)
  127. .returns(false)
  128. this.HistoryManager.recordAndFlushHistoryOps(
  129. this.project_id,
  130. this.ops,
  131. this.project_ops_length
  132. )
  133. })
  134. it('should not flush project changes', function () {
  135. this.HistoryManager.flushProjectChangesAsync.called.should.equal(false)
  136. })
  137. })
  138. describe('shouldFlushHistoryOps', function () {
  139. it('should return false if the number of ops is not known', function () {
  140. this.HistoryManager.shouldFlushHistoryOps(
  141. null,
  142. ['a', 'b', 'c'].length,
  143. 1
  144. ).should.equal(false)
  145. })
  146. it("should return false if the updates didn't take us past the threshold", function () {
  147. // Currently there are 14 ops
  148. // Previously we were on 11 ops
  149. // We didn't pass over a multiple of 5
  150. this.HistoryManager.shouldFlushHistoryOps(
  151. 14,
  152. ['a', 'b', 'c'].length,
  153. 5
  154. ).should.equal(false)
  155. it('should return true if the updates took to the threshold', function () {})
  156. // Currently there are 15 ops
  157. // Previously we were on 12 ops
  158. // We've reached a new multiple of 5
  159. this.HistoryManager.shouldFlushHistoryOps(
  160. 15,
  161. ['a', 'b', 'c'].length,
  162. 5
  163. ).should.equal(true)
  164. })
  165. it('should return true if the updates took past the threshold', function () {
  166. // Currently there are 19 ops
  167. // Previously we were on 16 ops
  168. // We didn't pass over a multiple of 5
  169. this.HistoryManager.shouldFlushHistoryOps(
  170. 17,
  171. ['a', 'b', 'c'].length,
  172. 5
  173. ).should.equal(true)
  174. })
  175. })
  176. })
  177. describe('resyncProjectHistory', function () {
  178. beforeEach(function () {
  179. this.projectHistoryId = 'history-id-1234'
  180. this.docs = [
  181. {
  182. doc: this.doc_id,
  183. path: 'main.tex',
  184. },
  185. ]
  186. this.files = [
  187. {
  188. file: 'mock-file-id',
  189. path: 'universe.png',
  190. url: `www.filestore.test/${this.project_id}/mock-file-id`,
  191. },
  192. ]
  193. this.ProjectHistoryRedisManager.queueResyncProjectStructure = sinon
  194. .stub()
  195. .yields()
  196. this.DocumentManager.resyncDocContentsWithLock = sinon.stub().yields()
  197. this.HistoryManager.resyncProjectHistory(
  198. this.project_id,
  199. this.projectHistoryId,
  200. this.docs,
  201. this.files,
  202. this.callback
  203. )
  204. })
  205. it('should queue a project structure reync', function () {
  206. this.ProjectHistoryRedisManager.queueResyncProjectStructure
  207. .calledWith(
  208. this.project_id,
  209. this.projectHistoryId,
  210. this.docs,
  211. this.files
  212. )
  213. .should.equal(true)
  214. })
  215. it('should queue doc content reyncs', function () {
  216. this.DocumentManager.resyncDocContentsWithLock
  217. .calledWith(this.project_id, this.docs[0].doc, this.docs[0].path)
  218. .should.equal(true)
  219. })
  220. it('should call the callback', function () {
  221. this.callback.called.should.equal(true)
  222. })
  223. })
  224. })