Files
kits/packages/sitekit/test/theme.test.js
CatoandClaude Opus 5.5 e81bea6dba feat(sitekit): theme as data (AA-checked), settings, SiteDocument, and a form section (#2)
* feat(sitekit): a theme as data, AA-checked, and the SiteDocument page frame

siteThemeSchema / siteThemeCss turn a site's look into a value a server can
render without a stylesheet per site: one colour per semantic class (plus an
optional dark palette), faces from a closed self-hosted list, tracking,
radius and width, emitted as both the section tokens and the chrome's
--sk-* tokens. Text tokens below WCAG AA on either surface, and a button
label below AA on the accent, are refused with the measured ratio.

SiteDocument is the page frame consumers wrote by hand (camille's catch-all
route, class for class), so an engine-served page and an exported repository
render the same component.

Step 2 of loki ADR-0001 (the sites platform).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(sitekit): siteSettingsSchema — lang and a one-line banner, read the same by engine and export

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(sitekit): a form section — the first block with a server half

Closed fields (name, email, phone, message), a required privacy note and
anchor; refuses a field asked twice and a form nobody could answer. Renders a
plain HTML POST to the host's endpoint (SiteSections / SiteDocument `forms`
prop) with a honeypot and a live-region notice after the redirect back;
without a host it renders words, never a dead form. Settings gain `formTo`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(sitekit): a gallery section — sized, lazy, never stretched or cropped

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(sitekit): booking requests (date, time, people) and a map section on OpenStreetMap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(sitekit): card actions — a shop links each product to where it is sold

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(sitekit): the map's show control reads as a control (+ like the FAQ)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 14:18:09 +01:00

165 lines
5.7 KiB
JavaScript

