ConversionOutputCleaner.test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import sinon from 'sinon'
  2. import { vi, describe, it, beforeEach, afterEach } from 'vitest'
  3. import Path from 'node:path'
  4. const MODULE_PATH = Path.join(
  5. import.meta.dirname,
  6. '../../../app/js/ConversionOutputCleaner'
  7. )
  8. describe('ConversionOutputCleaner', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.clock = sinon.useFakeTimers()
  11. ctx.Settings = {
  12. path: { outputDir: '/output' },
  13. }
  14. ctx.fs = {
  15. rm: sinon.stub().resolves(),
  16. }
  17. ctx.logger = {
  18. warn: sinon.stub(),
  19. }
  20. vi.doMock('node:fs/promises', () => ({ default: ctx.fs }))
  21. vi.doMock('@overleaf/settings', () => ({ default: ctx.Settings }))
  22. vi.doMock('@overleaf/logger', () => ({ default: ctx.logger }))
  23. ctx.ConversionOutputCleaner = (await import(MODULE_PATH)).default
  24. })
  25. afterEach(function (ctx) {
  26. ctx.clock.restore()
  27. })
  28. it('does not remove the directory before the TTL elapses', function (ctx) {
  29. ctx.ConversionOutputCleaner.scheduleCleanup('test-conversion-id')
  30. ctx.clock.tick(ctx.ConversionOutputCleaner.TTL_MS - 1)
  31. sinon.assert.notCalled(ctx.fs.rm)
  32. })
  33. it('removes the conversion output directory once the TTL elapses', function (ctx) {
  34. ctx.ConversionOutputCleaner.scheduleCleanup('test-conversion-id')
  35. ctx.clock.tick(ctx.ConversionOutputCleaner.TTL_MS)
  36. sinon.assert.calledWith(ctx.fs.rm, '/output/test-conversion-id', {
  37. recursive: true,
  38. force: true,
  39. })
  40. })
  41. })