// Theme engine v3, step 5: every surface and every role meets its target, for built-in-like and random palettes. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { contrastRatio } from '../src/themeApi.ts'; import { deriveSurfaces, reachable, surfacesInputFromV2, SURFACE_NAMES, type DeriveInput } from '../src/themeDerive.ts'; import { hexToOklch } from '../src/themeInk.ts'; function rng(seed: number) { return () => { seed |= 0; seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } const randomHex = (r: () => number) => `#${Array.from({ length: 3 }, () => Math.floor(r() * 256).toString(16).padStart(2, '0')).join('')}`; /** Every role of every surface against the fill(s) and states it sits on. */ function assertTargets(input: DeriveInput, label: string) { const set = deriveSurfaces(input); for (const name of SURFACE_NAMES) { const s = set[name]; const grounds = [s.fill, ...(s.fill2 ? [s.fill2] : [])]; // The target, or the best any ink can do on this fill (a mid tone has no 7:1 ink; the deriver notes it). const best = reachable(grounds); const text = Math.min(best, input.access === 'high' ? 7 : 4.5); const soft = Math.min(best, 4.5); for (const g of [...grounds, s.hover, s.selected]) { assert.ok(contrastRatio(s.ink, g) >= text - 1e-9, `${label} ${name}.ink ${s.ink} on ${g}`); assert.ok(contrastRatio(s.inkSoft, g) >= soft - 1e-9, `${label} ${name}.inkSoft on ${g}`); assert.ok(contrastRatio(s.link, g) >= soft - 1e-9, `${label} ${name}.link on ${g}`); } for (const g of grounds) assert.ok(contrastRatio(s.focus, g) >= Math.min(best, 3) - 1e-9, `${label} ${name}.focus on ${g}`); // A hover state is a visible step but never a different colour family. const step = contrastRatio(s.hover, s.fill); if (!input.palette.hover) assert.ok(step <= 1.6, `${label} ${name}.hover step ${step.toFixed(3)}`); } return set; } test('Windows 98: silver chrome, navy-to-blue title bar with one readable ink, silver hover', () => { const set = assertTargets(surfacesInputFromV2({ page: '#c0c0c0', bg: '#ffffff', surface: '#f4f4f4', elevated: '#dfdfdf', border: '#808080', text: '#000000', muted: '#3c3c3c', accent: '#0000a8', inverse: '#000080', onInverse: '#ffffff' }, { bevel: true }), 'w98'); assert.equal(set.chrome.fill, '#c0c0c0'); assert.equal(set.strong.fill, '#000080'); assert.ok(contrastRatio(set.strong.ink, set.strong.fill2 ?? set.strong.fill) >= 4.5); assert.equal(set.edge, 'bevel'); }); test('the content hover is never an accent wash (the 1.0.2 lavender)', () => { const set = deriveSurfaces(surfacesInputFromV2({ bg: '#ffffff', surface: '#f4f4f4', elevated: '#dfdfdf', border: '#808080', text: '#000000', muted: '#3c3c3c', accent: '#0000a8' })); const hover = hexToOklch(set.content.hover); assert.ok(hover.c < 0.01, `hover ${set.content.hover} has chroma ${hover.c.toFixed(3)}`); }); test('5,000 random palettes: every surface and role meets its target (standard and high access)', () => { const r = rng(5000); for (let i = 0; i < 5000; i++) { const bg = randomHex(r); const palette = { bg, surface: randomHex(r), elevated: randomHex(r), border: randomHex(r), text: randomHex(r), muted: randomHex(r), accent: randomHex(r), page: randomHex(r), inverse: randomHex(r), inverse2: r() < 0.5 ? randomHex(r) : null }; assertTargets({ palette, edge: { style: r() < 0.3 ? 'bevel' : 'hairline' }, access: i % 5 === 0 ? 'high' : 'standard' }, `#${i}`); } });