HistoryManagerTests.js 8.1 KB

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