Files
xenia-pos-local/packages/ui/tests/contrast.test.mjs
T
bonaminandClaude Opus 5.5 77e0adbe9c feat(ui): board design language as global tokens; KDS on tokens and shared components (platform-v2 3.2b-2)
- @xenia/ui: --x-board-* tokens (the kitchen display's look: dark board, light cards,
  status colours, card radius/shadows, number font), Tailwind board.* colours, contrast
  test for the board pairs; near-identical shades merged.
- New shared Stepper component (gallery + ui.mjs), labels ui.decrease / ui.increase.
- KDS: theme.js maps to the board tokens, no colour literals left (only stored setting
  values); decline / call waiter / settings use Dialog, Button, Switch, Select, Stepper;
  toasts are @xenia/ui toasts (ToastProvider at the terminal root).
- Terminal: apps can set their theme (APP_THEMES, KDS dark) while they run.
- CS-16: board screens and "no hard-coded colours in app code".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 20:02:35 +02:00

85 lines
4.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')
})
// The board (kitchen display, 3.2b-2): always dark, so one set. Done items (card-muted) and
// empty states (ink-4) are meant to recede and aren't checked.
test('board token pairs are readable', () => {
const b = Object.fromEntries([...css.matchAll(/--x-board-([\w-]+)-rgb:\s*([\d\s]+);/g)].map(([, k, v]) => [k, v.trim().split(/\s+/).map(Number)]))
const boardPairs = [
['ink', 'bg', TEXT], ['ink-2', 'bg', TEXT], ['ink-2', 'line', TEXT], ['ink-3', 'rail', TEXT], ['bg', 'tab-active', TEXT],
['card-ink', 'card', TEXT], ['card-ink-2', 'card-soft', TEXT], ['note-ink', 'note', TEXT],
['late-deep', 'card', TEXT], ['fresh-deep', 'card', TEXT], ['on-status', 'fresh-deep', TEXT], ['on-status', 'late-deep', TEXT],
['warning-deep', 'card', UI], ['progress', 'card', UI], ['on-status', 'warning-deep', UI],
]
for (const [fg, bg, min] of boardPairs) {
assert.ok(b[fg] && b[bg], `board tokens ${fg} / ${bg} exist`)
const r = ratio(b[fg], b[bg])
assert.ok(r >= min, `board ${fg} on ${bg}: ${r.toFixed(2)} < ${min}`)
}
})