'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); const sharp = require('sharp'); sharp.cache(false); 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); } function writeTinySvg (target) { const svg = ` `; fs.writeFileSync(target, svg, 'utf8'); } 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 = []; try { fs.rmSync(tempDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 30 }); } catch (err) { if (!err || !/EBUSY|EPERM/i.test(String(err.code || err.message || ''))) { throw err; } } }); 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); }); test('preserves folder-icons.json across sync cycles', async () => { const mod = loadModule(); // Run one sync to create the thumbs dir. await mod.syncThumbnailCacheOnce(); // Write a folder icon assignment directly. await mod.setFolderIconAssignment(sourceRoot, path.join(sourceRoot, 'cover.jpg')); const iconsPath = path.join(thumbsDir, '.folder-icons.json'); expect(fs.existsSync(iconsPath)).toBe(true); // Run sync again — the file must survive pruning. await mod.syncThumbnailCacheOnce(); expect(fs.existsSync(iconsPath)).toBe(true); const stored = JSON.parse(fs.readFileSync(iconsPath, 'utf8')); expect(stored[path.resolve(sourceRoot)]).toBe(path.join(sourceRoot, 'cover.jpg')); }); test('creates cached thumbnail for svg files', async () => { const mod = loadModule(); const imagePath = path.join(sourceRoot, 'assets', 'logo.svg'); fs.mkdirSync(path.dirname(imagePath), { recursive: true }); writeTinySvg(imagePath); await mod.syncThumbnailCacheOnce(); const thumbPath = mod.getThumbPathForSourceFile(imagePath); expect(fs.existsSync(thumbPath)).toBe(true); const info = await mod.getCachedThumbnailInfo(imagePath); expect(info).toBeTruthy(); expect(info.thumbPath).toBe(thumbPath); }); test('auto-detects first cached image inside /public-style root', async () => { const mod = loadModule(); const publicRoot = path.join(tempDir, 'public-root'); fs.mkdirSync(publicRoot, { recursive: true }); mockPaths = [publicRoot]; const nested = path.join(publicRoot, 'icons', 'a.svg'); fs.mkdirSync(path.dirname(nested), { recursive: true }); writeTinySvg(nested); await mod.syncThumbnailCacheOnce(); const detected = await mod.getFolderIconSourceFile(publicRoot); expect(detected).toBe(nested); }); test('preserves assigned folder icon and thumbnail when root is temporarily unavailable', async () => { const mod = loadModule(); const imagePath = path.join(sourceRoot, 'Pepijn', 'cover.jpg'); fs.mkdirSync(path.dirname(imagePath), { recursive: true }); await writeTinyPng(imagePath); await mod.syncThumbnailCacheOnce(); await mod.setFolderIconAssignment(path.dirname(imagePath), imagePath); const thumbPath = mod.getThumbPathForSourceFile(imagePath); expect(fs.existsSync(thumbPath)).toBe(true); expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath); // Simulate mount disappearing while still configured. const offlinePath = sourceRoot + '-offline'; fs.renameSync(sourceRoot, offlinePath); await mod.syncThumbnailCacheOnce(); // Cache entry and assignment must survive the temporary outage. expect(fs.existsSync(thumbPath)).toBe(true); expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath); fs.renameSync(offlinePath, sourceRoot); }); });