import { readFileSync, readdirSync } from "node:fs"; import postcss from "postcss"; import { describe, expect, it } from "vitest"; const here = new URL("./", import.meta.url); const read = (rel: string) => readFileSync(new URL(rel, here), "utf8"); const repo = (rel: string) => readFileSync(new URL(`../../../${rel}`, import.meta.url), "utf8"); /* The names a theme must define (documented in docs/THEMES.md). Add a name here when a component starts reading a new seam. */ /** Colour roles: the library's semantic names, as bare HSL triplets, defined for light AND dark. */ const COLOUR_NAMES = [ "background", "foreground", "card", "card-foreground", "popover", "popover-foreground", "primary", "primary-foreground", "secondary", "secondary-foreground", "muted", "muted-foreground", "accent", "accent-foreground", "destructive", "destructive-foreground", "success", "success-foreground", "warning", "warning-foreground", "info", "info-foreground", "border", "input", "ring", ] as const; /** Shape variables every theme sets (shape does not change with the colour scheme). */ const SHAPE_NAMES = [ // control heights "--ui-control-h-xs", "--ui-control-h-sm", "--ui-control-h-md", "--ui-control-h-lg", // radius steps "--ui-radius-sm", "--ui-radius-md", "--ui-radius-lg", "--ui-radius-xl", "--ui-radius-2xl", // type sizes "--ui-text-body", "--ui-text-control", // edge model: `border` (real border) or `ring` (inset ring); --ui-edge-width is the border width a // component that draws a border uses (0px under a ring theme) "--ui-edge", "--ui-edge-width", "--ui-ring-w", // Badge / Tag "--ui-badge-h", "--ui-badge-px", "--ui-badge-py", "--ui-badge-radius", "--ui-badge-text", "--ui-badge-leading", "--ui-badge-weight", "--ui-badge-font", // Skeleton "--ui-skeleton-radius", // Button ...["xs", "sm", "md", "lg"].flatMap((z) => [`--ui-btn-h-${z}`, `--ui-btn-px-${z}`, `--ui-btn-text-${z}`]), ...["xs", "sm", "md"].map((z) => `--ui-btn-leading-${z}`), "--ui-btn-radius", "--ui-btn-radius-xs", "--ui-btn-radius-lg", "--ui-btn-gap", "--ui-btn-gap-xs", // Fields (Input, Textarea, Select trigger) ...["sm", "md", "lg"].flatMap((z) => [`--ui-field-h-${z}`, `--ui-field-px-${z}`, `--ui-field-text-${z}`, `--ui-field-leading-${z}`]), "--ui-field-radius", "--ui-field-shadow", ] as const; /** Optional: a theme may leave these unset and the component keeps its default. */ const OPTIONAL_NAMES = [ "--ui-badge-neutral-fg", "--ui-badge-neutral-edge", // pixel parity: Button, fields, Select trigger (docs/THEMES.md) "--ui-btn-edge-danger", "--ui-btn-edge-solid", "--ui-btn-face", "--ui-btn-ghost-fg", "--ui-btn-ghost-hover-fg", "--ui-btn-hover-danger", "--ui-btn-hover-primary", "--ui-btn-hover-quiet", "--ui-btn-icon", "--ui-btn-icon-xs", "--ui-btn-px-xs-quiet", "--ui-btn-tracking-lg", "--ui-btn-weight-lg", "--ui-btn-weight-solid", "--ui-btn-weight-solid-lg", "--ui-field-disabled-bg", "--ui-field-disabled-fg", "--ui-field-disabled-opacity", "--ui-field-face", "--ui-field-focus-c", "--ui-field-focus-edge", "--ui-field-focus-offset", "--ui-field-focus-w", "--ui-field-invalid-edge", "--ui-field-placeholder", "--ui-focus-offset", "--ui-focus-outline", "--ui-focus-outline-offset", "--ui-focus-w", "--ui-input-px-md", "--ui-select-chevron", "--ui-select-chevron-opacity", "--ui-select-gap", "--ui-select-pl-md", "--ui-select-pr-md", "--ui-textarea-min-h-md", "--ui-textarea-py-md", ] as const; /* Step 5 (card-empty-alert): ConsoleCard / ConsoleEmptyState / ConsoleCallout seams. Optional (a theme may leave one unset and the component keeps its fallback). SHAPE ones are in the shape-only files too; COLOUR ones carry colour values (HSL triplets/hex of the full file's scheme), so they live in the full files only. */ const CEA_SHAPE_NAMES = [ "--ui-callout-actions-basis", "--ui-callout-actions-ml", "--ui-callout-border-w", "--ui-callout-gap", "--ui-callout-icon", "--ui-card-title-display", "--ui-card-header-leading", "--ui-card-desc-leading", "--ui-card-footer-leading", "--ui-card-head-overflow", "--ui-card-head-ellipsis", "--ui-card-head-ws", "--ui-callout-dashed-radius", "--ui-callout-dashed-leading", "--ui-callout-icon-col", "--ui-callout-icon-mt-titled", "--ui-callout-gap-plain", "--ui-empty-caption-leading", "--ui-empty-eyebrow-leading", "--ui-callout-px", "--ui-callout-py", "--ui-callout-radius", "--ui-callout-text", "--ui-callout-title-text", "--ui-callout-title-weight", "--ui-card-body-px", "--ui-card-border-w", "--ui-card-desc-display", "--ui-card-desc-ml", "--ui-card-desc-text", "--ui-card-footer-px", "--ui-card-footer-py", "--ui-card-footer-rule", "--ui-card-footer-text", "--ui-card-gap", "--ui-card-header-align", "--ui-card-header-px", "--ui-card-header-py", "--ui-card-header-rule", "--ui-card-header-text", "--ui-card-header-weight", "--ui-card-py", "--ui-card-radius", "--ui-card-ring-w", "--ui-empty-caption-text", "--ui-empty-card-actions-mt", "--ui-empty-card-desc-mt", "--ui-empty-card-desc-text", "--ui-empty-card-glyph", "--ui-empty-card-px", "--ui-empty-card-py", "--ui-empty-card-tile", "--ui-empty-card-tile-bg", "--ui-empty-card-tile-gap", "--ui-empty-card-tile-radius", "--ui-empty-card-title-text", "--ui-empty-chart-desc-text", "--ui-empty-chart-px", "--ui-empty-chart-py", "--ui-empty-chart-title-text", "--ui-empty-frame-radius", "--ui-empty-frame-style", "--ui-empty-frame-w", "--ui-empty-page-actions-mt", "--ui-empty-page-desc-mt", "--ui-empty-page-desc-text", "--ui-empty-page-glyph", "--ui-empty-page-px", "--ui-empty-page-py", "--ui-empty-page-tile", "--ui-empty-page-tile-gap", "--ui-empty-page-tile-radius", "--ui-empty-page-title-text", "--ui-empty-row-desc-text", "--ui-empty-row-glyph", "--ui-empty-row-px", "--ui-empty-row-py", "--ui-empty-row-tile", "--ui-empty-row-tile-radius", "--ui-empty-row-title-text", "--ui-empty-title-weight", ] as const; const CEA_COLOUR_NAMES = [ "--ui-callout-body-fg", "--ui-callout-destructive-bg", "--ui-callout-destructive-border", "--ui-callout-destructive-fg", "--ui-callout-info-bg", "--ui-callout-info-border", "--ui-callout-info-fg", "--ui-callout-neutral-bg", "--ui-callout-neutral-border", "--ui-callout-neutral-fg", "--ui-callout-ok-bg", "--ui-callout-ok-border", "--ui-callout-ok-fg", "--ui-callout-warn-bg", "--ui-callout-warn-border", "--ui-callout-warn-fg", "--ui-callout-warn-fill-bg", "--ui-callout-warn-fill-fg", "--ui-empty-card-tile-fg", "--ui-empty-page-tile-fg", "--ui-empty-row-tile-fg", "--ui-ink-2", "--ui-ink-3", "--ui-line-soft", ] as const; /* type-roles (Text, Heading, ui-text-* classes): optional. Sizes are shape (in the shape-only files too); the four text colours carry colour, so they live in the full files only. */ const TYPE_ROLES = ["numeric", "title-page", "title-section", "title-card", "body", "body-sm", "overline"] as const; const TYPE_ROLE_NAMES = TYPE_ROLES.flatMap((r) => ["size", "line", "weight", "tracking"].map((p) => `--ui-text-${r}-${p}`)); const TEXT_COLOUR_NAMES = ["--ui-text-primary", "--ui-text-secondary", "--ui-text-tertiary", "--ui-text-disabled"]; /** Optional variables of the status-chip-tones block (Badge/Tag tones, StatusChip, MeterBar, Skeleton `theme`). */ const TONES_BLOCK = /^--ui-(tone-|status-|chip-|meter-|dot-size$|outline-width$|edge-ring-width$|skeleton-(animation|image|size)$)/; const isOptional = (k: string) => (OPTIONAL_NAMES as readonly string[]).includes(k) || TONES_BLOCK.test(k); type Blocks = { light: Record; dark: Record }; /** Split a theme file into its light rule and its dark rule (the one whose selector mentions dark). */ function blocks(css: string): Blocks { const out: Blocks = { light: {}, dark: {} }; postcss.parse(css).walkRules((rule) => { const target = rule.selector.includes("dark") ? out.dark : out.light; rule.walkDecls(/^--/, (d) => { target[d.prop] = d.value; }); }); return out; } const themes = { v2: blocks(read("v2.css")), fe: blocks(read("fe.css")) }; const shapeOnly = { v2: blocks(read("v2.shape.css")), fe: blocks(read("fe.shape.css")) }; /* ------------------------------------------------------------------ 1. every name is defined */ describe.each(["v2", "fe"] as const)("theme %s defines every required name", (name) => { it("light: all colour roles and all shape variables", () => { for (const c of COLOUR_NAMES) expect(themes[name].light[`--${c}`], `--${c}`).toBeTruthy(); for (const v of SHAPE_NAMES) expect(themes[name].light[v], v).toBeTruthy(); }); it("dark: all colour roles", () => { for (const c of COLOUR_NAMES) expect(themes[name].dark[`--${c}`], `--${c}`).toBeTruthy(); }); it("colour values are bare HSL triplets", () => { for (const scheme of ["light", "dark"] as const) for (const c of COLOUR_NAMES) expect(themes[name][scheme][`--${c}`]).toMatch(/^\d+(\.\d+)? \d+(\.\d+)?% \d+(\.\d+)?%$/); }); it("defines nothing outside the documented names", () => { const known = new Set([...COLOUR_NAMES.map((c) => `--${c}`), ...SHAPE_NAMES, ...OPTIONAL_NAMES, ...CEA_SHAPE_NAMES, ...CEA_COLOUR_NAMES, ...TYPE_ROLE_NAMES, ...TEXT_COLOUR_NAMES]); for (const scheme of ["light", "dark"] as const) for (const prop of Object.keys(themes[name][scheme])) expect(known.has(prop) || isOptional(prop), prop).toBe(true); }); it("the shape-only file is exactly the full file's shape variables", () => { const shapeNames = new Set([...SHAPE_NAMES, ...OPTIONAL_NAMES, ...CEA_SHAPE_NAMES, ...TYPE_ROLE_NAMES]); const pick = (o: Record) => Object.fromEntries(Object.entries(o).filter(([k]) => shapeNames.has(k) || isOptional(k))); expect(shapeOnly[name].light).toEqual(pick(themes[name].light)); expect(shapeOnly[name].dark).toEqual(pick(themes[name].dark)); for (const c of COLOUR_NAMES) expect(shapeOnly[name].light[`--${c}`]).toBeUndefined(); }); }); describe("theme shape values (owner's measurements)", () => { it("v2: controls 28/24/22, radius 5/6/10/12, body 13 / control 12, border edge", () => { const l = themes.v2.light; expect([l["--ui-control-h-md"], l["--ui-control-h-sm"], l["--ui-control-h-xs"]]).toEqual(["28px", "24px", "22px"]); expect([l["--ui-radius-sm"], l["--ui-radius-md"], l["--ui-radius-lg"], l["--ui-radius-xl"]]).toEqual(["5px", "6px", "10px", "12px"]); expect([l["--ui-text-body"], l["--ui-text-control"]]).toEqual(["13px", "12px"]); expect([l["--ui-edge"], l["--ui-edge-width"]]).toEqual(["border", "1px"]); }); it("fe: controls 36/32/40, radius 6/8/10/12, body 14, ring edge", () => { const l = themes.fe.light; expect([l["--ui-control-h-md"], l["--ui-control-h-sm"], l["--ui-control-h-lg"]]).toEqual(["36px", "32px", "40px"]); expect([l["--ui-radius-sm"], l["--ui-radius-md"], l["--ui-radius-lg"], l["--ui-radius-xl"]]).toEqual(["6px", "8px", "10px", "12px"]); expect([l["--ui-text-body"], l["--ui-text-control"]]).toEqual(["14px", "14px"]); expect([l["--ui-edge"], l["--ui-edge-width"]]).toEqual(["ring", "0px"]); }); }); /* Type roles. v2: the table v2's src/design/type-scale.ts `typeRole` exports (typography ruling, 2026-11-15); v2's src/design/type-scale.test.ts pins the same table, so change both together. fe: aisquare-fe src/styles/tokens.css (Type, TY1-TY13): page-title, section, heading, body, small, mono-label. */ const V2_TYPE_ROLE: Record = { numeric: ["24px", "32px", "600", "0"], "title-page": ["20px", "28px", "600", "-0.01em"], "title-section": ["16px", "24px", "600", "0"], "title-card": ["14px", "20px", "600", "0"], body: ["13px", "20px", "400", "0"], "body-sm": ["12px", "16px", "400", "0"], overline: ["11px", "16px", "600", "0.06em"], }; const FE_TYPE_ROLE: Record = { numeric: ["24px", "32px", "600", "-0.02em"], "title-page": ["24px", "32px", "600", "-0.02em"], "title-section": ["18px", "24px", "600", "-0.01em"], "title-card": ["16px", "24px", "600", "0"], body: ["14px", "20px", "400", "0"], "body-sm": ["13px", "18px", "400", "0"], overline: ["11px", "16px", "500", "0.08em"], }; describe("type roles", () => { const role = (l: Record, r: string) => ["size", "line", "weight", "tracking"].map((p) => l[`--ui-text-${r}-${p}`]); it("v2: the typography ruling's seven roles, equal to v2's typeRole", () => { for (const r of TYPE_ROLES) expect(role(themes.v2.light, r), r).toEqual(V2_TYPE_ROLE[r]); }); it("v2: nothing under 12px but the overline", () => { for (const r of TYPE_ROLES) if (r !== "overline") expect(parseFloat(themes.v2.light[`--ui-text-${r}-size`]!), r).toBeGreaterThanOrEqual(12); }); it("fe: aisquare-fe's named styles", () => { for (const r of TYPE_ROLES) expect(role(themes.fe.light, r), r).toEqual(FE_TYPE_ROLE[r]); }); it("v2: the four text colours are ink, ink-2, ink-3 and ink-disabled, in both schemes", () => { const ink = (b: Record) => TEXT_COLOUR_NAMES.map((n) => b[n]); expect(ink(themes.v2.light)).toEqual(["#0F172A", "#44506A", "#5C6780", "#A7B1C0"]); expect(ink(themes.v2.dark)).toEqual(["#E9EDF4", "#B3BDCE", "#98A3B6", "#5E6878"]); expect(themes.v2.light["--ui-text-primary"]).toBe(V2_LIGHT.ink); expect(themes.v2.dark["--ui-text-tertiary"]).toBe(V2_DARK["ink-3"]); }); it("text colours stay out of the shape-only files", () => { for (const n of TEXT_COLOUR_NAMES) { expect(shapeOnly.v2.light[n], n).toBeUndefined(); expect(shapeOnly.fe.light[n], n).toBeUndefined(); } }); }); /* ------------------------------------------------------------------ 2. v2 colours = v2's tokens */ /* Snapshot of the v2 colours the library names map to. Source: aisquare-explainability-v2 src/design/tokens.ts (`color`) and src/design/dark-palette.ts (`darkColor`) at origin/develop commit 22cd978c43f6405b33b9c929f662a26932b3a3e0. The role -> token mapping is v2 PR #781's src/design/library-aliases.ts (branch feat-design-system-integration). When v2's tokens change, update this snapshot and the theme together. */ const V2_LIGHT: Record = { paper: "#F5F7FB", surface: "#FFFFFF", sunken: "#EEF2F7", line: "#D8DEE9", "line-soft": "#E7ECF3", ink: "#0F172A", "ink-2": "#44506A", "ink-3": "#5C6780", accent: "#0B6BE0", "on-accent": "#FFFFFF", "ok-fg": "#0F7A4A", "warn-fg": "#8A5A00", "danger-fg": "#C2262E", "info-fg": "#1F5FBF", }; const V2_DARK: Record = { paper: "#16181D", surface: "#1C1F26", sunken: "#2A2E38", line: "#363C48", "line-soft": "#2B2F39", ink: "#E9EDF4", "ink-2": "#B3BDCE", "ink-3": "#98A3B6", accent: "#3D8BFD", "on-accent": "#06142B", "ok-fg": "#5FD69D", "warn-fg": "#F2C455", "danger-fg": "#F98A91", "info-fg": "#79ACF1", }; const V2_ROLE_TO_TOKEN: Record = { background: "paper", foreground: "ink", card: "surface", "card-foreground": "ink", popover: "surface", "popover-foreground": "ink", primary: "accent", "primary-foreground": "on-accent", accent: "accent", "accent-foreground": "on-accent", secondary: "sunken", "secondary-foreground": "ink", muted: "sunken", "muted-foreground": "ink-3", destructive: "danger-fg", "destructive-foreground": "on-accent", success: "ok-fg", "success-foreground": "on-accent", warning: "warn-fg", "warning-foreground": "on-accent", info: "info-fg", "info-foreground": "on-accent", border: "line", input: "line", ring: "accent", }; /** "H S% L%" -> "#RRGGBB" (the inverse of what the theme writes). */ function tripletToHex(t: string): string { const [h, s, l] = t.split(" ").map(parseFloat) as [number, number, number]; const S = s / 100; const L = l / 100; const a = S * Math.min(L, 1 - L); const f = (n: number) => { const k = (n + h / 30) % 12; return L - a * Math.max(-1, Math.min(k - 3, Math.min(9 - k, 1))); }; return `#${[f(0), f(8), f(4)].map((x) => Math.round(x * 255).toString(16).padStart(2, "0")).join("")}`.toUpperCase(); } describe("v2 theme colours equal v2's tokens", () => { for (const [scheme, palette] of [["light", V2_LIGHT], ["dark", V2_DARK]] as const) { it(`${scheme}: every role is exactly the mapped v2 hex`, () => { for (const role of COLOUR_NAMES) { const token = V2_ROLE_TO_TOKEN[role]!; expect(tripletToHex(themes.v2[scheme][`--${role}`]!), `--${role} (${token})`).toBe(palette[token]); } }); it(`${scheme}: the Badge/Tag extras are v2's ink-2 and line-soft`, () => { const unwrap = (v: string) => v.replace(/^hsl\((.*)\)$/, "$1"); expect(tripletToHex(unwrap(themes.v2[scheme]["--ui-badge-neutral-fg"]!))).toBe(palette["ink-2"]); expect(tripletToHex(unwrap(themes.v2[scheme]["--ui-badge-neutral-edge"]!))).toBe(palette["line-soft"]); }); } }); describe("fe theme colours equal fe's tokens (aisquare-fe src/styles/tokens.css)", () => { const FE_LIGHT: Record = { background: "#EFF1F3", foreground: "#171717", primary: "#192532", "primary-foreground": "#FFFFFF", secondary: "#EFF0F2", "secondary-foreground": "#333333", muted: "#EFF0F2", "muted-foreground": "#4D4D4D", accent: "#EBECEE", destructive: "#B4321F", border: "#DDE3EB", input: "#B5C3D3", ring: "#171717", }; const FE_DARK: Record = { background: "#141516", foreground: "#EDEDED", primary: "#E5E8EC", "primary-foreground": "#141516", secondary: "#1F2021", "secondary-foreground": "#CCCCCC", muted: "#1F2021", "muted-foreground": "#A1A1A1", accent: "#2A2B2D", destructive: "#FF6B6B", ring: "#EDEDED", // border and input are rgb(198 207 235 / 12% | 24%) in fe: stored pre-blended over --card #171819 border: "#2C2E32", input: "#41444B", }; for (const [scheme, palette] of [["light", FE_LIGHT], ["dark", FE_DARK]] as const) it(`${scheme}: checked roles are exactly fe's hex`, () => { for (const [role, hex] of Object.entries(palette)) expect(tripletToHex(themes.fe[scheme][`--${role}`]!), `--${role}`).toBe(hex); }); }); /* ------------------------------------------------------------------ 3. default output unchanged */ describe("existing consumers see no change by default", () => { const pkg = JSON.parse(repo("package.json")) as { exports: Record }; it("every export that existed before the themes is still there, unchanged", () => { // `exports` as of origin/main ded77e1, before this change (the two tailwind presets are // covered by tailwind.preset.split.test.ts) const before: Record = { ".": { types: "./dist/index.d.ts", import: "./dist/index.js" }, "./stock": { types: "./dist/stock.d.ts", import: "./dist/stock.js" }, "./chat": { types: "./dist/chat.d.ts", import: "./dist/chat.js" }, "./icons": { types: "./dist/icons.d.ts", import: "./dist/icons.js" }, "./tokens/tokens.template.css": "./src/tokens/tokens.template.css", "./tokens/foundations.css": "./src/tokens/foundations.css", "./tokens/fonts.css": "./src/tokens/fonts.css", "./tokens/shadcn-extras.css": "./src/tokens/shadcn-extras.css", "./onyx-hex.css": "./src/tokens/onyx-hex.css", "./package.json": "./package.json", }; for (const [k, v] of Object.entries(before)) expect(pkg.exports[k], k).toEqual(v); }); it("only the four theme files were added to exports", () => { const added = Object.keys(pkg.exports).filter((k) => k.startsWith("./themes/")); expect(added.sort()).toEqual(["./themes/fe.css", "./themes/fe.shape.css", "./themes/v2.css", "./themes/v2.shape.css"]); }); it("nothing existing loads a theme: no token file, entry or preset mentions one", () => { const tokenDir = new URL("../", import.meta.url); for (const f of readdirSync(tokenDir).filter((n) => n.endsWith(".css"))) { const css = readFileSync(new URL(f, tokenDir), "utf8"); expect(css, f).not.toMatch(/data-ui-theme|--ui-|themes\//); } for (const f of ["src/index.ts", "src/stock.ts", "src/chat.ts", "tailwind.preset.ts", "tailwind.tokens.preset.ts"]) expect(repo(f), f).not.toMatch(/themes\//); }); it("themes only act under [data-ui-theme]: every selector is scoped", () => { for (const f of ["v2.css", "fe.css", "v2.shape.css", "fe.shape.css"]) postcss.parse(read(f)).walkRules((r) => { for (const sel of r.selectors) expect(sel, `${f}: ${sel}`).toContain("[data-ui-theme="); }); }); });