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

218 lines
9.0 KiB
TypeScript

import { fireEvent, render, screen } from "@testing-library/react";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { Composer, type ComposerProps } from "@/features/chat/composer";
import { setLanguage } from "@/lib/i18n";
import { sessionFixture } from "./chat-fixtures";
beforeAll(async () => {
await setLanguage("en");
});
const MODELS = [
{
provider: "anthropic",
models: [{ id: "claude-sonnet-4-5" }, { id: "claude-haiku-4-5" }],
},
];
function renderComposer(session = sessionFixture(), overrides: Partial<ComposerProps> = {}) {
const onSend = vi.fn();
const onAbort = vi.fn();
const onRemoveQueue = vi.fn();
const onSetSteer = vi.fn();
const onSetModel = vi.fn();
const onSetThinking = vi.fn();
const utils = render(
<Composer
session={session}
models={MODELS}
connected
onSend={onSend}
onAbort={onAbort}
onRemoveQueue={onRemoveQueue}
onSetSteer={onSetSteer}
onSetModel={onSetModel}
onSetThinking={onSetThinking}
{...overrides}
/>,
);
return { ...utils, onSend, onAbort, onRemoveQueue, onSetSteer, onSetModel, onSetThinking };
}
describe("composer", () => {
it("exposes the stable Playwright selectors", () => {
renderComposer(sessionFixture({ status: "busy" }));
expect(screen.getByTestId("composer-input")).toHaveClass("composer__ta");
// Busy: the single slot is the abort button, so `composer-send` is absent.
expect(screen.queryByTestId("composer-send")).not.toBeInTheDocument();
expect(screen.getByTestId("composer-abort")).toHaveClass("composer__send");
expect(screen.getByTestId("composer-model")).toHaveAttribute("aria-label", "Model");
expect(screen.getByTestId("composer-thinking")).toHaveAttribute("aria-label", "Thinking level");
});
it("keeps one circular send slot while the session is idle", () => {
renderComposer(sessionFixture({ status: "idle" }));
const send = screen.getByTestId("composer-send");
expect(send).toHaveClass("composer__send");
expect(send).not.toHaveClass("is-busy");
expect(screen.queryByTestId("composer-abort")).not.toBeInTheDocument();
// The text label is screen-reader only; the button itself is an icon.
expect(screen.getByRole("button", { name: "Send" })).toBe(send);
});
it("sends on Enter and keeps Shift+Enter for a newline", () => {
const { onSend } = renderComposer(sessionFixture({ status: "idle" }));
const textarea = screen.getByTestId("composer-input");
fireEvent.change(textarea, { target: { value: "hello" } });
fireEvent.keyDown(textarea, { key: "Enter", shiftKey: true });
expect(onSend).not.toHaveBeenCalled();
fireEvent.keyDown(textarea, { key: "Enter" });
// An idle session sends a plain prompt (no `follow_up` / `steer` mode).
expect(onSend).toHaveBeenCalledWith("hello", [], undefined);
expect(textarea).toHaveValue("");
});
it("queues an Enter send as a follow-up while the session is busy", () => {
const { onSend } = renderComposer(sessionFixture({ status: "busy" }));
const textarea = screen.getByTestId("composer-input");
fireEvent.change(textarea, { target: { value: "next step" } });
fireEvent.keyDown(textarea, { key: "Enter" });
expect(onSend).toHaveBeenCalledWith("next step", [], "follow_up");
});
it("turns the send slot into a single red abort circle while busy", () => {
const { onAbort } = renderComposer(sessionFixture({ status: "busy" }));
// No separate steer / abort text buttons any more (CHG-2026-10-26-16).
expect(screen.queryByTestId("composer-steer")).not.toBeInTheDocument();
const abort = screen.getByTestId("composer-abort");
expect(abort).toHaveClass("composer__send");
expect(abort).toHaveClass("is-busy");
expect(screen.queryAllByTestId("composer-send")).toHaveLength(0);
fireEvent.click(abort);
expect(onAbort).toHaveBeenCalledTimes(1);
});
it("never renders queue bodies, per-item steer toggles or delete buttons", () => {
const session = sessionFixture({
status: "idle",
queue: [
{ id: "q1", text: "later please", steer: false },
{ id: "q2", text: "and this one", steer: true },
],
});
renderComposer(session);
expect(screen.queryByText("later please")).not.toBeInTheDocument();
expect(screen.queryByText("and this one")).not.toBeInTheDocument();
expect(screen.queryByRole("checkbox")).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Remove queued message" })).not.toBeInTheDocument();
});
it("reports counts in a role=status bar, not the message text", () => {
const session = sessionFixture({
status: "busy",
queue: [
{ id: "q1", text: "later please", steer: false },
{ id: "q2", text: "and this one", steer: true },
],
});
renderComposer(session);
const bar = screen.getByTestId("composer-queue");
expect(bar).toHaveAttribute("role", "status");
expect(bar).toHaveAttribute("aria-live", "polite");
expect(bar).not.toHaveClass("is-empty");
expect(bar).toHaveTextContent(/2 pending/);
expect(bar).toHaveTextContent(/1 inserting now/);
// "inserting now" supersedes the after-turn hint.
expect(bar).not.toHaveTextContent(/Sends after this turn/);
});
it("falls back to the after-turn hint and hides the bar when the queue is empty", () => {
const session = sessionFixture({
status: "busy",
queue: [{ id: "q1", text: "later please", steer: false }],
});
const { rerender } = renderComposer(session);
expect(screen.getByTestId("composer-queue")).toHaveTextContent(/1 pending/);
expect(screen.getByTestId("composer-queue")).toHaveTextContent(/Sends after this turn/);
rerender(
<Composer
session={sessionFixture({ status: "busy", queue: [] })}
models={MODELS}
connected
onSend={vi.fn()}
onAbort={vi.fn()}
onRemoveQueue={vi.fn()}
onSetSteer={vi.fn()}
onSetModel={vi.fn()}
onSetThinking={vi.fn()}
/>,
);
const empty = screen.getByTestId("composer-queue");
expect(empty).toHaveClass("is-empty");
expect(empty).toHaveTextContent(/0 pending/);
});
it("counts the optimistic send until the queue projects it, and honours queuedCount", () => {
const sending = { text: "first prompt" };
const { unmount } = renderComposer(sessionFixture({ status: "busy", queue: [] }), { sending });
expect(screen.getByTestId("composer-queue")).toHaveTextContent(/1 pending/);
// The body of the optimistic message never shows up in the composer.
expect(screen.queryByText("first prompt")).not.toBeInTheDocument();
unmount();
renderComposer(
sessionFixture({
status: "busy",
queue: [{ id: "q1", text: "later please", steer: false }],
}),
{ sending, queuedCount: 7 },
);
expect(screen.getByTestId("composer-queue")).toHaveTextContent(/7 pending/);
});
it("clears the queue through onClearQueue", () => {
const session = sessionFixture({
status: "busy",
queue: [
{ id: "q1", text: "later please", steer: false },
{ id: "q2", text: "and this one", steer: false },
],
});
const onClearQueue = vi.fn();
const { onRemoveQueue } = renderComposer(session, { onClearQueue });
fireEvent.click(screen.getByRole("button", { name: "Clear queue" }));
expect(onClearQueue).toHaveBeenCalledTimes(1);
expect(onRemoveQueue).not.toHaveBeenCalled();
});
it("falls back to per-item removal when no bulk clear is wired", () => {
const session = sessionFixture({
status: "busy",
queue: [
{ id: "q1", text: "later please", steer: false },
{ id: "q2", text: "and this one", steer: false },
],
});
const { onRemoveQueue } = renderComposer(session);
fireEvent.click(screen.getByRole("button", { name: "Clear queue" }));
expect(onRemoveQueue.mock.calls.map(([id]) => id)).toEqual(["q1", "q2"]);
});
it("drops the mock-era demo copy from the attachment dialog", () => {
renderComposer();
fireEvent.click(screen.getByRole("button", { name: "Add attachment" }));
expect(screen.queryByText(/Demo only/i)).not.toBeInTheDocument();
expect(screen.queryByText(/prototype demo only/i)).not.toBeInTheDocument();
});
});