43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
|
|
import assert from 'node:assert/strict';
|
||
|
|
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';
|
||
|
|
|
||
|
|
test('isPreviewPath matches transform cache files only', () => {
|
||
|
|
assert.equal(isPreviewPath('id__0123456789abcdef0123456789abcdef01234567.avif'), true);
|
||
|
|
assert.equal(isPreviewPath('id__0123456789abcdef0123456789abcdef01234567.webp'), true);
|
||
|
|
assert.equal(isPreviewPath('id.jpg'), false);
|
||
|
|
assert.equal(isPreviewPath('state.jsonl'), false);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('isStatePath detects the state directory', () => {
|
||
|
|
assert.equal(isStatePath('.image-previews/settings.json'), true);
|
||
|
|
assert.equal(isStatePath('.image-previews'), true);
|
||
|
|
assert.equal(isStatePath('abc.jpg'), false);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('storageUsage splits previews and originals, excluding state', async () => {
|
||
|
|
const dir = await mkdtemp(join(tmpdir(), 'previews-usage-'));
|
||
|
|
|
||
|
|
try {
|
||
|
|
await writeFile(join(dir, 'a__0123456789abcdef0123456789abcdef01234567.avif'), 'x'.repeat(10));
|
||
|
|
await writeFile(join(dir, 'a.jpg'), 'y'.repeat(99));
|
||
|
|
await mkdir(join(dir, '.image-previews'));
|
||
|
|
await writeFile(join(dir, '.image-previews', 'settings.json'), 'z'.repeat(500));
|
||
|
|
|
||
|
|
const usage = await storageUsage({ STORAGE_LOCAL_ROOT: dir, STORAGE_LOCATIONS: 'local' });
|
||
|
|
|
||
|
|
assert.equal(usage.source, 'local');
|
||
|
|
assert.equal(usage.previews.count, 1);
|
||
|
|
assert.equal(usage.previews.bytes, 10);
|
||
|
|
assert.equal(usage.originals.count, 1);
|
||
|
|
assert.equal(usage.originals.bytes, 99);
|
||
|
|
assert.equal(usage.total.count, 2);
|
||
|
|
assert.equal(usage.total.bytes, 109);
|
||
|
|
} finally {
|
||
|
|
await rm(dir, { recursive: true, force: true });
|
||
|
|
}
|
||
|
|
});
|