'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); const sharp = require('sharp'); let mockPaths = []; jest.mock('../server/config', () => ({ loadConfig: () => ({ paths: [...mockPaths] }), getEffectivePaths: (paths) => paths, })); async function writeTinyPng (target) { await sharp({ create: { width: 400, height: 240, channels: 3, background: { r: 120, g: 180, b: 220 }, }, }).png().toFile(target); } describe('thumbnail cache worker', () => { let tempDir; let publicDir; let thumbsDir; let sourceRoot; function loadModule () { jest.resetModules(); return require('../server/thumbnail-cache'); } beforeEach(() => { tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-thumb-cache-')); publicDir = path.join(tempDir, 'public'); thumbsDir = path.join(publicDir, 'thumbs'); sourceRoot = path.join(tempDir, 'root'); fs.mkdirSync(publicDir, { recursive: true }); fs.mkdirSync(sourceRoot, { recursive: true }); process.env.FILEMANAGER_PUBLIC_DIR = publicDir; process.env.FILEMANAGER_THUMBS_DIR = thumbsDir; mockPaths = [sourceRoot]; }); afterEach(() => { delete process.env.FILEMANAGER_PUBLIC_DIR; delete process.env.FILEMANAGER_THUMBS_DIR; mockPaths = []; fs.rmSync(tempDir, { recursive: true, force: true }); }); test('creates cached thumbnail with dimensions smaller than 100px', async () => { const mod = loadModule(); const imagePath = path.join(sourceRoot, 'photos', 'sample.png'); fs.mkdirSync(path.dirname(imagePath), { recursive: true }); await writeTinyPng(imagePath); await mod.syncThumbnailCacheOnce(); const thumbPath = mod.getThumbPathForSourceFile(imagePath); expect(fs.existsSync(thumbPath)).toBe(true); const meta = await sharp(thumbPath).metadata(); expect(meta.width).toBeLessThan(100); expect(meta.height).toBeLessThan(100); const manifestPath = path.join(thumbsDir, '.manifest.json'); const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); expect(manifest.files[imagePath]).toBeTruthy(); }); test('removes cached thumbnail when source file is deleted', async () => { const mod = loadModule(); const imagePath = path.join(sourceRoot, 'sample.png'); await writeTinyPng(imagePath); await mod.syncThumbnailCacheOnce(); const thumbPath = mod.getThumbPathForSourceFile(imagePath); expect(fs.existsSync(thumbPath)).toBe(true); fs.unlinkSync(imagePath); await mod.syncThumbnailCacheOnce(); expect(fs.existsSync(thumbPath)).toBe(false); }); });