| 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 | let mockPaths = []; |
| 9 | |
| 10 | jest.mock('../server/config', () => ({ |
| 11 | loadConfig: () => ({ paths: [...mockPaths] }), |
| 12 | getEffectivePaths: (paths) => paths, |
| 13 | })); |
| 14 | |
| 15 | async 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 | |
| 27 | describe('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 | }); |