import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { readFile, stat } from 'node:fs/promises';
import test from 'node:test';
import { IMAGE_PLACEHOLDER } from '../src/lib/view-helpers';
import { IMAGE_PLACEHOLDER as REACT_IMAGE_PLACEHOLDER } from '../src/lib/media/images';
import { coverImage } from '../src/server/services/model';
test('the shared image fallback preserves the complete Figma brand export', async () => {
const svg = await readFile(new URL('../public/static/brand/placeholder.svg', import.meta.url));
const brand = await readFile(new URL('../public/static/brand/footer.svg', import.meta.url));
const manifest = JSON.parse(await readFile(new URL('../public/static/brand/placeholder.manifest.json', import.meta.url), 'utf8')) as {
width: number; height: number; sha256: string;
composition: { embeddedImage: { sha256: string; unmodified: boolean; width: number; height: number } };
};
const embedded = svg.toString().match(/href="data:image\/svg\+xml;base64,([^"]+)"/);
assert.ok(embedded);
assert.deepEqual(Buffer.from(embedded[1], 'base64'), brand);
assert.match(svg.toString(), /width="480" height="480" viewBox="0 0 480 480"/);
assert.match(svg.toString(), //);
assert.match(svg.toString(), /]*\bopacity="0\.2"/);
assert.equal(manifest.width, 480);
assert.equal(manifest.height, 480);
assert.equal(manifest.sha256, createHash('sha256').update(svg).digest('hex'));
assert.equal(manifest.composition.embeddedImage.sha256, createHash('sha256').update(brand).digest('hex'));
assert.equal(manifest.composition.embeddedImage.unmodified, true);
assert.equal(manifest.composition.embeddedImage.width, 240);
assert.ok(Math.abs(manifest.composition.embeddedImage.height - 240 * 44 / 124) < 0.000001);
assert.equal(IMAGE_PLACEHOLDER, '/static/brand/placeholder.svg');
assert.equal(coverImage(undefined), IMAGE_PLACEHOLDER);
assert.equal(new URL(REACT_IMAGE_PLACEHOLDER, 'https://site.example').pathname, IMAGE_PLACEHOLDER);
await assert.rejects(stat(new URL('../public/static/images/placeholder.png', import.meta.url)), { code: 'ENOENT' });
});