Give themes named color roles, theme data files, and file format 3
Phase 5a-1 and 5a-2. - roles.ts names 51 color roles. One derivation rule fills the roles that a theme does not give, for all themes. tokens.css keeps only the root defaults, without theme blocks. - Each built-in theme is one data file in src/lib/theme/themes/. No code outside the theme data names a theme ID. characterThemes.ts is gone. - New roles: link, focus, on-accent, highlight, scrim, the syntax roles, and 16 terminal (ANSI) roles. Stray colors in components use the roles. - contrast.ts checks each theme against the readability rules of docs/ui-color.md. The tests mark the known palette failures as todo, and a second test fails when a known failure starts to pass. - Theme files use schema 3 with a mode and an optional built-in base. A short file gets the missing colors and fonts from the base. Schema 1 and 2 files still import. Old syntax names convert on import. - Docs: ui-color.md, plugin-development.md (theme tokens), ui-design.md, guides/themes.md, and UI-041 and UI-042 in design-decisions.md. The applied colors stay the same for every built-in theme, except three small changes: the conflict line uses the warn color, the Write highlight uses the highlight role, and two dialog backdrops use the scrim role. pnpm check passes. pnpm test: 447 pass, 31 todo, 0 fail. Full smoke suite: 93 passed, 2 known. appearance/palettes read the old syntax names and passed 3 of 3 times after the fix. workspace/manageTemplates failed one time and passed on retry.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// 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. Stages 5a-3 and 5a-4 redesign these palettes and remove each entry.
|
||||
const KNOWN_FAILURES: Record<string, readonly string[]> = {
|
||||
'default/dark': ['text', 'syntax-variety'],
|
||||
'default/light': ['text', 'syntax', 'syntax-variety', 'light-page'],
|
||||
'claude/dark': ['accent-text', 'status-text', 'syntax', 'status-distinct', 'syntax-variety'],
|
||||
'claude/light': ['accent-text', 'status-text', 'syntax', 'ansi', 'focus', 'selection-outline', 'syntax-variety', 'light-page'],
|
||||
'sakura/light': ['text', 'accent-text', 'status-text', 'syntax', 'ansi', 'syntax-variety', 'light-page'],
|
||||
'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. Stages 5a-3 and 5a-4 redesign 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, []);
|
||||
});
|
||||
Reference in New Issue
Block a user