ContentCacheManagerTests.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. const fs = require('fs')
  2. const Path = require('path')
  3. const { expect } = require('chai')
  4. const MODULE_PATH = '../../../app/js/ContentCacheManager'
  5. describe('ContentCacheManager', function () {
  6. let contentDir, pdfPath
  7. let ContentCacheManager, files, Settings
  8. before(function () {
  9. Settings = require('@overleaf/settings')
  10. ContentCacheManager = require(MODULE_PATH)
  11. })
  12. let contentRanges, newContentRanges, reclaimed
  13. async function run(filePath, size) {
  14. const result = await ContentCacheManager.promises.update(
  15. contentDir,
  16. filePath,
  17. size
  18. )
  19. let newlyReclaimed
  20. ;({
  21. contentRanges,
  22. newContentRanges,
  23. reclaimedSpace: newlyReclaimed,
  24. } = result)
  25. reclaimed += newlyReclaimed
  26. const fileNames = await fs.promises.readdir(contentDir)
  27. files = {}
  28. for (const fileName of fileNames) {
  29. const path = Path.join(contentDir, fileName)
  30. files[path] = await fs.promises.readFile(path)
  31. }
  32. }
  33. before(function () {
  34. contentDir =
  35. '/overleaf/services/clsi/output/602cee6f6460fca0ba7921e6/content/1797a7f48f9-5abc1998509dea1f'
  36. pdfPath =
  37. '/overleaf/services/clsi/output/602cee6f6460fca0ba7921e6/generated-files/1797a7f48ea-8ac6805139f43351/output.pdf'
  38. reclaimed = 0
  39. Settings.pdfCachingMinChunkSize = 1024
  40. })
  41. before(async function () {
  42. await fs.promises.rm(contentDir, { recursive: true, force: true })
  43. await fs.promises.mkdir(contentDir, { recursive: true })
  44. await fs.promises.mkdir(Path.dirname(pdfPath), { recursive: true })
  45. })
  46. describe('minimal', function () {
  47. const PATH_MINIMAL = 'test/acceptance/fixtures/minimal.pdf'
  48. const OBJECT_ID_1 = '9 0 '
  49. const HASH_LARGE =
  50. 'd7cfc73ad2fba4578a437517923e3714927bbf35e63ea88bd93c7a8076cf1fcd'
  51. const OBJECT_ID_2 = '10 0 '
  52. const HASH_SMALL =
  53. '896749b8343851b0dc385f71616916a7ba0434fcfb56d1fc7e27cd139eaa2f71'
  54. function getChunkPath(hash) {
  55. return Path.join('test/unit/js/snapshots/minimalCompile/chunks', hash)
  56. }
  57. let MINIMAL_SIZE, RANGE_1, RANGE_2, h1, h2, START_1, START_2, END_1, END_2
  58. before(async function () {
  59. await fs.promises.copyFile(PATH_MINIMAL, pdfPath)
  60. const MINIMAL = await fs.promises.readFile(PATH_MINIMAL)
  61. MINIMAL_SIZE = (await fs.promises.stat(PATH_MINIMAL)).size
  62. RANGE_1 = await fs.promises.readFile(getChunkPath(HASH_LARGE))
  63. RANGE_2 = await fs.promises.readFile(getChunkPath(HASH_SMALL))
  64. h1 = HASH_LARGE
  65. h2 = HASH_SMALL
  66. START_1 = MINIMAL.indexOf(RANGE_1)
  67. END_1 = START_1 + RANGE_1.byteLength
  68. START_2 = MINIMAL.indexOf(RANGE_2)
  69. END_2 = START_2 + RANGE_2.byteLength
  70. })
  71. async function runWithMinimal() {
  72. await run(pdfPath, MINIMAL_SIZE)
  73. }
  74. describe('with two ranges qualifying', function () {
  75. before(function () {
  76. Settings.pdfCachingMinChunkSize = 500
  77. })
  78. before(async function () {
  79. await runWithMinimal()
  80. })
  81. it('should produce two ranges', function () {
  82. expect(contentRanges).to.have.length(2)
  83. })
  84. it('should find the correct offsets', function () {
  85. expect(contentRanges).to.deep.equal([
  86. {
  87. objectId: OBJECT_ID_1,
  88. start: START_1,
  89. end: END_1,
  90. hash: h1,
  91. },
  92. {
  93. objectId: OBJECT_ID_2,
  94. start: START_2,
  95. end: END_2,
  96. hash: h2,
  97. },
  98. ])
  99. })
  100. it('should store the contents', function () {
  101. expect(files).to.deep.equal({
  102. [Path.join(contentDir, h1)]: RANGE_1,
  103. [Path.join(contentDir, h2)]: RANGE_2,
  104. [Path.join(contentDir, '.state.v0.json')]: Buffer.from(
  105. JSON.stringify({
  106. hashAge: [
  107. [h1, 0],
  108. [h2, 0],
  109. ],
  110. hashSize: [
  111. [h1, RANGE_1.byteLength],
  112. [h2, RANGE_2.byteLength],
  113. ],
  114. })
  115. ),
  116. })
  117. })
  118. it('should mark all ranges as new', function () {
  119. expect(contentRanges).to.deep.equal(newContentRanges)
  120. })
  121. describe('when re-running with one range too small', function () {
  122. before(function () {
  123. Settings.pdfCachingMinChunkSize = 1024
  124. })
  125. before(async function () {
  126. await runWithMinimal()
  127. })
  128. it('should produce one range', function () {
  129. expect(contentRanges).to.have.length(1)
  130. })
  131. it('should find the correct offsets', function () {
  132. expect(contentRanges).to.deep.equal([
  133. {
  134. objectId: OBJECT_ID_1,
  135. start: START_1,
  136. end: END_1,
  137. hash: h1,
  138. },
  139. ])
  140. })
  141. it('should update the age of the 2nd range', function () {
  142. expect(files).to.deep.equal({
  143. [Path.join(contentDir, h1)]: RANGE_1,
  144. [Path.join(contentDir, h2)]: RANGE_2,
  145. [Path.join(contentDir, '.state.v0.json')]: Buffer.from(
  146. JSON.stringify({
  147. hashAge: [
  148. [h1, 0],
  149. [h2, 1],
  150. ],
  151. hashSize: [
  152. [h1, RANGE_1.byteLength],
  153. [h2, RANGE_2.byteLength],
  154. ],
  155. })
  156. ),
  157. })
  158. })
  159. it('should find no new ranges', function () {
  160. expect(newContentRanges).to.deep.equal([])
  161. })
  162. describe('when re-running 5 more times', function () {
  163. for (let i = 0; i < 5; i++) {
  164. before(async function () {
  165. await runWithMinimal()
  166. })
  167. }
  168. it('should still produce one range', function () {
  169. expect(contentRanges).to.have.length(1)
  170. })
  171. it('should still find the correct offsets', function () {
  172. expect(contentRanges).to.deep.equal([
  173. {
  174. objectId: OBJECT_ID_1,
  175. start: START_1,
  176. end: END_1,
  177. hash: h1,
  178. },
  179. ])
  180. })
  181. it('should delete the 2nd range', function () {
  182. expect(files).to.deep.equal({
  183. [Path.join(contentDir, h1)]: RANGE_1,
  184. [Path.join(contentDir, '.state.v0.json')]: Buffer.from(
  185. JSON.stringify({
  186. hashAge: [[h1, 0]],
  187. hashSize: [[h1, RANGE_1.byteLength]],
  188. })
  189. ),
  190. })
  191. })
  192. it('should find no new ranges', function () {
  193. expect(newContentRanges).to.deep.equal([])
  194. })
  195. it('should yield the reclaimed space', function () {
  196. expect(reclaimed).to.equal(RANGE_2.byteLength)
  197. })
  198. })
  199. })
  200. })
  201. })
  202. })