theme.test.ts (9322B)
1 import { readFileSync } from 'node:fs'; 2 import { describe, expect, it } from 'vitest'; 3 4 // This standalone contract records approved primitives, not parent-file inputs. 5 const approved = { 6 '--color-page': '#ffffff', 7 '--color-surface': 'var(--color-page)', 8 '--color-surface-muted': '#f4f5f7', 9 '--color-text': '#202124', 10 '--color-text-muted': '#5e6268', 11 '--color-border': '#d4d7dc', 12 '--color-control-border': '#73777f', 13 '--color-primary': '#1769c2', 14 '--color-primary-hover': '#12559e', 15 '--color-primary-active': '#104984', 16 '--color-on-primary': 'var(--color-page)', 17 '--color-secondary': '#f2f3f5', 18 '--color-secondary-hover': '#e6e8eb', 19 '--color-secondary-active': '#d9dde2', 20 '--color-focus': '#005fcc', 21 '--color-danger': '#9d2424', 22 '--color-warning': '#76520b', 23 '--color-success': '#246234', 24 '--color-disabled-text': '#62666d', 25 '--font-family-body': 26 "-apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif", 27 '--font-family-mono': "ui-monospace, 'SFMono-Regular', Consolas, monospace", 28 '--font-size-small': '0.875rem', 29 '--font-size-body': '1rem', 30 '--font-size-title': '1.625rem', 31 '--font-size-heading': '1.125rem', 32 '--font-weight-normal': '400', 33 '--font-weight-emphasis': '600', 34 '--line-height-body': '1.5', 35 '--line-height-heading': '1.25', 36 '--space-1': '0.25rem', 37 '--space-2': '0.5rem', 38 '--space-3': '0.75rem', 39 '--space-4': '1rem', 40 '--space-5': '1.5rem', 41 '--space-6': '2rem', 42 '--space-7': '3rem', 43 '--border-hairline': '1px', 44 '--border-focus': '2px', 45 '--focus-offset': '2px', 46 '--radius-control': '4px', 47 '--control-min-height': '2.75rem', 48 '--textarea-min-height': '7rem', 49 '--measure-page': '64rem', 50 '--measure-form': '42rem', 51 '--measure-control-min': '14rem', 52 '--measure-key': '32ch', 53 '--size-visually-hidden': '1px', 54 '--visually-hidden-offset': '-1px' 55 } as const; 56 57 type Token = keyof typeof approved; 58 59 function source(): string { 60 return readFileSync(new URL('../../src/theme.css', import.meta.url), 'utf8'); 61 } 62 63 // Deliberately accepts only this small primitive-only CSS grammar. 64 function primitives(css: string): Map<string, string> { 65 const clean = css.replace(/\/\*[\s\S]*?\*\//g, '').trim(); 66 const root = 67 /^@layer reset, base, components, utilities;\s*@layer base\s*\{\s*:root\s*\{([^{}]*)\}\s*\}$/.exec( 68 clean 69 ); 70 if (!root) 71 throw new Error( 72 'Theme must contain only ordered layers and one base :root' 73 ); 74 const entries = new Map<string, string>(); 75 for (const declaration of root[1] 76 .split(';') 77 .map((item) => item.trim()) 78 .filter(Boolean)) { 79 const pair = /^(color-scheme|--[a-z][a-z0-9-]*):\s*(.+)$/.exec(declaration); 80 if (!pair || entries.has(pair[1])) 81 throw new Error(`Invalid or duplicate primitive: ${declaration}`); 82 entries.set(pair[1], pair[2].trim()); 83 } 84 return entries; 85 } 86 87 function color(tokens: Map<string, string>, name: Token): string { 88 const value = tokens.get(name); 89 const alias = /^var\((--[a-z-]+)\)$/.exec(value ?? ''); 90 // The only approved color aliases point to the literal white page primitive. 91 const resolved = alias ? tokens.get(alias[1]) : value; 92 if (!resolved || !/^#[0-9a-f]{6}$/i.test(resolved)) 93 throw new Error(`Invalid color: ${name}`); 94 return resolved; 95 } 96 97 // WCAG 2.2 sRGB relative luminance and contrast, for source arithmetic only: 98 // https://www.w3.org/TR/WCAG22/#dfn-relative-luminance 99 function linear(channel: number): number { 100 return channel <= 0.04045 101 ? channel / 12.92 102 : ((channel + 0.055) / 1.055) ** 2.4; 103 } 104 105 function luminance(hex: string): number { 106 const channels = [1, 3, 5].map((offset) => 107 linear(parseInt(hex.slice(offset, offset + 2), 16) / 255) 108 ); 109 return 0.2126 * channels[0] + 0.7152 * channels[1] + 0.0722 * channels[2]; 110 } 111 112 function contrast(foreground: string, background: string): number { 113 const a = luminance(foreground); 114 const b = luminance(background); 115 return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05); 116 } 117 118 const surfaces: Token[] = [ 119 '--color-page', 120 '--color-surface', 121 '--color-surface-muted' 122 ]; 123 const primary: Token[] = [ 124 '--color-primary', 125 '--color-primary-hover', 126 '--color-primary-active' 127 ]; 128 const secondary: Token[] = [ 129 '--color-secondary', 130 '--color-secondary-hover', 131 '--color-secondary-active' 132 ]; 133 const text: Token[] = [ 134 '--color-text', 135 '--color-text-muted', 136 '--color-danger', 137 '--color-warning', 138 '--color-success' 139 ]; 140 141 describe('approved theme primitives (source checks, not rendered accessibility)', () => { 142 it('declares exactly one approved value per primitive in one root', () => { 143 expect(Object.fromEntries(primitives(source()))).toEqual({ 144 'color-scheme': 'light', 145 ...approved 146 }); 147 }); 148 149 it('rejects duplicate declarations even when their values are identical', () => { 150 expect(() => 151 primitives( 152 source().replace( 153 '--color-page:', 154 '--color-page: #ffffff; --color-page:' 155 ) 156 ) 157 ).toThrow('duplicate primitive'); 158 }); 159 160 it('contains only local system font stacks and no font or external style loading', () => { 161 const css = source(); 162 const tokens = primitives(css); 163 expect(tokens.get('--font-family-body')).toBe( 164 approved['--font-family-body'] 165 ); 166 expect(tokens.get('--font-family-mono')).toBe( 167 approved['--font-family-mono'] 168 ); 169 expect(css).not.toMatch(/@import|@font-face|url\s*\(|https?:|\/\//i); 170 }); 171 172 it('preserves the page/form measures and 44px control goal at a 16px root', () => { 173 const tokens = primitives(source()); 174 expect(tokens.get('--measure-page')).toBe('64rem'); 175 expect(tokens.get('--measure-form')).toBe('42rem'); 176 expect(tokens.get('--control-min-height')).toBe('2.75rem'); 177 expect(parseFloat(tokens.get('--control-min-height')!) * 16).toBe(44); 178 }); 179 180 it('checks the luminance oracle at both transfer branches and known contrast anchors', () => { 181 expect(linear(0.04045)).toBeCloseTo(0.0031308049535603713, 15); 182 expect(linear(0.04046)).toBeCloseTo(0.003131594552688991, 15); 183 expect(luminance('#ff0000')).toBeCloseTo(0.2126, 12); 184 expect(luminance('#00ff00')).toBeCloseTo(0.7152, 12); 185 expect(luminance('#0000ff')).toBeCloseTo(0.0722, 12); 186 expect(contrast('#000000', '#ffffff')).toBe(21); 187 expect(contrast('#ffffff', '#000000')).toBe(21); 188 expect(contrast('#777777', '#777777')).toBe(1); 189 expect(contrast('#777777', '#ffffff')).toBeLessThan(4.5); 190 expect(contrast('#767676', '#ffffff')).toBeGreaterThanOrEqual(4.5); 191 }); 192 193 it.each( 194 text.flatMap((foreground) => 195 surfaces.map((background) => [foreground, background] as const) 196 ) 197 )('ordinary %s text on %s reaches 4.5:1', (foreground, background) => { 198 const tokens = primitives(source()); 199 expect( 200 contrast(color(tokens, foreground), color(tokens, background)) 201 ).toBeGreaterThanOrEqual(4.5); 202 }); 203 204 it.each(primary)('white primary text on %s reaches 4.5:1', (background) => { 205 const tokens = primitives(source()); 206 expect( 207 contrast(color(tokens, '--color-on-primary'), color(tokens, background)) 208 ).toBeGreaterThanOrEqual(4.5); 209 }); 210 211 it.each( 212 primary.flatMap((foreground) => 213 surfaces.map((background) => [foreground, background] as const) 214 ) 215 )('%s link text on %s reaches 4.5:1', (foreground, background) => { 216 const tokens = primitives(source()); 217 expect( 218 contrast(color(tokens, foreground), color(tokens, background)) 219 ).toBeGreaterThanOrEqual(4.5); 220 }); 221 222 it.each(secondary)( 223 'secondary text and useful boundary on %s remain distinct', 224 (background) => { 225 const tokens = primitives(source()); 226 expect( 227 contrast(color(tokens, '--color-text'), color(tokens, background)) 228 ).toBeGreaterThanOrEqual(4.5); 229 expect( 230 contrast( 231 color(tokens, '--color-control-border'), 232 color(tokens, background) 233 ) 234 ).toBeGreaterThanOrEqual(3); 235 // Gray fills alone do not identify the control; later compositions must use its stronger border. 236 expect( 237 contrast(color(tokens, background), color(tokens, '--color-page')) 238 ).toBeLessThan(3); 239 } 240 ); 241 242 it.each(surfaces)( 243 'useful control boundaries and offset focus on %s reach 3:1', 244 (background) => { 245 const tokens = primitives(source()); 246 for (const foreground of [ 247 '--color-control-border', 248 '--color-focus', 249 ...primary 250 ] as Token[]) { 251 expect( 252 contrast(color(tokens, foreground), color(tokens, background)) 253 ).toBeGreaterThanOrEqual(3); 254 } 255 } 256 ); 257 258 it.each(surfaces)( 259 'decorative hairlines on %s are not admitted as useful control boundaries', 260 (background) => { 261 const tokens = primitives(source()); 262 expect(color(tokens, '--color-border')).not.toBe( 263 color(tokens, '--color-control-border') 264 ); 265 expect( 266 contrast(color(tokens, '--color-border'), color(tokens, background)) 267 ).toBeLessThan(3); 268 } 269 ); 270 271 it('measures disabled text on its secondary fill without making an active-control compliance claim', () => { 272 const tokens = primitives(source()); 273 expect( 274 contrast( 275 color(tokens, '--color-disabled-text'), 276 color(tokens, '--color-secondary') 277 ) 278 ).toBeGreaterThanOrEqual(4.5); 279 }); 280 });