import { describe, expect, it } from "vitest"; import { contrastRatio, parseOklch, readCustomProperties, resolveColor, } from "../../scripts/lib/oklch-contrast.ts"; const color = (value: string) => { const parsed = parseOklch(value); if (!parsed) throw new Error(`not an oklch value: ${value}`); return parsed; }; describe("oklch contrast", () => { it("gives 21:1 for white on black", () => { expect(contrastRatio(color("oklch(1 0 0)"), color("oklch(0 0 0)"))).toBeCloseTo(21, 1); }); // UI-01: the old light focus ring measured 2.33:1 on white in Chromium. it("matches the ratio the browser measured for the old focus ring", () => { expect( contrastRatio(color("oklch(0.72 0.16 225)"), color("oklch(1 0 0)")), ).toBeCloseTo(2.33, 1); }); it("reads percentages and a missing hue", () => { expect(parseOklch("oklch(50% 0.1 none)")).toEqual({ l: 0.5, c: 0.1, h: 0 }); expect(parseOklch("color-mix(in oklab, red, blue)")).toBeNull(); }); // 12-04: valid CSS the browser draws the same as the unitless form. it("reads a hue in deg and a negative hue, and refuses alpha", () => { expect(parseOklch("oklch(52% 0.15 230deg)")).toEqual({ l: 0.52, c: 0.15, h: 230 }); expect(parseOklch("oklch(0.5 0.1 -30)")).toEqual({ l: 0.5, c: 0.1, h: -30 }); expect(parseOklch("oklch(0.5 0.1 230 / 0.9)")).toBeNull(); }); it("follows var() chains and refuses values it cannot draw", () => { const properties = readCustomProperties(` --palette-white: oklch(1 0 0); --color-panel: var(--palette-white); --color-mixed: color-mix(in oklab, var(--palette-white) 50%, transparent); `); expect(resolveColor("--color-panel", properties)).toEqual({ l: 1, c: 0, h: 0 }); expect(resolveColor("--color-mixed", properties)).toBeNull(); expect(resolveColor("--color-missing", properties)).toBeNull(); }); });