Files
xenia-pos-local/packages/ui/tests/contrast.test.mjs
T
bonaminandClaude Opus 5.5 3580f2815c feat(ui): @xenia/ui design tokens, Tailwind preset and base components
Platform-v2 step 2.3 (APPS_PLATFORM D-15 §9):
- tokens.css: --x- prefixed colours (light/dark/system + 5 venue accent themes),
  spacing, radius, type scale, shadows, motion, z-index; variables only
- components.css: .x-* classes only, no element selectors (new rule CS-16)
- Button, IconButton, Dialog, Sheet, Toast, PinPad, InfoTip, EmptyState,
  Spinner, Field/Input/Textarea/Select, Switch (accessible: focus trap and
  restore, ARIA wiring, 44px targets)
- tailwind-preset.js (adds names only); manager uses it + styles.css, the
  waiter imports tokens.css; existing screens unchanged
- dev-only /ui-gallery in the manager; e2e suite tests/e2e/ui.mjs; unit tests
  for PIN rules and WCAG AA contrast of every token pair, both in run-all

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:33:58 +02:00

68 lines
3.0 KiB
JavaScript

// WCAG contrast of the token pairs the components actually use, in light and dark,
// for every venue theme. Reads tokens.css itself, so a colour edit that breaks
// readability fails here.
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { test } from 'node:test'
const css = readFileSync(new URL('../src/tokens.css', import.meta.url), 'utf8')
/** Channel declarations of the first rule whose selector list is exactly `selector`. */
function rule(selector) {
const re = /([^{}]+)\{([^{}]*)\}/g
for (const m of css.matchAll(re)) {
const sel = m[1].replace(/\/\*[\s\S]*?\*\//g, '').trim().replace(/\s+/g, ' ')
if (sel !== selector) continue
return Object.fromEntries([...m[2].matchAll(/--x-([\w-]+)-rgb:\s*([\d\s]+);/g)].map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)]))
}
throw new Error(`no rule ${selector}`)
}
const lum = ([r, g, b]) => {
const f = c => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4 }
return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)
}
const ratio = (a, b) => { const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p); return (x + 0.05) / (y + 0.05) }
const TEXT = 4.5 // normal-size text (AA)
const UI = 3 // component boundaries and states (AA, 1.4.11)
const pairs = [
['ink', 'surface', TEXT], ['ink-2', 'surface', TEXT], ['ink-3', 'surface', TEXT], ['ink', 'canvas', TEXT],
['ink-2', 'surface-2', TEXT],
['canvas', 'ink', TEXT], // InfoTip popover
['accent-fg', 'accent', TEXT], ['accent-fg', 'accent-strong', TEXT],
['accent-ink', 'accent-soft', TEXT], ['accent-ink', 'surface', TEXT],
['accent', 'surface', UI], // switch on, focus, filled PIN dots
['ink-3', 'surface', UI], // switch off track
...['success', 'warning', 'danger', 'info'].flatMap(t => [
[`${t}-fg`, t, TEXT], [`${t}-ink`, `${t}-soft`, TEXT], [`${t}-ink`, 'surface', TEXT], [t, 'surface', UI],
]),
]
const venues = ['teal', 'amber', 'blue', 'violet', 'rose']
const light = rule(':root')
const dark = rule('[data-theme="dark"]')
for (const venue of venues) {
for (const [mode, base, sel] of [
['light', light, `[data-venue-theme="${venue}"]`],
['dark', dark, `[data-theme="dark"][data-venue-theme="${venue}"], [data-theme="dark"] [data-venue-theme="${venue}"]`],
]) {
test(`${venue} / ${mode}`, () => {
const t = { ...base, ...rule(sel) }
const failures = pairs
.map(([fg, bg, min]) => [fg, bg, min, ratio(t[fg], t[bg])])
.filter(([, , min, r]) => r < min)
.map(([fg, bg, min, r]) => `${fg} on ${bg}: ${r.toFixed(2)} < ${min}`)
assert.deepEqual(failures, [])
})
}
}
test('system theme mirrors dark', () => {
const sys = css.slice(css.indexOf('@media (prefers-color-scheme: dark)'))
assert.match(sys, /\[data-theme="system"\]/)
const darkCanvas = dark.canvas.join(' ')
assert.ok(sys.includes(`--x-canvas-rgb: ${darkCanvas};`), 'system dark uses the dark canvas')
})