BlueGit

timfa filemanager

Files

filemanager / tests / thumbnail-cache.test.js

thumbnail-cache.test.js

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