Files
pi2d/web/tests/tokens.test.ts
T
yuez 88e73c7737 feat(web): port the pi2-web-ui-design v0.1.1 UI
Sync the React console with the v0.1.1 prototype (20 CHGs): Quiet Workbench
shell, Chat IA v3, tool-family rendering, and the mobile page pill.

Shell / tokens (CHG-01/04/15/20):
- tokens.css rewritten from the prototype (three-layer light/dark/system),
  `--tabbar-h` removed, new faint/border-subtle/surface-1-2/fs-*/dur-*/reading-max
- 56px icon-only nav rail/sidebar (44x44 hit targets, CSS tooltips, active is a
  solid primary fill, no left bar), 52px topbar, merged preferences menu,
  ⌘K/Cmd-K palette
- `<600` gets a topbar page pill + page sheet; the bottom tabbar is gone

Pages (CHG-01): workspaces becomes a card grid with whole-card links and a
320px recent-activity column; status gets four metric tiles + a zero-primary
layout; connect gets a two-column hero, one primary per state and a collapsed
manual form.

Chat IA v3 (CHG-02/10-14/16-19): two columns, single-workspace tree with a
workspace switch, single-line rows with attention, body-level row popover,
session tray (todo/tasks) replacing the right panel, context ring moved into a
composer telemetry line, right-aligned user messages, queued messages as one
stream entity plus a composer status bar, and an in-place subagent child view.

Tool rendering (CHG-05/06/09): new `features/chat/tools` module — one row per
call, DTO adapter for the daemon's block shape, 11 kind families, state
semantics ("one state, one meaning"), 44px touch targets and the diff row
contrast fix.

i18n: 729 keys per language (zh/en parity checked), 0 unresolved keys.

Verification: web typecheck + 297 vitest + vite build green, root typecheck
green, `scripts/smoke.sh` green, and a new visual harness
(`scripts/ui-mock-server.mjs` + `scripts/ui-shots.mjs`, docs/ui-parity.md)
shoots 4 pages x 3 viewports x 2 themes = 24 screenshots clean with
overflow/console/primary/tabbar assertions. `scripts/e2e/ui-e2e.mjs` selectors
are updated to the new DOM; that suite still needs a real pi + nvim to run.
2026-10-26 20:28:47 +08:00

165 lines
5.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import appCss from "@/styles/app.css?raw";
import tokensCss from "@/styles/tokens.css?raw";
function block(source: string, selector: string): string {
// Strip comments first: the file header mentions `html[data-theme="dark"]`,
// which would otherwise make `indexOf` land inside the header and return `:root`.
const text = source.replace(/\/\*[\s\S]*?\*\//g, "");
const start = text.indexOf(selector);
expect(start, `missing selector ${selector}`).toBeGreaterThanOrEqual(0);
const open = text.indexOf("{", start);
let depth = 0;
for (let i = open; i < text.length; i += 1) {
if (text[i] === "{") depth += 1;
if (text[i] === "}") {
depth -= 1;
if (depth === 0) return text.slice(open + 1, i);
}
}
throw new Error(`unclosed block ${selector}`);
}
function customProps(source: string): Record<string, string> {
const props: Record<string, string> = {};
for (const match of source.matchAll(/--([\w-]+)\s*:\s*([^;]+);/g)) {
const name = match[1];
const value = match[2];
if (name && value) props[name] = value.trim();
}
return props;
}
describe("design tokens", () => {
const light = customProps(block(tokensCss, ":root"));
const dark = customProps(block(tokensCss, 'html[data-theme="dark"]'));
it("defines the contract's semantic token names in :root", () => {
for (const name of [
"background",
"foreground",
"card",
"card-foreground",
"popover",
"muted",
"muted-foreground",
"accent",
"primary",
"primary-foreground",
"secondary",
"destructive",
"border",
"input",
"ring",
"sidebar",
"sidebar-foreground",
"sidebar-accent",
"role-user",
"role-agent",
"role-thinking",
"role-error",
"ok",
"warn",
"err",
"info",
"radius-sm",
"radius",
"radius-lg",
"space-1",
"space-8",
"font-sans",
"font-mono",
"topbar-h",
"sidebar-w",
"rail-w",
// v0.1.1 additions (pi2-web-ui-design CHG-2026-10-23-01 / -04 / -20)
"faint",
"border-subtle",
"surface-1",
"surface-2",
"overlay",
"focus-ring",
"fs-2xs",
"fs-body",
"fs-xl",
"fw-strong",
"radius-pill",
"reading-max",
"dur-fast",
"ease-out",
]) {
expect(light[name], `--${name}`).toBeTruthy();
}
// CHG-2026-10-26-20 removed the mobile tabbar; the token must be gone.
expect(light["tabbar-h"]).toBeUndefined();
expect(dark["tabbar-h"]).toBeUndefined();
});
it("overrides the same surfaces in the dark data-theme block", () => {
expect(light.background).toBe("oklch(0.987 0.003 85)");
expect(dark.background).toBe("oklch(0.148 0.006 65)");
expect(light.foreground).not.toBe(dark.foreground);
for (const name of ["background", "foreground", "card", "muted", "muted-foreground", "primary", "border", "input", "ok", "warn", "role-user", "role-agent", "role-thinking"]) {
expect(dark[name], `--${name} dark override`).toBeTruthy();
}
// Dark borders are translucent white per the contract.
expect(dark.border).toContain("oklch(1 0 0");
});
it("maps every semantic token to a Tailwind color utility", () => {
const theme = block(appCss, "@theme inline");
const mapped = customProps(theme);
const expected: Record<string, string> = {
"color-background": "--background",
"color-foreground": "--foreground",
"color-card": "--card",
"color-card-foreground": "--card-foreground",
"color-popover": "--popover",
"color-muted": "--muted",
"color-muted-foreground": "--muted-foreground",
"color-accent": "--accent",
"color-primary": "--primary",
"color-primary-foreground": "--primary-foreground",
"color-secondary": "--secondary",
"color-destructive": "--destructive",
"color-border": "--border",
"color-input": "--input",
"color-ring": "--ring",
"color-sidebar": "--sidebar",
"color-sidebar-foreground": "--sidebar-foreground",
"color-sidebar-accent": "--sidebar-accent",
"color-role-user": "--role-user",
"color-role-agent": "--role-agent",
"color-role-thinking": "--role-thinking",
"color-role-error": "--role-error",
"color-ok": "--ok",
"color-warn": "--warn",
"color-err": "--err",
"color-info": "--info",
};
for (const [utility, token] of Object.entries(expected)) {
expect(mapped[utility], `--${utility}`).toBe(`var(${token})`);
}
expect(appCss).toContain("@custom-variant dark");
});
it("switches computed values with data-theme", () => {
const style = document.createElement("style");
style.textContent = tokensCss;
document.head.appendChild(style);
document.documentElement.dataset.theme = "light";
expect(getComputedStyle(document.documentElement).getPropertyValue("--background").trim()).toBe(
"oklch(0.987 0.003 85)",
);
document.documentElement.dataset.theme = "dark";
expect(getComputedStyle(document.documentElement).getPropertyValue("--background").trim()).toBe(
"oklch(0.148 0.006 65)",
);
style.remove();
});
});