BlueGit

timfa filemanager

Files

filemanager / tests / thumbnail-cache.test.js

thumbnail-cache.test.js

thumbnail-cache.test.js 3.3 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
27describe('thumbnail cache worker', () =>
28{
29 let tempDir;
30 let publicDir;
31 let thumbsDir;
32 let sourceRoot;
33
34 function loadModule ()
35 {
36 jest.resetModules();
37 return require('../server/thumbnail-cache');
38 }
39
40 beforeEach(() =>
41 {
42 tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-thumb-cache-'));
43 publicDir = path.join(tempDir, 'public');
44 thumbsDir = path.join(publicDir, 'thumbs');
45 sourceRoot = path.join(tempDir, 'root');
46
47 fs.mkdirSync(publicDir, { recursive: true });
48 fs.mkdirSync(sourceRoot, { recursive: true });
49
50 process.env.FILEMANAGER_PUBLIC_DIR = publicDir;
51 process.env.FILEMANAGER_THUMBS_DIR = thumbsDir;
52 mockPaths = [sourceRoot];
53 });
54
55 afterEach(() =>
56 {
57 delete process.env.FILEMANAGER_PUBLIC_DIR;
58 delete process.env.FILEMANAGER_THUMBS_DIR;
59 mockPaths = [];
60 fs.rmSync(tempDir, { recursive: true, force: true });
61 });
62
63 test('creates cached thumbnail with dimensions smaller than 100px', async () =>
64 {
65 const mod = loadModule();
66 const imagePath = path.join(sourceRoot, 'photos', 'sample.png');
67 fs.mkdirSync(path.dirname(imagePath), { recursive: true });
68 await writeTinyPng(imagePath);
69
70 await mod.syncThumbnailCacheOnce();
71
72 const thumbPath = mod.getThumbPathForSourceFile(imagePath);
73 expect(fs.existsSync(thumbPath)).toBe(true);
74
75 const meta = await sharp(thumbPath).metadata();
76 expect(meta.width).toBeLessThan(100);
77 expect(meta.height).toBeLessThan(100);
78
79 const manifestPath = path.join(thumbsDir, '.manifest.json');
80 const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
81 expect(manifest.files[imagePath]).toBeTruthy();
82 });
83
84 test('removes cached thumbnail when source file is deleted', async () =>
85 {
86 const mod = loadModule();
87 const imagePath = path.join(sourceRoot, 'sample.png');
88 await writeTinyPng(imagePath);
89
90 await mod.syncThumbnailCacheOnce();
91 const thumbPath = mod.getThumbPathForSourceFile(imagePath);
92 expect(fs.existsSync(thumbPath)).toBe(true);
93
94 fs.unlinkSync(imagePath);
95 await mod.syncThumbnailCacheOnce();
96
97 expect(fs.existsSync(thumbPath)).toBe(false);
98 });
99
100 test('preserves folder-icons.json across sync cycles', async () =>
101 {
102 const mod = loadModule();
103
104 // Run one sync to create the thumbs dir.
105 await mod.syncThumbnailCacheOnce();
106
107 // Write a folder icon assignment directly.
108 await mod.setFolderIconAssignment(sourceRoot, path.join(sourceRoot, 'cover.jpg'));
109
110 const iconsPath = path.join(thumbsDir, '.folder-icons.json');
111 expect(fs.existsSync(iconsPath)).toBe(true);
112
113 // Run sync again — the file must survive pruning.
114 await mod.syncThumbnailCacheOnce();
115
116 expect(fs.existsSync(iconsPath)).toBe(true);
117 const stored = JSON.parse(fs.readFileSync(iconsPath, 'utf8'));
118 expect(stored[path.resolve(sourceRoot)]).toBe(path.join(sourceRoot, 'cover.jpg'));
119 });
120});