| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const os = require('os'); |
| 5 | const path = require('path'); |
| 6 | const sharp = require('sharp'); |
| 7 | |
| 8 | sharp.cache(false); |
| 9 | |
| 10 | let mockPaths = []; |
| 11 | |
| 12 | jest.mock('../server/config', () => ({ |
| 13 | loadConfig: () => ({ paths: [...mockPaths] }), |
| 14 | getEffectivePaths: (paths) => paths, |
| 15 | })); |
| 16 | |
| 17 | async function writeTinyPng (target) |
| 18 | { |
| 19 | await sharp({ |
| 20 | create: { |
| 21 | width: 400, |
| 22 | height: 240, |
| 23 | channels: 3, |
| 24 | background: { r: 120, g: 180, b: 220 }, |
| 25 | }, |
| 26 | }).png().toFile(target); |
| 27 | } |
| 28 | |
| 29 | function writeTinySvg (target) |
| 30 | { |
| 31 | const svg = `<?xml version="1.0" encoding="UTF-8"?> |
| 32 | <svg xmlns="http://www.w3.org/2000/svg" width="200" height="120" viewBox="0 0 200 120"> |
| 33 | <rect x="0" y="0" width="200" height="120" fill="#2a7fff"/> |
| 34 | <circle cx="60" cy="60" r="32" fill="#ffd166"/> |
| 35 | <rect x="105" y="30" width="70" height="60" rx="10" fill="#0f172a"/> |
| 36 | </svg>`; |
| 37 | fs.writeFileSync(target, svg, 'utf8'); |
| 38 | } |
| 39 | |
| 40 | describe('thumbnail cache worker', () => |
| 41 | { |
| 42 | let tempDir; |
| 43 | let publicDir; |
| 44 | let thumbsDir; |
| 45 | let sourceRoot; |
| 46 | |
| 47 | function loadModule () |
| 48 | { |
| 49 | jest.resetModules(); |
| 50 | return require('../server/thumbnail-cache'); |
| 51 | } |
| 52 | |
| 53 | beforeEach(() => |
| 54 | { |
| 55 | tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-thumb-cache-')); |
| 56 | publicDir = path.join(tempDir, 'public'); |
| 57 | thumbsDir = path.join(publicDir, 'thumbs'); |
| 58 | sourceRoot = path.join(tempDir, 'root'); |
| 59 | |
| 60 | fs.mkdirSync(publicDir, { recursive: true }); |
| 61 | fs.mkdirSync(sourceRoot, { recursive: true }); |
| 62 | |
| 63 | process.env.FILEMANAGER_PUBLIC_DIR = publicDir; |
| 64 | process.env.FILEMANAGER_THUMBS_DIR = thumbsDir; |
| 65 | mockPaths = [sourceRoot]; |
| 66 | }); |
| 67 | |
| 68 | afterEach(() => |
| 69 | { |
| 70 | delete process.env.FILEMANAGER_PUBLIC_DIR; |
| 71 | delete process.env.FILEMANAGER_THUMBS_DIR; |
| 72 | mockPaths = []; |
| 73 | try |
| 74 | { |
| 75 | fs.rmSync(tempDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 30 }); |
| 76 | } catch (err) |
| 77 | { |
| 78 | if (!err || !/EBUSY|EPERM/i.test(String(err.code || err.message || ''))) |
| 79 | { |
| 80 | throw err; |
| 81 | } |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | test('creates cached thumbnail with dimensions smaller than 100px', async () => |
| 86 | { |
| 87 | const mod = loadModule(); |
| 88 | const imagePath = path.join(sourceRoot, 'photos', 'sample.png'); |
| 89 | fs.mkdirSync(path.dirname(imagePath), { recursive: true }); |
| 90 | await writeTinyPng(imagePath); |
| 91 | |
| 92 | await mod.syncThumbnailCacheOnce(); |
| 93 | |
| 94 | const thumbPath = mod.getThumbPathForSourceFile(imagePath); |
| 95 | expect(fs.existsSync(thumbPath)).toBe(true); |
| 96 | |
| 97 | const meta = await sharp(thumbPath).metadata(); |
| 98 | expect(meta.width).toBeLessThan(100); |
| 99 | expect(meta.height).toBeLessThan(100); |
| 100 | |
| 101 | const manifestPath = path.join(thumbsDir, '.manifest.json'); |
| 102 | const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); |
| 103 | expect(manifest.files[imagePath]).toBeTruthy(); |
| 104 | }); |
| 105 | |
| 106 | test('removes cached thumbnail when source file is deleted', async () => |
| 107 | { |
| 108 | const mod = loadModule(); |
| 109 | const imagePath = path.join(sourceRoot, 'sample.png'); |
| 110 | await writeTinyPng(imagePath); |
| 111 | |
| 112 | await mod.syncThumbnailCacheOnce(); |
| 113 | const thumbPath = mod.getThumbPathForSourceFile(imagePath); |
| 114 | expect(fs.existsSync(thumbPath)).toBe(true); |
| 115 | |
| 116 | fs.unlinkSync(imagePath); |
| 117 | await mod.syncThumbnailCacheOnce(); |
| 118 | |
| 119 | expect(fs.existsSync(thumbPath)).toBe(false); |
| 120 | }); |
| 121 | |
| 122 | test('preserves folder-icons.json across sync cycles', async () => |
| 123 | { |
| 124 | const mod = loadModule(); |
| 125 | |
| 126 | // Run one sync to create the thumbs dir. |
| 127 | await mod.syncThumbnailCacheOnce(); |
| 128 | |
| 129 | // Write a folder icon assignment directly. |
| 130 | await mod.setFolderIconAssignment(sourceRoot, path.join(sourceRoot, 'cover.jpg')); |
| 131 | |
| 132 | const iconsPath = path.join(thumbsDir, '.folder-icons.json'); |
| 133 | expect(fs.existsSync(iconsPath)).toBe(true); |
| 134 | |
| 135 | // Run sync again — the file must survive pruning. |
| 136 | await mod.syncThumbnailCacheOnce(); |
| 137 | |
| 138 | expect(fs.existsSync(iconsPath)).toBe(true); |
| 139 | const stored = JSON.parse(fs.readFileSync(iconsPath, 'utf8')); |
| 140 | expect(stored[path.resolve(sourceRoot)]).toBe(path.join(sourceRoot, 'cover.jpg')); |
| 141 | }); |
| 142 | |
| 143 | test('creates cached thumbnail for svg files', async () => |
| 144 | { |
| 145 | const mod = loadModule(); |
| 146 | const imagePath = path.join(sourceRoot, 'assets', 'logo.svg'); |
| 147 | fs.mkdirSync(path.dirname(imagePath), { recursive: true }); |
| 148 | writeTinySvg(imagePath); |
| 149 | |
| 150 | await mod.syncThumbnailCacheOnce(); |
| 151 | |
| 152 | const thumbPath = mod.getThumbPathForSourceFile(imagePath); |
| 153 | expect(fs.existsSync(thumbPath)).toBe(true); |
| 154 | |
| 155 | const info = await mod.getCachedThumbnailInfo(imagePath); |
| 156 | expect(info).toBeTruthy(); |
| 157 | expect(info.thumbPath).toBe(thumbPath); |
| 158 | }); |
| 159 | |
| 160 | test('auto-detects first cached image inside /public-style root', async () => |
| 161 | { |
| 162 | const mod = loadModule(); |
| 163 | const publicRoot = path.join(tempDir, 'public-root'); |
| 164 | fs.mkdirSync(publicRoot, { recursive: true }); |
| 165 | mockPaths = [publicRoot]; |
| 166 | |
| 167 | const nested = path.join(publicRoot, 'icons', 'a.svg'); |
| 168 | fs.mkdirSync(path.dirname(nested), { recursive: true }); |
| 169 | writeTinySvg(nested); |
| 170 | |
| 171 | await mod.syncThumbnailCacheOnce(); |
| 172 | |
| 173 | const detected = await mod.getFolderIconSourceFile(publicRoot); |
| 174 | expect(detected).toBe(nested); |
| 175 | }); |
| 176 | |
| 177 | test('preserves assigned folder icon and thumbnail when root is temporarily unavailable', async () => |
| 178 | { |
| 179 | const mod = loadModule(); |
| 180 | const imagePath = path.join(sourceRoot, 'Pepijn', 'cover.jpg'); |
| 181 | fs.mkdirSync(path.dirname(imagePath), { recursive: true }); |
| 182 | await writeTinyPng(imagePath); |
| 183 | |
| 184 | await mod.syncThumbnailCacheOnce(); |
| 185 | await mod.setFolderIconAssignment(path.dirname(imagePath), imagePath); |
| 186 | |
| 187 | const thumbPath = mod.getThumbPathForSourceFile(imagePath); |
| 188 | expect(fs.existsSync(thumbPath)).toBe(true); |
| 189 | expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath); |
| 190 | |
| 191 | // Simulate mount disappearing while still configured. |
| 192 | const offlinePath = sourceRoot + '-offline'; |
| 193 | fs.renameSync(sourceRoot, offlinePath); |
| 194 | |
| 195 | await mod.syncThumbnailCacheOnce(); |
| 196 | |
| 197 | // Cache entry and assignment must survive the temporary outage. |
| 198 | expect(fs.existsSync(thumbPath)).toBe(true); |
| 199 | expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath); |
| 200 | |
| 201 | fs.renameSync(offlinePath, sourceRoot); |
| 202 | }); |
| 203 | }); |