返回 CodeWhale
design-grammar-contract.test.ts
根目录 / web / lib / design-grammar-contract.test.ts
1 import { existsSync, 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 // Radii, state roles and motion follow the GPUI client (DESIGN.md, set_theme,
7 // motion.rs): one radius grammar, one focus ring, spring-fitted easing, and a
8 // still pose under reduced motion.
9 const CSS = siteCss();
10 const TAILWIND = readFileSync(new URL("../tailwind.config.ts", import.meta.url), "utf8");
11
12 describe("design grammar contract", () => {
13 it("draws every radius from the 10/18/pill grammar", () => {
14 const defined = [...CSS.matchAll(/--radius-[\w-]+:\s*([^;]+);/g)].map((m) => resolveWhale(m[1].trim()));
15 expect(new Set(defined)).toEqual(new Set(["10px", "18px", "999px"]));
16 const used = [...CSS.matchAll(/border-radius:\s*([^;]+);/g)].map((m) => m[1].trim());
17 expect(used.length).toBeGreaterThan(0);
18 // A corner is a grammar token or square; per-corner shorthands combine them.
19 for (const value of used) {
20 expect(value).toMatch(/^(?:(?:var\(--radius-(control|surface|sheet|pill)\)|0)\s*){1,4}$/);
21 }
22 const scale = TAILWIND.match(/borderRadius:\s*\{([\s\S]*?)\}/)?.[1] ?? "";
23 expect(scale).not.toMatch(/\d+(px|rem)/);
24 });
25
26 it("has one focus ring and the set_theme selection", () => {
27 expect(CSS.match(/(?<![\w-])outline:/g)).toHaveLength(1);
28 expect(CSS).toContain(":focus-visible { outline: var(--gpui-focus-width) solid var(--ring); outline-offset: var(--gpui-focus-offset); }");
29 expect(resolveWhale("var(--gpui-focus-width)")).toBe("2px");
30 expect(resolveWhale("var(--gpui-focus-offset)")).toBe("3px");
31 expect(CSS).not.toMatch(/outline:\s*none/);
32 expect(CSS).toMatch(/::selection \{ background: var\(--selection\); \}/);
33 });
34
35 it("paints every primary fill with the logo gradient and white text", () => {
36 for (const cls of ["btn-primary", "paper-install-cta"]) {
37 const rest = CSS.match(new RegExp(`\\.${cls}[^{]*\\{([^}]*)\\}`))?.[1] ?? "";
38 const hover = CSS.match(new RegExp(`\\.${cls}:hover[^{]*\\{([^}]*)\\}`))?.[1] ?? "";
39 expect(rest, cls).toMatch(/background: var\(--brand-fill\)/);
40 expect(rest, cls).toMatch(/color: var\(--on-brand\)/);
41 expect(hover, cls).toMatch(/background: var\(--brand-fill-hover\)/);
42 }
43 });
44
45 it("keeps every keyframed animation behind a motion preference", () => {
46 // Strip the blocks that only run when motion is allowed; nothing left
47 // may start an animation.
48 let rest = CSS;
49 for (;;) {
50 const at = rest.indexOf("@media (prefers-reduced-motion: no-preference)");
51 if (at < 0) break;
52 let depth = 0;
53 let end = rest.indexOf("{", at);
54 for (let i = end; i < rest.length; i++) {
55 if (rest[i] === "{") depth++;
56 else if (rest[i] === "}" && --depth === 0) { end = i; break; }
57 }
58 rest = rest.slice(0, at) + rest.slice(end + 1);
59 }
60 const starts = [...rest.matchAll(/animation:\s*([^;]+);/g)].map((m) => m[1].trim());
61 for (const value of starts) expect(value).toBe("none");
62 });
63
64 it("times motion with the spring tokens and stills it under reduced motion", () => {
65 expect(CSS).not.toMatch(/\d+ms ease[,;]/);
66 expect(CSS).not.toMatch(/cubic-bezier\(0\.25, 0\.46/);
67 expect(CSS).toMatch(/--ease-spring: var\(--gpui-easing-panel\);/);
68 expect(resolveWhale("var(--gpui-easing-panel)")).toMatch(/^cubic-bezier\(/);
69 expect(CSS).toMatch(
70 /@media \(prefers-reduced-motion: reduce\) \{\s*:root \{\s*--dur-state: 0ms;\s*--dur-spring: 0ms;/,
71 );
72 expect(TAILWIND).toMatch(/transitionDuration: \{ DEFAULT: "var\(--dur-state\)" \}/);
73 expect(TAILWIND).toMatch(/transitionTimingFunction: \{ DEFAULT: "var\(--ease-spring\)" \}/);
74 expect(CSS).not.toMatch(/caustic/);
75 });
76
77 it("keeps no unused presence shapes", () => {
78 expect(existsSync(new URL("../components/presence.tsx", import.meta.url))).toBe(false);
79 });
80 });
81
81 lines TYPESCRIPT