Files
Ekklesia/packages/ui/tests/tokens.test.ts
ekklesiaandClaude Opus 5.5 d42b64a093
CI / verify (pull_request) Successful in 1m10s
CI / infrastructure-smoke (pull_request) Successful in 34s
CI / package-release (pull_request) Successful in 52s
CI / migration-smoke (pull_request) Successful in 2m20s
CI / image-build (pull_request) Successful in 1m5s
CI / deploy-smoke (same-origin) (pull_request) Successful in 50s
CI / package-publication (pull_request) Successful in 58s
CI / deploy-smoke (split) (pull_request) Successful in 2m38s
feat(theme): Light/Dark/System, reduced motion, and one radius scale
The theme foundation DESIGN.md requires (ADR 0060):

- A design token may carry a `dark` value and a `reduced` value. The
  token stylesheet selects them in CSS alone: dark under
  prefers-color-scheme unless the host chose light, or when it chose
  dark; motion durations zero under prefers-reduced-motion unless it
  allowed motion, or when it chose reduce. `color-scheme` travels with
  the tokens. System is live, an explicit choice wins, and every module
  root follows through custom-property inheritance, with no module
  contract change.
- @ekklesia/design-tokens adds a small appearance API: system/light/dark
  and system/reduce/allow, parsers for untrusted stored values,
  resolvers, and a controller that applies the attributes immediately.
  The shell applies it before React renders. That is the hook the
  account preferences work will feed; nothing is persisted. The
  Dashboard page declares `color-scheme: light dark` so a dark system
  never flashes white. Displays stay light.
- A designed dark theme: teal-tinted charcoal, never pure black or
  white, the same teal accent.
- WCAG AA contrast is tested for 41 element pairings in both schemes.
  Two light pairings failed and are fixed visibly: text.muted darkens,
  and form controls get a new border.control (3:1).
  background.placeholder keeps skeletons visible in dark.
- Motion tokens (duration-short, duration-ambient) replace literal
  durations and every per-element reduced-motion rule. radius.scale
  multiplies sm/md/lg, unchanged at its default of 1.
- The sign-in card and module loading/error text move from literal
  colours to tokens. Guards keep raw colours and literal radii out of
  Dashboard product source and @ekklesia/ui.
- Fixes ConfirmationDialog's phrase input, which used a class that
  does not exist (`ek-input`) and so was unstyled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 20:00:39 -05:00

122 lines
5.1 KiB
TypeScript

import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { ekklesiaDesignTokens } from "@ekklesia/design-tokens";
import { describe, expect, it } from "vitest";
import {
EKKLESIA_UI_STYLE_ATTRIBUTE,
EKKLESIA_UI_STYLESHEET,
installEkklesiaUiStyles,
} from "../src/index.js";
const packageDirectory = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
async function sourceFiles(): Promise<readonly string[]> {
const directory = path.join(packageDirectory, "src");
return (await readdir(directory))
.filter((entry) => entry.endsWith(".ts") || entry.endsWith(".tsx"))
.map((entry) => path.join(directory, entry));
}
/** Any literal that would be a theme decision rather than a layout one. */
const THEME_LITERAL_PATTERN =
/#[0-9a-f]{3,8}\b|\brgba?\s*\(|\bhsla?\s*\(|\boklch\s*\(|\bsystem-ui\b|\bsans-serif\b/giu;
describe("the shared elements consume design tokens, never hardcoded theme values", () => {
it("expresses every theme value in the stylesheet as a token reference", () => {
expect(EKKLESIA_UI_STYLESHEET.match(THEME_LITERAL_PATTERN)).toBeNull();
expect(EKKLESIA_UI_STYLESHEET).toContain("var(--ekklesia-");
});
it("references only registered tokens", () => {
const referenced = [...EKKLESIA_UI_STYLESHEET.matchAll(/var\((--ekklesia-[a-z0-9-]+)\)/gu)].map(
([, variable]) => String(variable),
);
const registered = new Set(ekklesiaDesignTokens.list().map((token) => token.cssVariable));
expect(referenced.length).toBeGreaterThan(0);
expect([...new Set(referenced)].filter((variable) => !registered.has(variable))).toEqual([]);
});
it("never defines a global Ekklesia theme variable of its own", () => {
// Referencing `--ekklesia-*` is the contract; *declaring* one would invent a global theme
// variable outside the registry, which is precisely what the design-token contract forbids.
expect(EKKLESIA_UI_STYLESHEET).not.toMatch(/--ekklesia-[a-z0-9-]+\s*:/u);
});
it("hardcodes no theme value in component source either", async () => {
for (const file of await sourceFiles()) {
if (path.basename(file) === "styles.ts") {
continue;
}
expect(await readFile(file, "utf8")).not.toMatch(THEME_LITERAL_PATTERN);
}
});
it("never names a Node built-in or reads the environment", async () => {
for (const file of await sourceFiles()) {
const source = await readFile(file, "utf8");
expect(source).not.toContain('"node:');
expect(source).not.toContain("process.env");
}
});
it("depends only on the design tokens and React", async () => {
const manifest = JSON.parse(
await readFile(path.join(packageDirectory, "package.json"), "utf8"),
) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
};
// React is a peer only (ADR 0053): the consumer's React is the one in every bundle these
// elements are built into. The development dependency resolves the package's own imports for its
// builds and tests; nothing loads it from this workspace in a production Node.js process
// (`runtime-imports-are-production-dependencies` allows exactly this peer).
expect(Object.keys(manifest.dependencies ?? {})).toEqual(["@ekklesia/design-tokens"]);
expect(manifest.devDependencies?.react).toBeDefined();
expect(Object.keys(manifest.peerDependencies ?? {})).toEqual(["react"]);
});
it("installs its stylesheet into a document root exactly once", () => {
const remove = installEkklesiaUiStyles(document);
installEkklesiaUiStyles(document);
expect(document.querySelectorAll(`style[${EKKLESIA_UI_STYLE_ATTRIBUTE}]`).length).toBe(1);
remove();
remove();
expect(document.querySelectorAll(`style[${EKKLESIA_UI_STYLE_ATTRIBUTE}]`).length).toBe(0);
});
});
describe("the element stylesheet takes every colour, radius, and duration from a token (ADR 0060)", () => {
// Comments are dropped: they may mention a value without using it.
const css = EKKLESIA_UI_STYLESHEET.replace(/\/\*[\s\S]*?\*\//gu, "");
it("contains no raw colour", () => {
expect(css).not.toMatch(/#[0-9a-f]{3,8}\b/iu);
expect(css).not.toMatch(/\b(?:rgba?|hsla?|oklch|lab|lch|hwb)\(/iu);
// A named colour is a raw colour too; `transparent` and `currentColor` are not colours.
expect(css).not.toMatch(/:\s*(?:white|black|red|green|blue|gray|grey)\s*;/iu);
});
it("rounds corners only with radius tokens", () => {
for (const match of css.matchAll(/border-radius:\s*([^;]+);/gu)) {
// Each corner is a radius token, or square.
for (const corner of (match[1] ?? "").split(/\s+/u)) {
expect(corner, match[0]).toMatch(/^(?:var\(--ekklesia-radius-[a-z]+\)|0)$/u);
}
}
});
it("animates only for motion-token durations", () => {
for (const match of css.matchAll(/(?:animation|transition):\s*([^;]+);/gu)) {
expect(match[1], match[0]).not.toMatch(/\b\d+(?:\.\d+)?m?s\b/u);
expect(match[1], match[0]).toContain("--ekklesia-motion-duration-");
}
});
});