fix: count pre-existing preview files as cached in backfill

Pre-scan storage filenames and check each task's predicted cache file
before generating. Existing previews are now counted as "already cached"
and skipped, instead of being counted as remaining work and regenerated.
Storage-scan and plan-computation failures fall back to generation.
This commit is contained in:
Rawman Grey 2026-10-25 13:52:28 +03:00
parent 5a368c4a8c
commit 18db52a47c
6 changed files with 179 additions and 13 deletions

View file

@ -3,7 +3,7 @@ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test } from 'node:test';
import { isPreviewPath, isStatePath, storageUsage } from './storage';
import { isPreviewPath, isStatePath, listFileNames, storageUsage } from './storage';
test('isPreviewPath matches transform cache files only', () => {
assert.equal(isPreviewPath('id__0123456789abcdef0123456789abcdef01234567.avif'), true);
@ -18,6 +18,26 @@ test('isStatePath detects the state directory', () => {
assert.equal(isStatePath('abc.jpg'), false);
});
test('listFileNames lists files and excludes the state dir', async () => {
const dir = await mkdtemp(join(tmpdir(), 'previews-names-'));
try {
await writeFile(join(dir, 'a__0123456789abcdef0123456789abcdef01234567.avif'), 'x');
await writeFile(join(dir, 'a.jpg'), 'y');
await mkdir(join(dir, '.image-previews'));
await writeFile(join(dir, '.image-previews', 'settings.json'), 'z');
const names = await listFileNames({ STORAGE_LOCAL_ROOT: dir, STORAGE_LOCATIONS: 'local' });
assert.equal(names.size, 2);
assert.ok(names.has('a__0123456789abcdef0123456789abcdef01234567.avif'));
assert.ok(names.has('a.jpg'));
assert.equal(names.has('settings.json'), false);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test('storageUsage splits previews and originals, excluding state', async () => {
const dir = await mkdtemp(join(tmpdir(), 'previews-usage-'));