ResourceWriter.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. import { vi, expect, describe, beforeEach, it } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(
  5. import.meta.dirname,
  6. '../../../app/js/ResourceWriter'
  7. )
  8. describe('ResourceWriter', () => {
  9. beforeEach(async ctx => {
  10. let Timer
  11. vi.doMock('fs', () => ({
  12. default: (ctx.fs = {
  13. mkdir: sinon.stub().callsArg(1),
  14. unlink: sinon.stub().callsArg(1),
  15. }),
  16. }))
  17. vi.doMock('../../../app/js/ResourceStateManager', () => ({
  18. default: (ctx.ResourceStateManager = {}),
  19. }))
  20. vi.doMock('../../../app/js/UrlCache', () => ({
  21. default: (ctx.UrlCache = {
  22. createProjectDir: sinon.stub().yields(),
  23. }),
  24. }))
  25. vi.doMock('../../../app/js/OutputFileFinder', () => ({
  26. default: (ctx.OutputFileFinder = {}),
  27. }))
  28. vi.doMock('@overleaf/metrics', () => ({
  29. // Mocks allow us to import Metrics.js twice without getting errors.
  30. prom: {
  31. Gauge: sinon.stub(),
  32. Histogram: sinon.stub(),
  33. Counter: sinon.stub(),
  34. },
  35. default: (ctx.Metrics = {
  36. inc: sinon.stub(),
  37. Timer: (Timer = (function () {
  38. Timer = class Timer {
  39. static initClass() {
  40. this.prototype.done = sinon.stub()
  41. }
  42. }
  43. Timer.initClass()
  44. return Timer
  45. })()),
  46. }),
  47. }))
  48. ctx.ResourceWriter = (await import(modulePath)).default
  49. ctx.project_id = 'project-id-123'
  50. ctx.basePath = '/path/to/write/files/to'
  51. return (ctx.callback = sinon.stub())
  52. })
  53. describe('syncResourcesToDisk on a full request', () => {
  54. beforeEach(ctx => {
  55. ctx.resources = ['resource-1-mock', 'resource-2-mock', 'resource-3-mock']
  56. ctx.request = {
  57. project_id: ctx.project_id,
  58. syncState: (ctx.syncState = '0123456789abcdef'),
  59. resources: ctx.resources,
  60. }
  61. ctx.ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  62. ctx.ResourceWriter._removeExtraneousFiles = sinon.stub().yields(null)
  63. ctx.ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
  64. return ctx.ResourceWriter.syncResourcesToDisk(
  65. ctx.request,
  66. ctx.basePath,
  67. ctx.callback
  68. )
  69. })
  70. it('should remove old files', ctx => {
  71. return ctx.ResourceWriter._removeExtraneousFiles
  72. .calledWith(ctx.request, ctx.resources, ctx.basePath)
  73. .should.equal(true)
  74. })
  75. it('should write each resource to disk', ctx => {
  76. return Array.from(ctx.resources).map(resource =>
  77. ctx.ResourceWriter._writeResourceToDisk
  78. .calledWith(ctx.project_id, resource, ctx.basePath)
  79. .should.equal(true)
  80. )
  81. })
  82. it('should store the sync state and resource list', ctx => {
  83. return ctx.ResourceStateManager.saveProjectState
  84. .calledWith(ctx.syncState, ctx.resources, ctx.basePath)
  85. .should.equal(true)
  86. })
  87. return it('should call the callback', ctx => {
  88. return ctx.callback.called.should.equal(true)
  89. })
  90. })
  91. describe('syncResourcesToDisk on an incremental update', () => {
  92. beforeEach(ctx => {
  93. ctx.resources = ['resource-1-mock']
  94. ctx.request = {
  95. project_id: ctx.project_id,
  96. syncType: 'incremental',
  97. syncState: (ctx.syncState = '1234567890abcdef'),
  98. resources: ctx.resources,
  99. }
  100. ctx.fullResources = ctx.resources.concat(['file-1'])
  101. ctx.ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  102. ctx.ResourceWriter._removeExtraneousFiles = sinon
  103. .stub()
  104. .yields(null, (ctx.outputFiles = []), (ctx.allFiles = []))
  105. ctx.ResourceStateManager.checkProjectStateMatches = sinon
  106. .stub()
  107. .callsArgWith(2, null, ctx.fullResources)
  108. ctx.ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
  109. ctx.ResourceStateManager.checkResourceFiles = sinon.stub().callsArg(3)
  110. return ctx.ResourceWriter.syncResourcesToDisk(
  111. ctx.request,
  112. ctx.basePath,
  113. ctx.callback
  114. )
  115. })
  116. it('should check the sync state matches', ctx => {
  117. return ctx.ResourceStateManager.checkProjectStateMatches
  118. .calledWith(ctx.syncState, ctx.basePath)
  119. .should.equal(true)
  120. })
  121. it('should remove old files', ctx => {
  122. return ctx.ResourceWriter._removeExtraneousFiles
  123. .calledWith(ctx.request, ctx.fullResources, ctx.basePath)
  124. .should.equal(true)
  125. })
  126. it('should check each resource exists', ctx => {
  127. return ctx.ResourceStateManager.checkResourceFiles
  128. .calledWith(ctx.fullResources, ctx.allFiles, ctx.basePath)
  129. .should.equal(true)
  130. })
  131. it('should write each resource to disk', ctx => {
  132. return Array.from(ctx.resources).map(resource =>
  133. ctx.ResourceWriter._writeResourceToDisk
  134. .calledWith(ctx.project_id, resource, ctx.basePath)
  135. .should.equal(true)
  136. )
  137. })
  138. return it('should call the callback', ctx => {
  139. return ctx.callback.called.should.equal(true)
  140. })
  141. })
  142. describe('syncResourcesToDisk on an incremental update when the state does not match', () => {
  143. beforeEach(ctx => {
  144. ctx.resources = ['resource-1-mock']
  145. ctx.request = {
  146. project_id: ctx.project_id,
  147. syncType: 'incremental',
  148. syncState: (ctx.syncState = '1234567890abcdef'),
  149. resources: ctx.resources,
  150. }
  151. ctx.ResourceStateManager.checkProjectStateMatches = sinon
  152. .stub()
  153. .callsArgWith(2, (ctx.error = new Error()))
  154. return ctx.ResourceWriter.syncResourcesToDisk(
  155. ctx.request,
  156. ctx.basePath,
  157. ctx.callback
  158. )
  159. })
  160. it('should check whether the sync state matches', ctx => {
  161. return ctx.ResourceStateManager.checkProjectStateMatches
  162. .calledWith(ctx.syncState, ctx.basePath)
  163. .should.equal(true)
  164. })
  165. return it('should call the callback with an error', ctx => {
  166. return ctx.callback.calledWith(ctx.error).should.equal(true)
  167. })
  168. })
  169. describe('_removeExtraneousFiles', () => {
  170. beforeEach(ctx => {
  171. ctx.output_files = [
  172. {
  173. path: 'output.pdf',
  174. type: 'pdf',
  175. },
  176. {
  177. path: 'extra/file.tex',
  178. type: 'tex',
  179. },
  180. {
  181. path: 'extra.aux',
  182. type: 'aux',
  183. },
  184. {
  185. path: 'cache/_chunk1',
  186. },
  187. {
  188. path: 'figures/image-eps-converted-to.pdf',
  189. type: 'pdf',
  190. },
  191. {
  192. path: 'foo/main-figure0.md5',
  193. type: 'md5',
  194. },
  195. {
  196. path: 'foo/main-figure0.dpth',
  197. type: 'dpth',
  198. },
  199. {
  200. path: 'foo/main-figure0.pdf',
  201. type: 'pdf',
  202. },
  203. {
  204. path: '_minted-main/default-pyg-prefix.pygstyle',
  205. type: 'pygstyle',
  206. },
  207. {
  208. path: '_minted-main/default.pygstyle',
  209. type: 'pygstyle',
  210. },
  211. {
  212. path: '_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex',
  213. type: 'pygtex',
  214. },
  215. {
  216. path: '_markdown_main/30893013dec5d869a415610079774c2f.md.tex',
  217. type: 'tex',
  218. },
  219. {
  220. path: 'output.stdout',
  221. },
  222. {
  223. path: 'output.stderr',
  224. },
  225. ]
  226. ctx.resources = 'mock-resources'
  227. ctx.request = {
  228. project_id: ctx.project_id,
  229. syncType: 'incremental',
  230. syncState: (ctx.syncState = '1234567890abcdef'),
  231. resources: ctx.resources,
  232. metricsOpts: { path: 'foo' },
  233. }
  234. ctx.OutputFileFinder.findOutputFiles = sinon
  235. .stub()
  236. .callsArgWith(2, null, ctx.output_files)
  237. ctx.ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
  238. return ctx.ResourceWriter._removeExtraneousFiles(
  239. ctx.request,
  240. ctx.resources,
  241. ctx.basePath,
  242. ctx.callback
  243. )
  244. })
  245. it('should find the existing output files', ctx => {
  246. return ctx.OutputFileFinder.findOutputFiles
  247. .calledWith(ctx.resources, ctx.basePath)
  248. .should.equal(true)
  249. })
  250. it('should delete the output files', ctx => {
  251. return ctx.ResourceWriter._deleteFileIfNotDirectory
  252. .calledWith(path.join(ctx.basePath, 'output.pdf'))
  253. .should.equal(true)
  254. })
  255. it('should delete the stdout log file', ctx => {
  256. return ctx.ResourceWriter._deleteFileIfNotDirectory
  257. .calledWith(path.join(ctx.basePath, 'output.stdout'))
  258. .should.equal(true)
  259. })
  260. it('should delete the stderr log file', ctx => {
  261. return ctx.ResourceWriter._deleteFileIfNotDirectory
  262. .calledWith(path.join(ctx.basePath, 'output.stderr'))
  263. .should.equal(true)
  264. })
  265. it('should delete the extra files', ctx => {
  266. return ctx.ResourceWriter._deleteFileIfNotDirectory
  267. .calledWith(path.join(ctx.basePath, 'extra/file.tex'))
  268. .should.equal(true)
  269. })
  270. it('should not delete the extra aux files', ctx => {
  271. return ctx.ResourceWriter._deleteFileIfNotDirectory
  272. .calledWith(path.join(ctx.basePath, 'extra.aux'))
  273. .should.equal(false)
  274. })
  275. it('should not delete the knitr cache file', ctx => {
  276. return ctx.ResourceWriter._deleteFileIfNotDirectory
  277. .calledWith(path.join(ctx.basePath, 'cache/_chunk1'))
  278. .should.equal(false)
  279. })
  280. it('should not delete the epstopdf converted files', ctx => {
  281. return ctx.ResourceWriter._deleteFileIfNotDirectory
  282. .calledWith(
  283. path.join(ctx.basePath, 'figures/image-eps-converted-to.pdf')
  284. )
  285. .should.equal(false)
  286. })
  287. it('should not delete the tikz md5 files', ctx => {
  288. return ctx.ResourceWriter._deleteFileIfNotDirectory
  289. .calledWith(path.join(ctx.basePath, 'foo/main-figure0.md5'))
  290. .should.equal(false)
  291. })
  292. it('should not delete the tikz dpth files', ctx => {
  293. return ctx.ResourceWriter._deleteFileIfNotDirectory
  294. .calledWith(path.join(ctx.basePath, 'foo/main-figure0.dpth'))
  295. .should.equal(false)
  296. })
  297. it('should not delete the tikz pdf files', ctx => {
  298. return ctx.ResourceWriter._deleteFileIfNotDirectory
  299. .calledWith(path.join(ctx.basePath, 'foo/main-figure0.pdf'))
  300. .should.equal(false)
  301. })
  302. it('should not delete the minted pygstyle files', ctx => {
  303. return ctx.ResourceWriter._deleteFileIfNotDirectory
  304. .calledWith(
  305. path.join(ctx.basePath, '_minted-main/default-pyg-prefix.pygstyle')
  306. )
  307. .should.equal(false)
  308. })
  309. it('should not delete the minted default pygstyle files', ctx => {
  310. return ctx.ResourceWriter._deleteFileIfNotDirectory
  311. .calledWith(path.join(ctx.basePath, '_minted-main/default.pygstyle'))
  312. .should.equal(false)
  313. })
  314. it('should not delete the minted default pygtex files', ctx => {
  315. return ctx.ResourceWriter._deleteFileIfNotDirectory
  316. .calledWith(
  317. path.join(
  318. ctx.basePath,
  319. '_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex'
  320. )
  321. )
  322. .should.equal(false)
  323. })
  324. it('should not delete the markdown md.tex files', ctx => {
  325. return ctx.ResourceWriter._deleteFileIfNotDirectory
  326. .calledWith(
  327. path.join(
  328. ctx.basePath,
  329. '_markdown_main/30893013dec5d869a415610079774c2f.md.tex'
  330. )
  331. )
  332. .should.equal(false)
  333. })
  334. it('should call the callback', ctx => {
  335. return ctx.callback.called.should.equal(true)
  336. })
  337. return it('should time the request', ctx => {
  338. return ctx.Metrics.Timer.prototype.done.called.should.equal(true)
  339. })
  340. })
  341. describe('_writeResourceToDisk', () => {
  342. describe('with a url based resource', () => {
  343. beforeEach(ctx => {
  344. ctx.fs.mkdir = sinon.stub().callsArg(2)
  345. ctx.resource = {
  346. path: 'main.tex',
  347. url: 'http://www.example.com/primary/main.tex',
  348. fallbackURL: 'http://fallback.example.com/fallback/main.tex',
  349. modified: Date.now(),
  350. }
  351. ctx.UrlCache.downloadUrlToFile = sinon
  352. .stub()
  353. .callsArgWith(5, 'fake error downloading file')
  354. return ctx.ResourceWriter._writeResourceToDisk(
  355. ctx.project_id,
  356. ctx.resource,
  357. ctx.basePath,
  358. ctx.callback
  359. )
  360. })
  361. it('should ensure the directory exists', ctx => {
  362. ctx.fs.mkdir
  363. .calledWith(path.dirname(path.join(ctx.basePath, ctx.resource.path)))
  364. .should.equal(true)
  365. })
  366. it('should write the URL from the cache', ctx => {
  367. return ctx.UrlCache.downloadUrlToFile
  368. .calledWith(
  369. ctx.project_id,
  370. ctx.resource.url,
  371. ctx.resource.fallbackURL,
  372. path.join(ctx.basePath, ctx.resource.path),
  373. ctx.resource.modified
  374. )
  375. .should.equal(true)
  376. })
  377. it('should call the callback', ctx => {
  378. return ctx.callback.called.should.equal(true)
  379. })
  380. return it('should not return an error if the resource writer errored', ctx => {
  381. return expect(ctx.callback.args[0][0]).not.to.exist
  382. })
  383. })
  384. describe('with a content based resource', () => {
  385. beforeEach(ctx => {
  386. ctx.resource = {
  387. path: 'main.tex',
  388. content: 'Hello world',
  389. }
  390. ctx.fs.writeFile = sinon.stub().callsArg(2)
  391. ctx.fs.mkdir = sinon.stub().callsArg(2)
  392. return ctx.ResourceWriter._writeResourceToDisk(
  393. ctx.project_id,
  394. ctx.resource,
  395. ctx.basePath,
  396. ctx.callback
  397. )
  398. })
  399. it('should ensure the directory exists', ctx => {
  400. return ctx.fs.mkdir
  401. .calledWith(path.dirname(path.join(ctx.basePath, ctx.resource.path)))
  402. .should.equal(true)
  403. })
  404. it('should write the contents to disk', ctx => {
  405. return ctx.fs.writeFile
  406. .calledWith(
  407. path.join(ctx.basePath, ctx.resource.path),
  408. ctx.resource.content
  409. )
  410. .should.equal(true)
  411. })
  412. return it('should call the callback', ctx => {
  413. return ctx.callback.called.should.equal(true)
  414. })
  415. })
  416. return describe('with a file path that breaks out of the root folder', () => {
  417. beforeEach(ctx => {
  418. ctx.resource = {
  419. path: '../../main.tex',
  420. content: 'Hello world',
  421. }
  422. ctx.fs.writeFile = sinon.stub().callsArg(2)
  423. return ctx.ResourceWriter._writeResourceToDisk(
  424. ctx.project_id,
  425. ctx.resource,
  426. ctx.basePath,
  427. ctx.callback
  428. )
  429. })
  430. it('should not write to disk', ctx => {
  431. return ctx.fs.writeFile.called.should.equal(false)
  432. })
  433. it('should return an error', ctx => {
  434. ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
  435. const message = ctx.callback.args[0][0].message
  436. expect(message).to.include('resource path is outside root directory')
  437. })
  438. })
  439. })
  440. return describe('checkPath', () => {
  441. describe('with a valid path', () => {
  442. beforeEach(ctx => {
  443. return ctx.ResourceWriter.checkPath('foo', 'bar', ctx.callback)
  444. })
  445. return it('should return the joined path', ctx => {
  446. return ctx.callback.calledWith(null, 'foo/bar').should.equal(true)
  447. })
  448. })
  449. describe('with an invalid path', () => {
  450. beforeEach(ctx => {
  451. ctx.ResourceWriter.checkPath('foo', 'baz/../../bar', ctx.callback)
  452. })
  453. it('should return an error', ctx => {
  454. ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
  455. const message = ctx.callback.args[0][0].message
  456. expect(message).to.include('resource path is outside root directory')
  457. })
  458. })
  459. describe('with another invalid path matching on a prefix', () => {
  460. beforeEach(ctx => {
  461. return ctx.ResourceWriter.checkPath(
  462. 'foo',
  463. '../foobar/baz',
  464. ctx.callback
  465. )
  466. })
  467. it('should return an error', ctx => {
  468. ctx.callback.calledWith(sinon.match(Error)).should.equal(true)
  469. const message = ctx.callback.args[0][0].message
  470. expect(message).to.include('resource path is outside root directory')
  471. })
  472. })
  473. })
  474. })