import { readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; // react-remove-scroll (Radix Select/Dialog/Sheet/DropdownMenu) adds `margin-right: ` // to body while scroll is locked; html already reserves that gutter (scrollbar-gutter: stable), so // without this override the page narrows by the scrollbar width every time an overlay opens. describe("globals.css", () => { it("keeps the reserved gutter and cancels scroll-lock's body margin", () => { const css = readFileSync(join(__dirname, "globals.css"), "utf8"); expect(css).toMatch(/html\s*\{[^}]*scrollbar-gutter:\s*stable/); expect(css).toMatch(/html body\[data-scroll-locked\]\s*\{[^}]*margin-right:\s*0\s*!important/); }); it("uses one type family, the Inter accent blue and the 0.5rem radius", () => { const css = readFileSync(join(__dirname, "globals.css"), "utf8"); const block = (sel: string) => css.match(new RegExp(`${sel}\\s*\\{([\\s\\S]*?)\\n\\}`))?.[1] ?? ""; expect(css).not.toMatch(/dm-sans|DM Sans/i); expect(block(":root")).toMatch(/--primary:\s*#2563eb/i); expect(block("\\.dark")).toMatch(/--primary:\s*#60a5fa/i); expect(block(":root")).toMatch(/--radius:\s*0\.5rem/); expect(block(":root")).toMatch(/--table-head:\s*#f1f5f9/i); expect(block("\\.dark")).toMatch(/--table-head:\s*#1e293b/i); expect(block(":root")).toMatch(/--muted-foreground:\s*#475569/i); expect(css).not.toMatch(/dusty|gold|brown/i); }); // WCAG 2.x: 4.5:1 for text, 3:1 for the boundary of a form control (1.4.11). it.each([":root", "\\.dark"])("meets WCAG contrast for text and control borders in %s", (sel) => { const css = readFileSync(join(__dirname, "globals.css"), "utf8"); const body = css.match(new RegExp(`${sel}\\s*\\{([\\s\\S]*?)\\n\\}`))?.[1] ?? ""; const tok = (name: string) => body.match(new RegExp(`--${name}:\\s*(#[0-9a-f]{6})`, "i"))?.[1] ?? ""; const lum = (hex: string) => { const [r, g, b] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255).map((v) => (v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4)); return 0.2126 * r + 0.7152 * g + 0.0722 * b; }; const ratio = (a: string, b: string) => { const [x, y] = [lum(tok(a)), lum(tok(b))]; return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05); }; const text: [string, string][] = [ ["foreground", "background"], ["foreground", "card"], ["muted-foreground", "background"], ["muted-foreground", "card"], ["muted-foreground", "muted"], ["primary-foreground", "primary"], ["destructive-foreground", "destructive"], ["accent-foreground", "accent"], ["table-head-foreground", "table-head"], ]; for (const [fg, bg] of text) expect(ratio(fg, bg), `${fg} on ${bg}`).toBeGreaterThanOrEqual(4.5); for (const bg of ["background", "card"]) expect(ratio("input", bg), `input on ${bg}`).toBeGreaterThanOrEqual(3); }); });