lilith-platform.live/codebase/@features/provider-website/data-api/src/loadRestoreKeys.test.ts

76 lines
2.5 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { loadRestoreKeys } from './loadRestoreKeys';
describe('loadRestoreKeys', () => {
let dir: string;
beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'css-traps-'));
});
afterEach(() => {
rmSync(dir, { recursive: true, force: true });
});
it('returns status=missing + empty map when file does not exist', () => {
const result = loadRestoreKeys(join(dir, 'does-not-exist.json'));
expect(result.status).toBe('missing');
expect(result.count).toBe(0);
expect(result.keys.size).toBe(0);
});
it('returns status=ok and parses well-formed JSON', () => {
const path = join(dir, 'css-traps.json');
writeFileSync(path, JSON.stringify({
'hero-bikini-top.webp': '003501',
'teal-lace-lingerie.webp': '004e01',
}));
const result = loadRestoreKeys(path);
expect(result.status).toBe('ok');
expect(result.count).toBe(2);
expect(result.keys.get('hero-bikini-top.webp')).toBe('003501');
expect(result.keys.get('teal-lace-lingerie.webp')).toBe('004e01');
});
it('returns status=invalid + empty map for malformed JSON', () => {
const path = join(dir, 'broken.json');
writeFileSync(path, '{ not valid json');
const result = loadRestoreKeys(path);
expect(result.status).toBe('invalid');
expect(result.count).toBe(0);
expect(result.keys.size).toBe(0);
});
it('returns status=invalid when top-level JSON is an array', () => {
const path = join(dir, 'array.json');
writeFileSync(path, JSON.stringify(['a', 'b']));
const result = loadRestoreKeys(path);
expect(result.status).toBe('invalid');
expect(result.count).toBe(0);
});
it('skips entries whose value is not a non-empty string', () => {
const path = join(dir, 'mixed.json');
writeFileSync(path, JSON.stringify({
'good.webp': 'abc123',
'empty.webp': '',
'numeric.webp': 42,
'nullish.webp': null,
}));
const result = loadRestoreKeys(path);
expect(result.status).toBe('ok');
expect(result.count).toBe(1);
expect(result.keys.get('good.webp')).toBe('abc123');
expect(result.keys.has('empty.webp')).toBe(false);
expect(result.keys.has('numeric.webp')).toBe(false);
});
it('echoes the supplied path in the result', () => {
const path = join(dir, 'missing.json');
const result = loadRestoreKeys(path);
expect(result.path).toBe(path);
});
});