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 { readPromptHistory, rememberPrompt } from "@/features/chat/prompt-history"; 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 = {}) { 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( , ); 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( , ); 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(); }); }); /** * Prompt recall (↑/↓), the web port of pi2.nvim's prompt-buffer history. The * store/nav contract itself is covered in `prompt-history.test.ts`; here we * only pin the keyboard rules the composer adds on top of it. */ describe("composer prompt history", () => { /** Place a collapsed caret, so the ArrowUp/ArrowDown line rule is explicit. */ function caretAt(element: HTMLTextAreaElement, at: number): void { element.setSelectionRange(at, at); } function input(): HTMLTextAreaElement { return screen.getByTestId("composer-input") as HTMLTextAreaElement; } it("recalls older prompts on ArrowUp and the draft past the newest", () => { rememberPrompt("w1", "first"); rememberPrompt("w1", "second"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.change(textarea, { target: { value: "my draft" } }); caretAt(textarea, 0); // A recall swallows the key (the caret must not also move). expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false); expect(textarea).toHaveValue("second"); expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false); expect(textarea).toHaveValue("first"); // At the oldest entry the key is left to the caret again. expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(true); expect(textarea).toHaveValue("first"); fireEvent.keyDown(textarea, { key: "ArrowDown" }); expect(textarea).toHaveValue("second"); // Past the newest entry the in-progress draft is restored. fireEvent.keyDown(textarea, { key: "ArrowDown" }); expect(textarea).toHaveValue("my draft"); // Recall is over: ↓ is a plain caret move again. expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(true); expect(textarea).toHaveValue("my draft"); }); it("leaves ArrowUp to the caret inside a multi-line draft", () => { rememberPrompt("w1", "old prompt"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); const draft = "line1\nline2"; fireEvent.change(textarea, { target: { value: draft } }); // Caret on the second line: ↑ must move the caret, not recall. caretAt(textarea, draft.length); expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(true); expect(textarea).toHaveValue(draft); // Caret on the first line: ↑ recalls. caretAt(textarea, 0); expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false); expect(textarea).toHaveValue("old prompt"); }); it("leaves ArrowDown to the caret above the last line", () => { rememberPrompt("w1", "archived"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); const draft = "draft\ntail"; fireEvent.change(textarea, { target: { value: draft } }); caretAt(textarea, 2); // still on the first line expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(true); expect(textarea).toHaveValue(draft); }); it("walks back out of a recalled multi-line entry from its last line", () => { rememberPrompt("w1", "line1\nline2"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.change(textarea, { target: { value: "draft" } }); caretAt(textarea, 0); expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false); expect(textarea).toHaveValue("line1\nline2"); // The caret is parked on the entry's last line, so ↓ leaves recall. expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(false); expect(textarea).toHaveValue("draft"); }); it("always recalls on Ctrl+P / Ctrl+N", () => { rememberPrompt("w1", "alpha"); rememberPrompt("w1", "beta"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.change(textarea, { target: { value: "draft text" } }); caretAt(textarea, 3); // mid-line: the caret rule does not apply fireEvent.keyDown(textarea, { key: "p", ctrlKey: true }); expect(textarea).toHaveValue("beta"); fireEvent.keyDown(textarea, { key: "p", ctrlKey: true }); expect(textarea).toHaveValue("alpha"); fireEvent.keyDown(textarea, { key: "n", ctrlKey: true }); expect(textarea).toHaveValue("beta"); fireEvent.keyDown(textarea, { key: "n", ctrlKey: true }); expect(textarea).toHaveValue("draft text"); }); it("moves the caret to the end of a recalled prompt", () => { rememberPrompt("w1", "remembered prompt"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.keyDown(textarea, { key: "ArrowUp" }); expect(textarea).toHaveValue("remembered prompt"); expect(textarea.selectionStart).toBe("remembered prompt".length); expect(textarea.selectionEnd).toBe("remembered prompt".length); }); it("ends recall when the user edits by hand", () => { rememberPrompt("w1", "older"); rememberPrompt("w1", "newer"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.keyDown(textarea, { key: "ArrowUp" }); expect(textarea).toHaveValue("newer"); fireEvent.change(textarea, { target: { value: "newer, edited" } }); caretAt(textarea, "newer, edited".length); // No longer browsing: ↓ must not replace the edit with a stale entry. expect(fireEvent.keyDown(textarea, { key: "ArrowDown" })).toBe(true); expect(textarea).toHaveValue("newer, edited"); // ↓ again starts from the newest entry, not from where recall stopped. expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false); expect(textarea).toHaveValue("newer"); }); it("remembers each send in its own workspace, trimmed and deduped", () => { const { onSend, unmount } = renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.change(textarea, { target: { value: " padded " } }); fireEvent.keyDown(textarea, { key: "Enter" }); expect(onSend).toHaveBeenCalledWith("padded", [], undefined); expect(readPromptHistory("w1")).toEqual(["padded"]); unmount(); // Another workspace shares no history… const other = renderComposer(sessionFixture({ status: "idle", workspaceId: "w2" })); fireEvent.change(input(), { target: { value: "from w2" } }); fireEvent.keyDown(input(), { key: "Enter" }); expect(other.onSend).toHaveBeenCalledWith("from w2", [], undefined); expect(readPromptHistory("w2")).toEqual(["from w2"]); expect(readPromptHistory("w1")).toEqual(["padded"]); other.unmount(); // …and the first one still recalls its own send. renderComposer(sessionFixture({ status: "idle" })); expect(fireEvent.keyDown(input(), { key: "ArrowUp" })).toBe(false); expect(input()).toHaveValue("padded"); }); it("does not let an IME own-up compose recall its candidate list", () => { rememberPrompt("w1", "archived"); renderComposer(sessionFixture({ status: "idle" })); const textarea = input(); fireEvent.change(textarea, { target: { value: "nihao" } }); caretAt(textarea, 5); expect(fireEvent.keyDown(textarea, { key: "ArrowUp", isComposing: true })).toBe(true); expect(textarea).toHaveValue("nihao"); }); it("recalls from an injected feed and records sends into it", () => { // `pages/chat.tsx` injects the plugin-backed feed; the composer must neither // read the mirror nor decide where a send is recorded. const entries: string[] = ["typed in nvim"]; const remember = vi.fn((text: string) => entries.push(text)); const { onSend } = renderComposer(sessionFixture({ status: "idle" }), { history: { entries: () => entries, remember }, }); const textarea = input(); expect(fireEvent.keyDown(textarea, { key: "ArrowUp" })).toBe(false); expect(textarea).toHaveValue("typed in nvim"); fireEvent.keyDown(textarea, { key: "Enter" }); expect(onSend).toHaveBeenCalledWith("typed in nvim", [], undefined); expect(remember).toHaveBeenCalledWith("typed in nvim"); // The mirror stays untouched: the injected feed owns the archive now. expect(readPromptHistory("w1")).toEqual([]); }); });