/**
* A theme is data a server turns into CSS, so the two things it must never do
* are: emit text a reader cannot read (WCAG AA), and emit anything that did
* not come from the closed set (no stray CSS, no third-party font).
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
siteThemeSchema,
siteThemeCss,
siteThemeFaces,
contrastRatio,
SITE_FACES,
siteSettingsSchema,
} from '@bitbaum/sitekit';
const PALETTE = {
surfacePage: '#FAF6F0',
surfaceRaised: '#ffffff',
fgPrimary: '#2c1810',
fgSecondary: '#5c4030',
fgTertiary: '#7a5a44',
fgMuted: '#7a5a44',
borderSubtle: '#ecdfd0',
borderStrong: '#e8d4a8',
accent: '#a8462b',
statusPositive: '#5a8a4c',
statusWarning: '#b98a3c',
statusNegative: '#b5564d',
};
const THEME = { palette: PALETTE, type: { heading: 'cormorant-garamond', body: 'dm-sans' } };
test('contrast ratio matches the WCAG reference points', () => {
assert.equal(contrastRatio('#000000', '#ffffff'), 21);
assert.equal(contrastRatio('#ffffff', '#ffffff'), 1);
assert.equal(contrastRatio('#777777', '#ffffff').toFixed(2), '4.48');
assert.equal(contrastRatio('#ffffff', '#777777'), contrastRatio('#777777', '#ffffff'));
});
test('a readable theme parses, with defaults filled and colours lower-cased', () => {
const t = siteThemeSchema.parse(THEME);
assert.equal(t.palette.surfacePage, '#faf6f0');
assert.equal(t.type.mono, 'system-mono');
assert.equal(t.radius, 'soft');
assert.equal(t.shellRem, 72);
});
test('text below AA is refused at the token, with the ratio in the message', () => {
// camille-boulangerie's fg-muted today: 3.15:1 on its cream page.
const r = siteThemeSchema.safeParse({ ...THEME, palette: { ...PALETTE, fgMuted: '#a3866c' } });
assert.equal(r.success, false);
const issue = r.error.issues.find((i) => i.path.join('.') === 'palette.fgMuted');
assert.ok(issue, JSON.stringify(r.error.issues));
assert.match(issue.message, /3\.\d\d:1/);
});
test('text that passes on the page but fails on raised cards is refused', () => {
const r = siteThemeSchema.safeParse({
...THEME,
palette: { ...PALETTE, surfaceRaised: '#9a9a9a' },
});
assert.equal(r.success, false);
assert.ok(r.error.issues.some((i) => /on surfaceRaised/.test(i.message)));
});
test('a filled button whose label is unreadable on the accent is refused', () => {
// camille's terracotta today: cream on #c45c3e is 3.94:1.
const r = siteThemeSchema.safeParse({ ...THEME, palette: { ...PALETTE, accent: '#c45c3e' } });
assert.equal(r.success, false);
assert.ok(r.error.issues.some((i) => i.path.join('.') === 'palette.accent'));
});
test('a dark palette is checked by the same rules', () => {
const dark = { ...PALETTE, surfacePage: '#000000', surfaceRaised: '#111111' };
assert.equal(siteThemeSchema.safeParse({ ...THEME, darkPalette: dark }).success, false);
});
test('only #rrggbb colours, known faces and small tracking values are accepted', () => {
for (const bad of ['red', '#fff', 'rgb(0,0,0)', '#000000;}body{display:none', 'var(--x)']) {
const r = siteThemeSchema.safeParse({ ...THEME, palette: { ...PALETTE, accent: bad } });
assert.equal(r.success, false, bad);
}
assert.equal(
siteThemeSchema.safeParse({ ...THEME, type: { heading: 'Comic Sans', body: 'dm-sans' } })
.success,
false,
);
assert.equal(
siteThemeSchema.safeParse({ ...THEME, type: { ...THEME.type, trackingDisplay: '1px;x' } })
.success,
false,
);
});
test('the CSS sets both the section tokens and the chrome tokens', () => {
const css = siteThemeCss(THEME);
assert.match(css, /^:root\{/);
for (const v of [
'--color-surface-page:#faf6f0',
'--color-fg-primary:#2c1810',
'--color-accent:#a8462b',
`--font-heading:${SITE_FACES['cormorant-garamond']}`,
`--font-sans:${SITE_FACES['dm-sans']}`,
'--spacing-shell:72rem',
'--sk-bg:#faf6f0',
'--sk-fg:#2c1810',
'--sk-line:#ecdfd0',
'--sk-radius:0.5rem',
'color-scheme:light',
]) {
assert.ok(css.includes(v), v);
}
assert.ok(!css.includes('@media'), 'no dark block without a dark palette');
});
test('a dark palette becomes a prefers-color-scheme override', () => {
const dark = {
surfacePage: '#000000',
surfaceRaised: '#111111',
fgPrimary: '#ffffff',
fgSecondary: '#c6c6c6',
fgTertiary: '#a0a0a0',
fgMuted: '#9a9a9a',
borderSubtle: '#222222',
borderStrong: '#3d3d3d',
accent: '#ffffff',
statusPositive: '#5a8a4c',
statusWarning: '#b98a3c',
statusNegative: '#b5564d',
};
const css = siteThemeCss({ ...THEME, darkPalette: dark });
assert.match(css, /color-scheme:light dark/);
assert.match(css, /@media \(prefers-color-scheme: dark\)\{:root\{--color-surface-page:#000000/);
});
test('siteThemeCss refuses a theme that never passed the schema', () => {
assert.throws(() => siteThemeCss({ ...THEME, palette: { ...PALETTE, fgMuted: '#a3866c' } }));
});
test('faces to serve: webfonts only, no duplicates', () => {
assert.deepEqual(siteThemeFaces(THEME), ['cormorant-garamond', 'dm-sans']);
assert.deepEqual(
siteThemeFaces({
...THEME,
type: { heading: 'helvetica', body: 'helvetica', mono: 'ibm-plex-mono' },
}),
['ibm-plex-mono'],
);
});
test('settings: a language tag (default de-CH) and an optional one-line banner', () => {
assert.deepEqual(siteSettingsSchema.parse({}), { lang: 'de-CH' });
assert.equal(siteSettingsSchema.parse({ lang: 'fr' }).lang, 'fr');
assert.equal(siteSettingsSchema.parse({ banner: ' Demo ' }).banner, 'Demo');
for (const bad of [
{ lang: 'German' },
{ lang: 'de_CH' },
{ banner: '' },
{ banner: 'x'.repeat(281) },
]) {
assert.equal(siteSettingsSchema.safeParse(bad).success, false, JSON.stringify(bad));
}
});