Files
klarbild/tests/provenance.test.ts
T
tillandClaude Opus 5 6ae6e0348c feat: CSP and security headers, memory limit, AI provenance, one crop core
Five things off the open list in the wiki.

CSP with real hashes for every inline script (Astro's experimental.csp), so no
'unsafe-inline' for scripts. style-src is deliberately reopened in middleware:
React sets styles as attributes and attributes cannot be hashed -- measured, the
hash-only policy blocks them and the print preview loses its layout. Plus
nosniff, referrer-policy, X-Frame-Options and a permissions-policy. Verified
across all pages: zero violations.

mem_limit 3g in compose. sharp can spike; without a limit an outlier takes the
whole container down instead of just the process, and the healthcheck can't
help.

AI results now carry IPTC DigitalSourceType (trainedAlgorithmicMedia, or
compositeWithTrainedAlgorithmicMedia for edited photos) plus the model, embedded
as XMP -- written by hand into PNG iTXt and JPEG APP1 rather than pulling in a
sharp upgrade with a native postinstall. exiftool reads it back from both
formats; pixels, dimensions and dpi are provably untouched.

Crop maths lived twice, in the browser preview and the server renderer, with
nothing holding them together. Now one dependency-free module, and 60000
randomised cases assert both entry points agree. That test immediately found a
real bug: the zoom limits (0.02 / 8x) also clamped the *initial* crop, so an
extreme format silently broke the aspect ratio -- "Rand lassen" would have
cropped after all. Limits are now widened to whatever the format needs.

Also: the colour flag sat exactly on the selection circle in the library and
covered it. Moved to the bottom left.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BNhLunz586g6DeYMa7Qsd1
2026-09-01 02:44:45 +00:00

81 lines
3.5 KiB
TypeScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import sharp from 'sharp';
import {
buildXmp, embedProvenance, readXmp,
TRAINED_ALGORITHMIC_MEDIA, COMPOSITE_WITH_TRAINED_ALGORITHMIC_MEDIA,
} from '../src/lib/provenance.ts';
const bild = (w = 240, h = 160) =>
sharp({ create: { width: w, height: h, channels: 3, background: '#c8463c' } });
test('Das XMP-Paket trägt den IPTC-Code und das Modell', () => {
const xmp = buildXmp({ sourceType: TRAINED_ALGORITHMIC_MEDIA, model: 'google/gemini-3-pro-image' });
assert.match(xmp, /Iptc4xmpExt:DigitalSourceType="http:\/\/cv\.iptc\.org\/newscodes\/digitalsourcetype\/trainedAlgorithmicMedia"/);
assert.match(xmp, /xmp:CreatorTool="Klarbild \(google\/gemini-3-pro-image\)"/);
assert.match(xmp, /<x:xmpmeta/);
});
test('Sonderzeichen im Prompt zerlegen das XML nicht', () => {
const xmp = buildXmp({
sourceType: TRAINED_ALGORITHMIC_MEDIA,
description: 'Lea & Phoenix <b>"groß"</b> — 3 < 5',
});
assert.ok(!xmp.includes('<b>'), 'unmaskiertes Tag im XMP');
assert.match(xmp, /Lea &amp; Phoenix/);
// Genau so viele öffnende wie schließende Klammern der bekannten Elemente.
assert.equal((xmp.match(/<rdf:Description/g) || []).length, 1);
assert.equal((xmp.match(/<\/rdf:Description>/g) || []).length, 1);
});
for (const [name, machen] of [
['PNG', async () => bild().png().toBuffer()],
['JPEG', async () => bild().jpeg().toBuffer()],
['JPEG mit dpi-Metadaten (wie im Auftrag)', async () => bild().withMetadata({ density: 300 }).jpeg({ quality: 92 }).toBuffer()],
] as const) {
test(`${name}: Kennzeichnung wird geschrieben und das Bild bleibt heil`, async () => {
const vorher = await machen();
const nachher = embedProvenance(vorher, {
sourceType: TRAINED_ALGORITHMIC_MEDIA, model: 'test/modell', description: 'Prüflauf',
});
assert.ok(nachher.length > vorher.length, 'nichts eingebettet');
// 1) Kennzeichnung ist wieder auslesbar …
const xmp = readXmp(nachher);
assert.ok(xmp, 'XMP nicht gefunden');
assert.match(xmp!, /trainedAlgorithmicMedia/);
// 2) … und sharp liest die Datei weiterhin, mit unveränderten Maßen.
const m = await sharp(nachher).metadata();
const m0 = await sharp(vorher).metadata();
assert.equal(m.width, m0.width);
assert.equal(m.height, m0.height);
assert.equal(m.format, m0.format);
if (m0.density) assert.equal(m.density, m0.density, 'dpi verloren');
// 3) Die Bildpunkte selbst sind unangetastet.
const a = await sharp(vorher).raw().toBuffer();
const b = await sharp(nachher).raw().toBuffer();
assert.ok(a.equals(b), 'Bildinhalt hat sich verändert');
});
}
test('Bereinigte Fotos bekommen den Code für verändertes Material', () => {
const xmp = buildXmp({ sourceType: COMPOSITE_WITH_TRAINED_ALGORITHMIC_MEDIA });
assert.match(xmp, /compositeWithTrainedAlgorithmicMedia/);
});
test('Unbekannte Formate und Schrott kommen unverändert zurück', () => {
for (const b of [Buffer.from('kein bild'), Buffer.alloc(0), Buffer.from([0x52, 0x49, 0x46, 0x46])]) {
assert.ok(embedProvenance(b, { sourceType: TRAINED_ALGORITHMIC_MEDIA }).equals(b));
}
});
test('Zweimal kennzeichnen bricht die Datei nicht', async () => {
const png = await bild().png().toBuffer();
const einmal = embedProvenance(png, { sourceType: TRAINED_ALGORITHMIC_MEDIA });
const zweimal = embedProvenance(einmal, { sourceType: TRAINED_ALGORITHMIC_MEDIA });
const m = await sharp(zweimal).metadata();
assert.equal(m.width, 240);
});