| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { describe, expect, it } from "vitest"; |
| 3 | import { resolveWhale } from "./whale-tokens"; |
| 4 | import { siteCss } from "./site-css"; |
| 5 | |
| 6 | // The role layer (app/styles/tokens-roles.css): every role in every scheme, |
| 7 | // drawn from the codewhale-design 5.x tokens, every ink measured against |
| 8 | // every ground at WCAG AA. |
| 9 | const CSS = siteCss(); |
| 10 | const ROLES = [ |
| 11 | "bg", "surface", "panel", "text", "muted", "line", "line-strong", "accent", "on-accent", |
| 12 | "hover", "selected", "selection", "ring", "live", "attention", "danger", |
| 13 | ]; |
| 14 | const GROUNDS = ["bg", "surface", "panel", "hover", "selected"]; |
| 15 | const TEXT_INKS = ["text", "muted", "accent", "live", "attention", "danger"]; |
| 16 | |
| 17 | function declarations(block: string): Record<string, string> { |
| 18 | const vars: Record<string, string> = {}; |
| 19 | for (const match of block.matchAll(/--([\w-]+):\s*([^;]+);/g)) vars[match[1]] = match[2].trim(); |
| 20 | return vars; |
| 21 | } |
| 22 | |
| 23 | /** The first block for `selector` (at any indent) that declares `--bg`. */ |
| 24 | function roleBlock(selector: string): Record<string, string> { |
| 25 | const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 26 | for (const match of CSS.matchAll(new RegExp(`(?:^|\\n)\\s*${escaped}\\s*\\{([^}]*)\\}`, "g"))) { |
| 27 | const vars = declarations(match[1]); |
| 28 | if ("bg" in vars) return vars; |
| 29 | } |
| 30 | throw new Error(`No role block for ${selector}`); |
| 31 | } |
| 32 | |
| 33 | function hex(value: string): string { |
| 34 | const resolved = resolveWhale(value); |
| 35 | if (!/^#[0-9a-f]{6}$/i.test(resolved)) throw new Error(`not a hex color: ${value} -> ${resolved}`); |
| 36 | return resolved.toLowerCase(); |
| 37 | } |
| 38 | |
| 39 | function luminance(color: string): number { |
| 40 | const [r, g, b] = color |
| 41 | .slice(1) |
| 42 | .match(/.{2}/g)! |
| 43 | .map((v) => Number.parseInt(v, 16) / 255) |
| 44 | .map((v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4)); |
| 45 | return 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 46 | } |
| 47 | |
| 48 | function contrast(a: string, b: string): number { |
| 49 | const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x); |
| 50 | return (hi + 0.05) / (lo + 0.05); |
| 51 | } |
| 52 | |
| 53 | const light = () => roleBlock(":root"); |
| 54 | const pinnedDark = () => roleBlock(':root[data-theme="dark"]'); |
| 55 | |
| 56 | describe("role tokens", () => { |
| 57 | it("defines every role in light and pinned-dark", () => { |
| 58 | for (const [name, scheme] of [["light", light()], ["pinned", pinnedDark()]] as const) { |
| 59 | for (const role of ROLES) expect(scheme, `${name}.${role}`).toHaveProperty(role); |
| 60 | } |
| 61 | // Light is the site's appearance: dark is a reader's pin, never the OS. |
| 62 | expect(CSS).not.toMatch(/@media \(prefers-color-scheme: dark\)/); |
| 63 | }); |
| 64 | |
| 65 | it("takes every role from the codewhale-design 5.x light and dark tokens", () => { |
| 66 | const gpui = (mode: string, name: string) => hex(`var(--gpui-${mode}-${name})`); |
| 67 | const inks: Record<string, Record<string, string>> = { |
| 68 | light: { bg: "bg", panel: "panel", surface: "sidebar", text: "text", muted: "text-muted", line: "border", "line-strong": "border-strong", accent: "primary", "on-accent": "on-primary", ring: "accent", live: "live", attention: "attention", danger: "danger", hover: "hover", selected: "list-active" }, |
| 69 | dark: { bg: "bg", panel: "sidebar", surface: "panel", text: "text", muted: "text-muted", line: "border", "line-strong": "border-strong", accent: "accent", ring: "accent", live: "live", attention: "attention", danger: "danger", hover: "hover", selected: "list-active" }, |
| 70 | }; |
| 71 | for (const [mode, scheme] of [["light", light()], ["dark", pinnedDark()]] as const) { |
| 72 | for (const [role, name] of Object.entries(inks[mode])) { |
| 73 | expect(scheme[role], `${mode}.${role}`).toBe(`var(--gpui-${mode}-${name})`); |
| 74 | expect(hex(scheme[role]), `${mode}.${role}`).toBe(gpui(mode, name)); |
| 75 | } |
| 76 | } |
| 77 | expect(hex(light().bg)).toBe("#f4f7fc"); |
| 78 | expect(hex(pinnedDark().bg)).toBe("#05070b"); |
| 79 | expect(CSS).not.toMatch(/--kit-(warm-paper|foam|mist|bioluminescence|ink|slate)/); |
| 80 | }); |
| 81 | |
| 82 | it("re-declares no role for the footer: it sits on the page's own ground", () => { |
| 83 | expect(CSS).not.toMatch(/\.stage\b/); |
| 84 | expect(CSS).not.toMatch(/\.site-footer\s*\{[^}]*--(bg|text|muted):/); |
| 85 | expect(CSS).not.toMatch(/--sea(-[\w-]+)?:/); |
| 86 | }); |
| 87 | |
| 88 | it("keeps every text ink at WCAG AA on every ground, in every scheme", () => { |
| 89 | for (const [name, scheme] of [["light", light()], ["dark", pinnedDark()]] as const) { |
| 90 | for (const ground of GROUNDS) { |
| 91 | for (const ink of TEXT_INKS) { |
| 92 | expect(contrast(hex(scheme[ink]), hex(scheme[ground])), `${name}: ${ink} on ${ground}`).toBeGreaterThanOrEqual(4.5); |
| 93 | } |
| 94 | } |
| 95 | // Control edges and state marks clear the 3:1 non-text floor. |
| 96 | for (const ground of ["bg", "panel"]) { |
| 97 | expect(contrast(hex(scheme["line-strong"]), hex(scheme[ground])), `${name}: line-strong on ${ground}`).toBeGreaterThanOrEqual(3); |
| 98 | } |
| 99 | expect(contrast(hex(scheme["on-accent"]), hex(scheme.accent)), `${name}: on-accent`).toBeGreaterThanOrEqual(4.5); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | it("puts white text on the logo gradient only where it clears 4.5:1", () => { |
| 104 | const fill = CSS.match(/--brand-fill:\s*linear-gradient\(([^;]+)\);/)?.[1] ?? ""; |
| 105 | const stops = [...fill.matchAll(/#[0-9a-f]{6}|var\(--brand-[\w-]+\)/gi)].map((m) => hex(m[0])); |
| 106 | expect(stops.length).toBeGreaterThanOrEqual(2); |
| 107 | for (const stop of stops) expect(contrast("#ffffff", stop), stop).toBeGreaterThanOrEqual(4.5); |
| 108 | expect(hex("var(--brand-deep)")).toBe("#0b48bb"); |
| 109 | expect(hex("var(--brand-light)")).toBe("#1e8fd8"); |
| 110 | }); |
| 111 | |
| 112 | it("keeps the home's text readable across the water, in both schemes", () => { |
| 113 | const tokens = readFileSync(new URL("../app/tokens.css", import.meta.url), "utf8"); |
| 114 | for (const [mode, scheme] of [["light", light()], ["dark", pinnedDark()]] as const) { |
| 115 | expect(scheme["home-sky"], mode).toContain(`var(--gpui-surface-light-${mode})`); |
| 116 | const ramp = tokens.match(new RegExp(`--gpui-water-${mode}:\\s*linear-gradient\\(([^;]+)\\);`))?.[1] ?? ""; |
| 117 | const stops = [...ramp.matchAll(/#[0-9a-f]{6}/gi)].map((m) => m[0].toLowerCase()); |
| 118 | expect(stops.length, mode).toBeGreaterThanOrEqual(3); |
| 119 | for (const stop of stops) { |
| 120 | for (const ink of ["text", "muted", "live"]) { |
| 121 | expect(contrast(hex(scheme[ink]), stop), `${mode}: ${ink} on ${stop}`).toBeGreaterThanOrEqual(4.5); |
| 122 | } |
| 123 | } |
| 124 | const band = [...CSS.matchAll(/--home-band: (#[0-9a-f]{6});/g)].map((m) => m[1])[mode === "light" ? 0 : 1]; |
| 125 | expect(stops, `${mode} band`).toContain(band); |
| 126 | } |
| 127 | }); |
| 128 | }); |
| 129 |