React 19 + TypeScript + Vite + TanStack Query/Router in web/, built on the dev machine and shipped as static files (SPEC section 6). Typechecks and builds clean; the System page reads live /health through the Vite proxy, which proves browser -> proxy -> FastAPI -> PostgreSQL end to end. Themes: light, dark and blue (dark navy), as design tokens on data-theme. An inline script applies the stored theme before first paint. No colour literal lives outside tokens.css. check-contrast.mjs asserts WCAG AA for every text/background pair in every theme, with no dependencies. It failed on its first run and found three --border-strong values below the 3:1 that WCAG 1.4.11 wants for interactive boundaries; all three are fixed and 36 pairs now pass. Colour contrast is the classic silently-regressing property, so it gets a test rather than a promise. i18n is hand-written and typed rather than react-i18next: en.ts defines the key set, de.ts is typed against it, so a missing German string fails the build instead of quietly showing English to a German user. Authentication is built but NOT enforced, deliberately. /auth/session, /auth/login and /auth/logout do not exist yet, so router.tsx does not wrap routes in RequireAuth and a banner says so plainly. There is no development bypass: a bypass that works is the kind of code that survives into production, and an auth bypass that works is the worst possible bug. Gallery and Devices render honest empty states rather than mock rows. Fake data hides the layout problems real data exposes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
127 lines
3.7 KiB
JavaScript
127 lines
3.7 KiB
JavaScript
/* Assert WCAG contrast for every theme in tokens.css.
|
|
*
|
|
* Colour contrast is the classic silently-regressing property: you nudge one
|
|
* hex value, it still "looks fine" on your monitor, and the muted text is now
|
|
* unreadable for someone else. So it gets a test rather than a promise.
|
|
*
|
|
* No dependencies -- run with `npm run check:contrast`.
|
|
*/
|
|
|
|
import { readFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, join } from "node:path";
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
const css = readFileSync(join(here, "..", "src", "styles", "tokens.css"), "utf8");
|
|
|
|
/** Extract custom properties per theme from the stylesheet. */
|
|
function parseThemes(source) {
|
|
const themes = {};
|
|
const blockRe = /([^{}]+)\{([^}]*)\}/g;
|
|
let match;
|
|
while ((match = blockRe.exec(source)) !== null) {
|
|
const selectors = match[1]
|
|
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
.split(",")
|
|
.map((s) => s.trim());
|
|
const body = match[2];
|
|
|
|
const props = {};
|
|
const propRe = /(--[\w-]+)\s*:\s*([^;]+);/g;
|
|
let prop;
|
|
while ((prop = propRe.exec(body)) !== null) {
|
|
props[prop[1]] = prop[2].trim();
|
|
}
|
|
|
|
for (const selector of selectors) {
|
|
const named = selector.match(/\[data-theme="([\w-]+)"\]/);
|
|
if (named) {
|
|
themes[named[1]] = { ...(themes[named[1]] ?? {}), ...props };
|
|
}
|
|
}
|
|
}
|
|
return themes;
|
|
}
|
|
|
|
function toLinear(channel) {
|
|
const c = channel / 255;
|
|
return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
}
|
|
|
|
function luminance(hex) {
|
|
const clean = hex.replace("#", "").trim();
|
|
if (!/^[0-9a-fA-F]{6}$/.test(clean)) return null;
|
|
const r = parseInt(clean.slice(0, 2), 16);
|
|
const g = parseInt(clean.slice(2, 4), 16);
|
|
const b = parseInt(clean.slice(4, 6), 16);
|
|
return 0.2126 * toLinear(r) + 0.7152 * toLinear(g) + 0.0722 * toLinear(b);
|
|
}
|
|
|
|
function contrast(fg, bg) {
|
|
const a = luminance(fg);
|
|
const b = luminance(bg);
|
|
if (a === null || b === null) return null;
|
|
const [hi, lo] = a > b ? [a, b] : [b, a];
|
|
return (hi + 0.05) / (lo + 0.05);
|
|
}
|
|
|
|
/* Each pair: [foreground token, background token, minimum ratio].
|
|
* 4.5 is WCAG AA for body text; 3.0 is AA for large text and for the
|
|
* boundaries of interactive components. */
|
|
const PAIRS = [
|
|
["--text", "--bg", 4.5],
|
|
["--text", "--surface", 4.5],
|
|
["--text", "--surface-raised", 4.5],
|
|
["--text-muted", "--bg", 4.5],
|
|
["--text-muted", "--surface", 4.5],
|
|
["--accent", "--bg", 4.5],
|
|
["--accent", "--surface", 4.5],
|
|
["--text-inverse", "--accent", 4.5],
|
|
["--success", "--surface", 4.5],
|
|
["--warning", "--surface", 4.5],
|
|
["--danger", "--surface", 4.5],
|
|
["--border-strong", "--surface", 3.0],
|
|
];
|
|
|
|
const themes = parseThemes(css);
|
|
const names = Object.keys(themes);
|
|
|
|
if (names.length === 0) {
|
|
console.error("no themes found in tokens.css - has the file moved?");
|
|
process.exit(1);
|
|
}
|
|
|
|
let failures = 0;
|
|
let checked = 0;
|
|
|
|
for (const name of names) {
|
|
const tokens = themes[name];
|
|
console.log(`\n ${name}`);
|
|
for (const [fgToken, bgToken, min] of PAIRS) {
|
|
const fg = tokens[fgToken];
|
|
const bg = tokens[bgToken];
|
|
if (!fg || !bg) {
|
|
console.log(` ? ${fgToken} on ${bgToken} - token missing`);
|
|
failures++;
|
|
continue;
|
|
}
|
|
const ratio = contrast(fg, bg);
|
|
if (ratio === null) {
|
|
console.log(` ? ${fgToken} on ${bgToken} - not a plain hex colour`);
|
|
continue;
|
|
}
|
|
checked++;
|
|
const ok = ratio >= min;
|
|
if (!ok) failures++;
|
|
const mark = ok ? "ok " : "FAIL";
|
|
console.log(
|
|
` ${mark} ${fgToken} on ${bgToken} ${ratio.toFixed(2)}:1 (need ${min})`
|
|
);
|
|
}
|
|
}
|
|
|
|
console.log(
|
|
`\n${checked} pairs checked across ${names.length} themes, ${failures} failure(s)\n`
|
|
);
|
|
process.exit(failures > 0 ? 1 : 0);
|