|
| 1 | +/** |
| 2 | + * WCAG 2.1 AA Contrast Verification Tests |
| 3 | + * |
| 4 | + * Verifies that the Shadcn/Tailwind CSS custom property HSL values used by |
| 5 | + * both light and dark themes meet WCAG 2.1 AA contrast ratio requirements. |
| 6 | + * |
| 7 | + * Since JSDOM/happy-dom cannot compute actual CSS, we test the raw HSL values |
| 8 | + * defined in index.css programmatically. |
| 9 | + * |
| 10 | + * WCAG AA thresholds: |
| 11 | + * - Normal text (< 18pt / < 14pt bold): contrast ratio >= 4.5:1 |
| 12 | + * - Large text (>= 18pt / >= 14pt bold): contrast ratio >= 3:1 |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect } from 'vitest'; |
| 16 | + |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | +// Theme HSL values (from apps/console/src/index.css :root and .dark) |
| 19 | +// Format: [hue, saturation%, lightness%] |
| 20 | +// --------------------------------------------------------------------------- |
| 21 | + |
| 22 | +type HSL = [number, number, number]; |
| 23 | + |
| 24 | +interface ThemeTokens { |
| 25 | + background: HSL; |
| 26 | + foreground: HSL; |
| 27 | + card: HSL; |
| 28 | + 'card-foreground': HSL; |
| 29 | + primary: HSL; |
| 30 | + 'primary-foreground': HSL; |
| 31 | + muted: HSL; |
| 32 | + 'muted-foreground': HSL; |
| 33 | + destructive: HSL; |
| 34 | + 'destructive-foreground': HSL; |
| 35 | +} |
| 36 | + |
| 37 | +const lightTheme: ThemeTokens = { |
| 38 | + background: [0, 0, 100], |
| 39 | + foreground: [222.2, 84, 4.9], |
| 40 | + card: [0, 0, 100], |
| 41 | + 'card-foreground': [222.2, 84, 4.9], |
| 42 | + primary: [222.2, 47.4, 11.2], |
| 43 | + 'primary-foreground': [210, 40, 98], |
| 44 | + muted: [210, 40, 96.1], |
| 45 | + 'muted-foreground': [215.4, 16.3, 46.9], |
| 46 | + destructive: [0, 84.2, 60.2], |
| 47 | + 'destructive-foreground': [210, 40, 98], |
| 48 | +}; |
| 49 | + |
| 50 | +const darkTheme: ThemeTokens = { |
| 51 | + background: [222.2, 84, 4.9], |
| 52 | + foreground: [210, 40, 98], |
| 53 | + card: [222.2, 84, 4.9], |
| 54 | + 'card-foreground': [210, 40, 98], |
| 55 | + primary: [210, 40, 98], |
| 56 | + 'primary-foreground': [222.2, 47.4, 11.2], |
| 57 | + muted: [217.2, 32.6, 17.5], |
| 58 | + 'muted-foreground': [215, 20.2, 65.1], |
| 59 | + destructive: [0, 62.8, 30.6], |
| 60 | + 'destructive-foreground': [210, 40, 98], |
| 61 | +}; |
| 62 | + |
| 63 | +// --------------------------------------------------------------------------- |
| 64 | +// Color conversion & contrast helpers |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | + |
| 67 | +/** Convert a single HSL channel value to its RGB component contribution. */ |
| 68 | +function hueToRgbChannel(p: number, q: number, t: number): number { |
| 69 | + if (t < 0) t += 1; |
| 70 | + if (t > 1) t -= 1; |
| 71 | + if (t < 1 / 6) return p + (q - p) * 6 * t; |
| 72 | + if (t < 1 / 2) return q; |
| 73 | + if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; |
| 74 | + return p; |
| 75 | +} |
| 76 | + |
| 77 | +/** Convert HSL [h (0-360), s (0-100), l (0-100)] to RGB [0-255, 0-255, 0-255]. */ |
| 78 | +function hslToRgb(hsl: HSL): [number, number, number] { |
| 79 | + const h = hsl[0] / 360; |
| 80 | + const s = hsl[1] / 100; |
| 81 | + const l = hsl[2] / 100; |
| 82 | + |
| 83 | + if (s === 0) { |
| 84 | + const v = Math.round(l * 255); |
| 85 | + return [v, v, v]; |
| 86 | + } |
| 87 | + |
| 88 | + const q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
| 89 | + const p = 2 * l - q; |
| 90 | + |
| 91 | + return [ |
| 92 | + Math.round(hueToRgbChannel(p, q, h + 1 / 3) * 255), |
| 93 | + Math.round(hueToRgbChannel(p, q, h) * 255), |
| 94 | + Math.round(hueToRgbChannel(p, q, h - 1 / 3) * 255), |
| 95 | + ]; |
| 96 | +} |
| 97 | + |
| 98 | +/** Linearize an sRGB channel value (0-255) for relative luminance calculation. */ |
| 99 | +function linearize(channel: number): number { |
| 100 | + const c = channel / 255; |
| 101 | + return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4); |
| 102 | +} |
| 103 | + |
| 104 | +/** Calculate relative luminance per WCAG 2.1 definition. */ |
| 105 | +function relativeLuminance(rgb: [number, number, number]): number { |
| 106 | + return 0.2126 * linearize(rgb[0]) + 0.7152 * linearize(rgb[1]) + 0.0722 * linearize(rgb[2]); |
| 107 | +} |
| 108 | + |
| 109 | +/** Calculate contrast ratio between two HSL colors (returns ratio as N:1). */ |
| 110 | +function contrastRatio(foreground: HSL, background: HSL): number { |
| 111 | + const lum1 = relativeLuminance(hslToRgb(foreground)); |
| 112 | + const lum2 = relativeLuminance(hslToRgb(background)); |
| 113 | + const lighter = Math.max(lum1, lum2); |
| 114 | + const darker = Math.min(lum1, lum2); |
| 115 | + return (lighter + 0.05) / (darker + 0.05); |
| 116 | +} |
| 117 | + |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +// WCAG AA minimum contrast ratios |
| 120 | +// --------------------------------------------------------------------------- |
| 121 | +const AA_NORMAL_TEXT = 4.5; |
| 122 | +const AA_LARGE_TEXT = 3.0; |
| 123 | + |
| 124 | +// --------------------------------------------------------------------------- |
| 125 | +// Tests |
| 126 | +// --------------------------------------------------------------------------- |
| 127 | + |
| 128 | +describe('WCAG 2.1 AA Contrast Verification', () => { |
| 129 | + describe('helper: hslToRgb', () => { |
| 130 | + it('converts pure white', () => { |
| 131 | + expect(hslToRgb([0, 0, 100])).toEqual([255, 255, 255]); |
| 132 | + }); |
| 133 | + |
| 134 | + it('converts pure black', () => { |
| 135 | + expect(hslToRgb([0, 0, 0])).toEqual([0, 0, 0]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('converts pure red', () => { |
| 139 | + expect(hslToRgb([0, 100, 50])).toEqual([255, 0, 0]); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('helper: contrastRatio', () => { |
| 144 | + it('returns 21:1 for black on white', () => { |
| 145 | + const ratio = contrastRatio([0, 0, 0], [0, 0, 100]); |
| 146 | + expect(ratio).toBeCloseTo(21, 0); |
| 147 | + }); |
| 148 | + |
| 149 | + it('returns 1:1 for identical colors', () => { |
| 150 | + const ratio = contrastRatio([210, 40, 98], [210, 40, 98]); |
| 151 | + expect(ratio).toBeCloseTo(1, 1); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('Light theme – normal text (>= 4.5:1)', () => { |
| 156 | + it('foreground on background', () => { |
| 157 | + const ratio = contrastRatio(lightTheme.foreground, lightTheme.background); |
| 158 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 159 | + }); |
| 160 | + |
| 161 | + it('primary-foreground on primary', () => { |
| 162 | + const ratio = contrastRatio(lightTheme['primary-foreground'], lightTheme.primary); |
| 163 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 164 | + }); |
| 165 | + |
| 166 | + it('card-foreground on card', () => { |
| 167 | + const ratio = contrastRatio(lightTheme['card-foreground'], lightTheme.card); |
| 168 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('Light theme – large text / UI components (>= 3:1)', () => { |
| 173 | + it('destructive-foreground on destructive', () => { |
| 174 | + const ratio = contrastRatio( |
| 175 | + lightTheme['destructive-foreground'], |
| 176 | + lightTheme.destructive, |
| 177 | + ); |
| 178 | + expect(ratio).toBeGreaterThanOrEqual(AA_LARGE_TEXT); |
| 179 | + }); |
| 180 | + |
| 181 | + it('muted-foreground on muted (secondary/helper text)', () => { |
| 182 | + const ratio = contrastRatio(lightTheme['muted-foreground'], lightTheme.muted); |
| 183 | + expect(ratio).toBeGreaterThanOrEqual(AA_LARGE_TEXT); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe('Dark theme – normal text (>= 4.5:1)', () => { |
| 188 | + it('foreground on background', () => { |
| 189 | + const ratio = contrastRatio(darkTheme.foreground, darkTheme.background); |
| 190 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 191 | + }); |
| 192 | + |
| 193 | + it('primary-foreground on primary', () => { |
| 194 | + const ratio = contrastRatio(darkTheme['primary-foreground'], darkTheme.primary); |
| 195 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 196 | + }); |
| 197 | + |
| 198 | + it('muted-foreground on muted', () => { |
| 199 | + const ratio = contrastRatio(darkTheme['muted-foreground'], darkTheme.muted); |
| 200 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 201 | + }); |
| 202 | + |
| 203 | + it('card-foreground on card', () => { |
| 204 | + const ratio = contrastRatio(darkTheme['card-foreground'], darkTheme.card); |
| 205 | + expect(ratio).toBeGreaterThanOrEqual(AA_NORMAL_TEXT); |
| 206 | + }); |
| 207 | + }); |
| 208 | + |
| 209 | + describe('Dark theme – large text (>= 3:1)', () => { |
| 210 | + it('destructive-foreground on destructive', () => { |
| 211 | + const ratio = contrastRatio( |
| 212 | + darkTheme['destructive-foreground'], |
| 213 | + darkTheme.destructive, |
| 214 | + ); |
| 215 | + expect(ratio).toBeGreaterThanOrEqual(AA_LARGE_TEXT); |
| 216 | + }); |
| 217 | + }); |
| 218 | +}); |
0 commit comments