diff --git a/tests/unit/design-system-rules.test.ts b/tests/unit/design-system-rules.test.ts index a907e4c8..27e966b7 100644 --- a/tests/unit/design-system-rules.test.ts +++ b/tests/unit/design-system-rules.test.ts @@ -1,3 +1,8 @@ +import { spawnSync } from "node:child_process"; +import { cp, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + import { describe, expect, it } from "vitest"; import { @@ -100,4 +105,53 @@ describe("design system rules", () => { ).toEqual([]); }); }); + + // 12-02: the cases above test the rule functions. This one tests that + // check:design-system still calls each of them on the token files and + // styles/**: drop a call from the script and it fails here, while the + // repository CSS keeps passing the gate. + it("check-design-system.ts reports one violation per rule in a copy of the CSS", async () => { + const root = await mkdtemp(path.join(tmpdir(), "design-system-rules-")); + try { + for (const folder of ["src/presentation/design-system/tokens", "src/presentation/styles"]) { + await cp(folder, path.join(root, folder), { recursive: true }); + } + const edit = async (file: string, from: string | RegExp, to: string) => { + const target = path.join(root, file); + const source = await readFile(target, "utf8"); + const edited = source.replace(from, to); + if (edited === source) throw new Error(`${String(from)} not found in ${file}`); + await writeFile(target, edited); + }; + const semantic = "src/presentation/design-system/tokens/semantic.css"; + await edit(semantic, /--color-focus: [^;]+;/, "--color-focus: oklch(0.72 0.16 225);"); + await edit(semantic, "--font-sans: ", "--font-sans: Inter, "); + await edit("src/presentation/styles/theme/primitives.css", "var(--drawer-width)", "20rem"); + await writeFile( + path.join(root, "src/presentation/styles/theme/probe.css"), + ".probe { color: color-mix(in oklch, red, blue); }\n", + ); + + const result = spawnSync(process.execPath, [path.resolve("scripts/check-design-system.ts")], { + cwd: root, + encoding: "utf8", + }); + expect(result.status, result.stderr).toBe(1); + const { failures } = JSON.parse( + await readFile(path.join(root, "artifacts/quality/design-system.json"), "utf8"), + ) as { failures: string[] }; + const rules = [ + /^light --color-focus contrast \d\.\d\d:1 against --color-[a-z-]+ is below 3:1$/, + /^src\/presentation\/styles\/theme\/probe\.css mixes colours in oklch; use oklab$/, + /^component token --drawer-width is defined but no style reads it$/, + /^--font-sans starts with "Inter", which is neither a generic family nor loaded by @font-face$/, + ]; + expect(failures.filter((failure) => !rules.some((rule) => rule.test(failure)))).toEqual([]); + for (const rule of rules) { + expect(failures, String(rule)).toContainEqual(expect.stringMatching(rule)); + } + } finally { + await rm(root, { recursive: true, force: true }); + } + }); });