Files
RuneStone/tests/unit/theme/contrast.spec.ts
T
Knotty Wyvern 445d0200b2 Make menus opaque and redesign the core palettes
Phase 5a-3. The owner chose candidate B of each palette.

- Menus are opaque (UI-043, replaces UI-013). With a background image,
  menus are at least 92% opaque and blur the image. Submenus show as
  manual popovers in the top layer, so the blur cannot clip them.
- In a light palette, the page is the lightest surface, and the side
  panes have a slight tint (UI-044).
- RuneStone uses neutral surfaces and the rune cyan of the app icon.
  Claude Dark uses the layout of the Claude app. Claude Light uses a
  white page with brand Light panes and a darker orange for text.
  Sakura uses Petal Veil for active rows.
- All five core palettes pass every readability rule. A text role keeps
  the hue of its source and changes only its lightness.
- The accent-text rule also checks accent-2, because inline code in
  Read uses it as text.
- The root token defaults match RuneStone Dark.

pnpm check passes. pnpm test: 447 pass, 5 todo, 0 fail. Full smoke
suite: 92 passed, 2 known. editor/markdownPaste and tables/formulas
failed one time and passed on retry. workspace/manageTemplates fails
about half of its runs, also on the previous commit: the tree menu
rebuilds when the template list reloads, and the open submenu closes.
2026-10-28 16:47:55 -07:00

75 lines
3.9 KiB
TypeScript

// Readability rules for every built-in theme (src/lib/theme/contrast.ts), and the rule that only theme data names a theme.
import assert from 'node:assert/strict';
import { test } from 'node:test';
import { readdirSync, readFileSync, statSync } from 'node:fs';
import { join, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import { BUILTIN_THEMES } from '../../../src/lib/theme/themes/index.ts';
import { BASE_ROLES, resolveColors, type Mode } from '../../../src/lib/theme/roles.ts';
import { RULES, checkTheme } from '../../../src/lib/theme/contrast.ts';
import { composite, contrast, distance, formatHex, mix, parseHex } from '../../../src/lib/theme/color.ts';
// Rules that a palette fails today. Stage 5a-4 redesigns these palettes and removes each entry.
const KNOWN_FAILURES: Record<string, readonly string[]> = {
'harvest-moon/dark': ['status-text', 'status-distinct'],
'crimson-pilot/dark': ['status-distinct'],
'silver-serpent/dark': ['status-text', 'status-distinct'],
};
const variants = BUILTIN_THEMES.flatMap(theme => Object.entries(theme.variants).map(([mode, colors]) => ({ key: `${theme.id}/${mode}`, mode: mode as Mode, colors: colors! })));
for (const variant of variants) {
const findings = checkTheme(resolveColors(variant.colors, variant.mode), variant.mode);
for (const rule of RULES) {
const results = findings.filter(finding => finding.rule === rule.id);
if (!results.length) continue;
const known = KNOWN_FAILURES[variant.key]?.includes(rule.id);
const failures = results.filter(finding => !finding.pass).map(finding => `${finding.subject}: ${finding.value.toFixed(2)} (minimum ${finding.min})`);
test(`${variant.key} passes the ${rule.id} rule`, known ? { todo: 'Known palette failure. Stage 5a-4 redesigns this palette.' } : {}, () => {
assert.deepEqual(failures, []);
});
if (known) test(`${variant.key} still fails the known ${rule.id} rule`, () => {
assert(failures.length > 0, `Remove ${rule.id} from KNOWN_FAILURES['${variant.key}'].`);
});
}
}
test('Each known failure names a built-in variant and a rule', () => {
for (const [key, rules] of Object.entries(KNOWN_FAILURES)) {
assert(variants.some(variant => variant.key === key), key);
for (const rule of rules) assert(RULES.some(item => item.id === rule), `${key}: ${rule}`);
}
});
test('Every built-in variant gives all base roles as hex colors', () => {
for (const variant of variants) {
for (const role of BASE_ROLES) assert.match(variant.colors[role] ?? '', /^#[0-9a-f]{6}$/, `${variant.key}: ${role}`);
}
});
test('Color arithmetic matches CSS and WCAG', () => {
assert.deepEqual(parseHex('#0f08'), [0, 255, 0, 136 / 255]);
assert.equal(formatHex([255, 0, 0, 1]), '#ff0000');
assert.equal(mix('#82aaff', '#141414', 0.8), '#6c8cd0');
assert.equal(mix('#82aaff', '#0000', 0.2), '#82aaff33');
assert.equal(composite('#ffffff80', '#000000'), '#808080');
assert.equal(contrast('#000000', '#ffffff').toFixed(1), '21.0');
assert(distance('#ff0000', '#ff0000') === 0 && distance('#000000', '#ffffff') > 0.99);
});
test('Only theme data names a theme ID', () => {
const root = fileURLToPath(new URL('../../../src', import.meta.url));
const ids = BUILTIN_THEMES.flatMap(theme => [theme.id, ...('legacyIds' in theme ? theme.legacyIds : [])]).filter(id => id !== 'default');
const pattern = new RegExp(`(['"\`])(?:${ids.join('|')})\\1|data-palette=`);
const files = (function walk(folder: string): string[] {
return readdirSync(folder).flatMap(name => {
const path = join(folder, name);
return statSync(path).isDirectory() ? walk(path) : /\.(ts|svelte|css)$/.test(name) ? [path] : [];
});
})(root);
const found = files.filter(file => !relative(root, file).startsWith(join('lib', 'theme', 'themes')) && !file.endsWith('messages.ts'))
.filter(file => pattern.test(readFileSync(file, 'utf8')))
.map(file => relative(root, file));
assert.deepEqual(found, []);
});