import { describe, expect, it } from "vitest"; import { FOCUS_RING_BACKGROUNDS, firstFontFamilyFailures, focusContrastFailures, hueMixFailures, unreadTokenFailures, } from "../../scripts/lib/design-system-rules.ts"; const css = (source: string) => ({ file: "probe.css", source }); describe("design system rules", () => { describe("UI-01 focus ring contrast", () => { const tokens = (focus: string) => new Map([ ...FOCUS_RING_BACKGROUNDS.map((surface) => [surface, "oklch(1 0 0)"] as const), ["--color-focus", focus], ]); it("fails a focus colour below 3:1 against a surface", () => { // The light focus ring before UI-01. expect(focusContrastFailures("light", tokens("oklch(0.72 0.16 225)"))).toContainEqual( expect.stringMatching(/^light --color-focus contrast 2\.\d\d:1 against --color-panel is below 3:1$/), ); }); it("passes a focus colour at 3:1 or more on every surface", () => { expect(focusContrastFailures("light", tokens("oklch(0.52 0.15 230)"))).toEqual([]); }); }); describe("UI-02 colour mixing space", () => { it("fails a hue-based mixing space", () => { expect(hueMixFailures(css("a { color: color-mix(in oklch, red, blue); }"))).toEqual([ "probe.css mixes colours in oklch; use oklab", ]); }); it("fails it in upper case, since CSS keywords ignore case", () => { expect(hueMixFailures(css("a { color: COLOR-MIX(in OKLCH, red, blue); }"))).toEqual([ "probe.css mixes colours in OKLCH; use oklab", ]); }); it("passes oklab, and old code left in a comment", () => { expect( hueMixFailures( css("/* was color-mix(in oklch, red, blue) */ a { color: color-mix(in oklab, red, blue); }"), ), ).toEqual([]); }); }); describe("UI-03 component token readers", () => { const tokens = ["--field-border", "--field-border-invalid"]; // 12-01: "var(--field-border" is also the start of // "var(--field-border-invalid)". it("does not count a longer token that starts with the same name", () => { expect( unreadTokenFailures(tokens, [css("a { border-color: var(--field-border-invalid); }")]), ).toEqual(["component token --field-border is defined but no style reads it"]); }); it("counts var(--x) and var(--x, fallback), with any case for var", () => { for (const source of [ "a { border-color: var(--field-border); }", "a { border: 1px solid VAR( --field-border , red); }", ]) { expect(unreadTokenFailures(["--field-border"], [css(source)])).toEqual([]); } }); it("does not count a name in another case, since custom properties are case-sensitive", () => { expect(unreadTokenFailures(["--field-border"], [css("a { color: var(--Field-Border); }")])) .toEqual(["component token --field-border is defined but no style reads it"]); }); it("does not count a read inside a comment", () => { expect(unreadTokenFailures(["--field-border"], [css("/* var(--field-border) */")])).toEqual([ "component token --field-border is defined but no style reads it", ]); }); }); describe("UI-05 first font family", () => { it("fails a named font that nothing loads", () => { expect(firstFontFamilyFailures("Inter, system-ui, sans-serif", [])).toEqual([ '--font-sans starts with "Inter", which is neither a generic family nor loaded by @font-face', ]); }); it("passes a generic family, or a family loaded by @font-face", () => { expect(firstFontFamilyFailures("system-ui, sans-serif", [])).toEqual([]); expect( firstFontFamilyFailures('"Brand Sans", sans-serif', [ css('@font-face { font-family: "Brand Sans"; src: url(brand.woff2); }'), ]), ).toEqual([]); }); }); });