Files
pi2d/web/tests/chat.blocks.test.tsx
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

161 lines
7.1 KiB
TypeScript

import { fireEvent, render, screen } from "@testing-library/react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import type { Entry } from "@protocol/types";
import { EntryBlock } from "@/features/chat/blocks";
import { setLanguage } from "@/lib/i18n";
import { entryFixture } from "./chat-fixtures";
beforeAll(async () => {
await setLanguage("en");
});
function bashTool(): Entry["blocks"][number] {
return {
type: "tool",
toolCallId: "call_1",
name: "bash",
args: { command: "ls -la" },
state: "done",
durationMs: 1200,
output: { text: "a.txt\nb.txt" },
};
}
function assistantEntry(overrides: Partial<Entry> = {}): Entry {
return entryFixture({
id: "a1",
role: "assistant",
blocks: [{ type: "text", text: "Looking at the auth path now." }, bashTool()],
...overrides,
});
}
describe("chat blocks — tool rows", () => {
it("renders a tool block as one family row, never the legacy card", () => {
const { container } = render(<EntryBlock entry={assistantEntry()} />);
const rows = container.querySelectorAll(".tool");
expect(rows).toHaveLength(1);
const row = rows[0] as HTMLElement;
expect(row).not.toHaveClass("card");
expect(row).toHaveClass("tool--shell", "tool--done");
expect(row.getAttribute("data-tool")).toBe("bash");
expect(row.getAttribute("data-state")).toBe("done");
// v0.1.0 anatomy is gone (`.tool__ico/.tool__name/.tool__sum`).
expect(container.querySelector(".tool__ico")).toBeNull();
expect(container.querySelector(".tool__name")).toBeNull();
expect(container.querySelector(".tool__sum")).toBeNull();
// v0.1.1 anatomy is present.
expect(row.querySelector(".tool__state")).toBeTruthy();
expect(row.querySelector(".tool__verb")).toBeTruthy();
expect(row.querySelector(".tool__target")?.textContent).toBe("ls -la");
});
it("mounts the family body only after the row is expanded", () => {
const { container } = render(<EntryBlock entry={assistantEntry()} />);
const body = container.querySelector(".tool__body") as HTMLElement;
expect(body).toHaveAttribute("hidden");
expect(container.querySelector(".shell__body")).toBeNull();
fireEvent.click(container.querySelector(".tool__head") as HTMLElement);
expect(body).not.toHaveAttribute("hidden");
expect(container.querySelector(".shell__body")).toBeTruthy();
expect(container.querySelector(".shell__cmd-text")?.textContent).toBe("ls -la");
expect(screen.getByText("a.txt")).toBeInTheDocument();
});
it("forwards the subagent opener through the tool ctx", () => {
const onOpenSubagent = vi.fn();
const { container } = render(<EntryBlock entry={assistantEntry()} onOpenSubagent={onOpenSubagent} />);
// The row still renders; the ctx is only consumed by families that offer the action.
expect(container.querySelectorAll(".tool")).toHaveLength(1);
expect(onOpenSubagent).not.toHaveBeenCalled();
});
});
describe("chat blocks — meta rules", () => {
it("shows meta on the turn's first message and drops it on same-turn follow-ups", () => {
const entry = assistantEntry();
const { container, rerender } = render(<EntryBlock entry={entry} turnStart />);
expect(container.querySelector(".msg__meta")).not.toBeNull();
rerender(<EntryBlock entry={entry} turnStart={false} />);
expect(container.querySelector(".msg__meta")).toBeNull();
rerender(<EntryBlock entry={entry} showMeta={false} />);
expect(container.querySelector(".msg__meta")).toBeNull();
rerender(<EntryBlock entry={entry} showMeta />);
expect(container.querySelector(".msg__meta")).not.toBeNull();
});
it("keeps meta on a user message and never invents one for a system entry", () => {
const user = entryFixture({ id: "u1", role: "user", blocks: [{ type: "text", text: "Fix the auth bug" }] });
const { container, rerender } = render(<EntryBlock entry={user} showMeta={false} />);
expect(container.querySelector(".msg__meta .who--user")).not.toBeNull();
const system = entryFixture({ id: "s1", role: "system", blocks: [{ type: "text", text: "session started" }] });
rerender(<EntryBlock entry={system} turnStart />);
expect(container.querySelector(".msg--system")).not.toBeNull();
expect(container.querySelector(".msg__meta")).toBeNull();
expect(container.querySelector(".msg__text")?.textContent).toBe("session started");
});
it("marks user messages for the right-aligned rail and leaves error rows on the left", () => {
const user = entryFixture({ id: "u1", role: "user", blocks: [{ type: "text", text: "Fix the auth bug" }] });
const { container } = render(<EntryBlock entry={user} />);
const article = container.querySelector("article") as HTMLElement;
expect(article).toHaveClass("msg", "msg--user");
expect(article.querySelector(".msg__meta")).not.toBeNull();
const assistant = assistantEntry();
expect(assistant.role).toBe("assistant");
});
});
describe("chat blocks — notice (sub-report)", () => {
function noticeEntry(extra: Record<string, unknown> = {}): Entry {
return {
...entryFixture({ id: "n1", role: "notice", blocks: [{ type: "text", text: "researcher finished" }] }),
...extra,
} as Entry;
}
it("turns the notice meta into a link that opens the subagent view", () => {
const onOpenSubagent = vi.fn();
const { container } = render(
<EntryBlock entry={noticeEntry({ subagentName: "researcher" })} onOpenSubagent={onOpenSubagent} />,
);
const link = container.querySelector(".who--agent.sub-report__link") as HTMLElement;
expect(link.tagName).toBe("BUTTON");
expect(container.querySelector(".msg--notice")).not.toBeNull();
fireEvent.click(link);
expect(onOpenSubagent).toHaveBeenCalledTimes(1);
expect(onOpenSubagent).toHaveBeenCalledWith("researcher");
});
it("stays a plain label when no subagent name or opener is known", () => {
const { container, rerender } = render(<EntryBlock entry={noticeEntry()} onOpenSubagent={vi.fn()} />);
expect(container.querySelector(".sub-report__link")).toBeNull();
expect(container.querySelector(".msg__meta .who--agent")?.tagName).toBe("SPAN");
rerender(<EntryBlock entry={noticeEntry({ subagentName: "researcher" })} />);
expect(container.querySelector(".sub-report__link")).toBeNull();
});
it("keeps the error rail for error blocks inside a notice", () => {
const entry = entryFixture({ id: "n2", role: "notice", blocks: [{ type: "error", text: "agent aborted" }] });
const { container } = render(<EntryBlock entry={entry} />);
const error = container.querySelector(".msg__error");
expect(error).not.toBeNull();
expect(error?.querySelector(".who--err")).not.toBeNull();
expect(screen.getByText("agent aborted")).toBeInTheDocument();
});
});