graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
test(gui): cover response copy dispatch
yxlyxa5b957464311
diff --git a/gui/src/components/chat/ChatMarkdownMessage.test.tsx b/gui/src/components/chat/ChatMessageRow.test.tsxsimilarity index 52%rename from gui/src/components/chat/ChatMarkdownMessage.test.tsxrename to gui/src/components/chat/ChatMessageRow.test.tsxindex 3ee17d73..59598022 100644--- a/gui/src/components/chat/ChatMarkdownMessage.test.tsx+++ b/gui/src/components/chat/ChatMessageRow.test.tsx@@ -10,11 +10,15 @@ import {import type { ReactNode } from "react";import ReactDOM from "react-dom";+import type { TranscriptMessage } from "@/services/desktop/types/contracts";+const { flushSync } = ReactDOM;let unregisterDom: (() => Promise<void>) | null = null;let createRoot: typeof import("react-dom/client").createRoot;let ChatMarkdownMessage: typeof import("./ChatMarkdownMessage").ChatMarkdownMessage;+let ChatMessageRow: typeof import("./ChatMessageRow").ChatMessageRow;+let ChatTranscriptMessage: typeof import("./ChatTranscriptMessage").ChatTranscriptMessage;beforeAll(async () => {const { GlobalRegistrator } = await import("@happy-dom/global-registrator");@@ -26,6 +30,8 @@ beforeAll(async () => {});({ createRoot } = await import("react-dom/client"));({ ChatMarkdownMessage } = await import("./ChatMarkdownMessage"));+ ({ ChatMessageRow } = await import("./ChatMessageRow"));+ ({ ChatTranscriptMessage } = await import("./ChatTranscriptMessage"));});afterAll(async () => {@@ -62,7 +68,19 @@ function mount(node: ReactNode) {};}-function mountMessage(text: string, copyText?: string) {+function contentMessage(+ kind: "user" | "assistant" | "reasoning",+ text: string,+): Extract<TranscriptMessage, { kind: "user" | "assistant" | "reasoning" }> {+ return {+ kind,+ id: `${kind}-1`,+ requestId: "request-1",+ text,+ };+}++function mountMarkdownMessage(text: string, copyText?: string) {return mount(<ChatMarkdownMessagecopyText={copyText}@@ -80,21 +98,112 @@ function setClipboard(writeText: (value: string) => Promise<void>) {});}-async function flushUpdates(delayMs = 0) {+async function flushUpdates(delayMs = 10) {await new Promise((resolve) => window.setTimeout(resolve, delayMs));}-async function click(button: HTMLButtonElement) {- button.click();- await flushUpdates();-}+describe("chat response copy dispatch", () => {+ test("the thread adds the action only to assistant messages", () => {+ const messages: Array<[TranscriptMessage, boolean]> = [+ [contentMessage("assistant", "Answer"), true],+ [contentMessage("user", "Question"), false],+ [contentMessage("reasoning", "Private reasoning"), false],+ [+ {+ kind: "status",+ id: "status-1",+ requestId: "request-1",+ title: "Working",+ subtitle: "Still working",+ category: "info",+ },+ false,+ ],+ [+ {+ kind: "tool_start",+ id: "tool-1",+ requestId: "request-1",+ name: "shell",+ callId: "call-1",+ detail: {+ kind: "shell",+ command: "pwd",+ cwd: null,+ description: null,+ },+ },+ false,+ ],+ ];++ for (const [message, shouldCopy] of messages) {+ const row = mount(+ <ChatTranscriptMessage message={message} workspacePath={null} />,+ );+ try {+ expect(+ row.container.querySelector('button[aria-label="Copy response"]') !=+ null,+ ).toBe(shouldCopy);+ } finally {+ row.unmount();+ }+ }+ });++ test("an assistant row streamed in place copies the latest response only", async () => {+ const writeText = mock(async () => {});+ setClipboard(writeText);+ const row = mount(+ <ChatMessageRow+ message={contentMessage("assistant", "")}+ workspacePath={null}+ />,+ );++ try {+ expect(row.container.querySelector("button")).toBeNull();++ row.rerender(+ <ChatMessageRow+ message={contentMessage("assistant", "Partial")}+ workspacePath={null}+ />,+ );+ expect(+ row.container.querySelector('button[aria-label="Copy response"]'),+ ).not.toBeNull();++ const finalText = "Final **answer**\n\n```ts\nconst value = 1;\n```";+ row.rerender(+ <ChatMessageRow+ message={contentMessage("assistant", finalText)}+ workspacePath={null}+ />,+ );+ const button = row.container.querySelector<HTMLButtonElement>(+ 'button[aria-label="Copy response"]',+ );+ expect(button).not.toBeNull();++ button!.click();+ await flushUpdates();++ expect(writeText).toHaveBeenCalledTimes(1);+ expect(writeText).toHaveBeenCalledWith(finalText);+ } finally {+ row.unmount();+ }+ });+});-describe("assistant response copy action", () => {+describe("assistant response copy behavior", () => {test("copies the exact raw output, including Markdown, whitespace, and Unicode", async () => {const text = "## Result\n\n```ts\nconst café = \"☕\";\n```\n\nDone. ";const writeText = mock(async () => {});setClipboard(writeText);- const { container, unmount } = mountMessage(text, text);+ const { container, unmount } = mountMarkdownMessage(text, text);try {const button = container.querySelector<HTMLButtonElement>(@@ -102,7 +211,8 @@ describe("assistant response copy action", () => {);expect(button).not.toBeNull();- await click(button!);+ button!.click();+ await flushUpdates();expect(writeText).toHaveBeenCalledTimes(1);expect(writeText).toHaveBeenCalledWith(text);@@ -113,8 +223,8 @@ describe("assistant response copy action", () => {}});- test("does not add a copy action when no assistant copy payload is provided", () => {- const { container, unmount } = mountMessage("Private reasoning text");+ test("does not add a copy action without an assistant copy payload", () => {+ const { container, unmount } = mountMarkdownMessage("Private reasoning text");try {expect(container.querySelector('button[aria-label="Copy response"]')).toBeNull();} finally {@@ -122,10 +232,10 @@ describe("assistant response copy action", () => {}});- test("adds the action during streaming and copies the latest output exactly", async () => {+ test("adds the action during streaming and copies whitespace exactly", async () => {const writeText = mock(async () => {});setClipboard(writeText);- const message = mountMessage("", "");+ const message = mountMarkdownMessage("", "");try {expect(message.container.querySelector("button")).toBeNull();@@ -143,7 +253,8 @@ describe("assistant response copy action", () => {'button[aria-label="Copy response"]',);expect(button).not.toBeNull();- await click(button!);+ button!.click();+ await flushUpdates();expect(writeText).toHaveBeenCalledWith(" \n\t ");} finally {message.unmount();@@ -151,11 +262,12 @@ describe("assistant response copy action", () => {});test("keeps the default label when clipboard access is unavailable or rejects", async () => {- const unavailable = mountMessage("Hello", "Hello");+ const unavailable = mountMarkdownMessage("Hello", "Hello");try {const button = unavailable.container.querySelector<HTMLButtonElement>("button");expect(button).not.toBeNull();- await click(button!);+ button!.click();+ await flushUpdates();expect(button!.getAttribute("aria-label")).toBe("Copy response");} finally {unavailable.unmount();@@ -164,11 +276,12 @@ describe("assistant response copy action", () => {setClipboard(async () => {throw new Error("clipboard denied");});- const rejected = mountMessage("Hello again", "Hello again");+ const rejected = mountMarkdownMessage("Hello again", "Hello again");try {const button = rejected.container.querySelector<HTMLButtonElement>("button");expect(button).not.toBeNull();- await click(button!);+ button!.click();+ await flushUpdates();expect(button!.getAttribute("aria-label")).toBe("Copy response");expect(button!.textContent).toBe("Copy response");} finally {@@ -178,12 +291,13 @@ describe("assistant response copy action", () => {test("returns copied feedback to the default label after the reset delay", async () => {setClipboard(async () => {});- const { container, unmount } = mountMessage("Reset me", "Reset me");+ const { container, unmount } = mountMarkdownMessage("Reset me", "Reset me");try {const button = container.querySelector<HTMLButtonElement>("button");expect(button).not.toBeNull();- await click(button!);+ button!.click();+ await flushUpdates();expect(button!.getAttribute("aria-label")).toBe("Copied");await flushUpdates(1_250);diff --git a/gui/src/components/chat/ChatMessageRow.tsx b/gui/src/components/chat/ChatMessageRow.tsxindex 0faaa0d2..b0beaf90 100644--- a/gui/src/components/chat/ChatMessageRow.tsx+++ b/gui/src/components/chat/ChatMessageRow.tsx@@ -9,7 +9,6 @@ import {classifyPath,parseAttachmentBlock,} from "@/components/attachments/attachmentTypes";-import { openPathDefault } from "@/services/desktop/client";import type {ChatMessageRowProps,TextOnlyMessageProps,@@ -20,9 +19,11 @@ interface UserChatMessageProps extends TextOnlyMessageProps {}function handleOpenAttachment(path: string) {- void openPathDefault(path).catch((error) => {- console.error("Failed to open attachment", error);- });+ void import("@/services/desktop/client")+ .then(({ openPathDefault }) => openPathDefault(path))+ .catch((error) => {+ console.error("Failed to open attachment", error);+ });}function UserChatMessage({ text, workspacePath }: UserChatMessageProps) {diff --git a/gui/src/components/chat/ChatTranscriptMessage.tsx b/gui/src/components/chat/ChatTranscriptMessage.tsxnew file mode 100644index 00000000..1215b189--- /dev/null+++ b/gui/src/components/chat/ChatTranscriptMessage.tsx@@ -0,0 +1,23 @@+import type { TranscriptMessage } from "@/services/desktop/types/contracts";++import { ChatEventRow } from "./ChatEventRow";+import { ChatMessageRow } from "./ChatMessageRow";++interface ChatTranscriptMessageProps {+ message: TranscriptMessage;+ workspacePath: string | null;+}++export function ChatTranscriptMessage({+ message,+ workspacePath,+}: ChatTranscriptMessageProps) {+ switch (message.kind) {+ case "user":+ case "assistant":+ case "reasoning":+ return <ChatMessageRow message={message} workspacePath={workspacePath} />;+ default:+ return <ChatEventRow message={message} />;+ }+}diff --git a/gui/src/components/chat/utils/chatThreadList.tsx b/gui/src/components/chat/utils/chatThreadList.tsxindex a201c03a..cdbd73fc 100644--- a/gui/src/components/chat/utils/chatThreadList.tsx+++ b/gui/src/components/chat/utils/chatThreadList.tsx@@ -1,8 +1,7 @@import type { TranscriptMessage } from "@/services/desktop/types/contracts";import { ChatCommandResultRow } from "../ChatCommandResultRow";-import { ChatEventRow } from "../ChatEventRow";-import { ChatMessageRow } from "../ChatMessageRow";+import { ChatTranscriptMessage } from "../ChatTranscriptMessage";import { ChatWorkRow } from "../ChatWorkRow";import { THREAD_ITEM_GAP } from "../constants/chatThread";import type {@@ -73,17 +72,6 @@ function estimateCommandResultItemSize(return Math.min(520, 64 + lineCount * 20);}-function renderChatMessage(message: TranscriptMessage, workspacePath: string | null) {- switch (message.kind) {- case "user":- case "assistant":- case "reasoning":- return <ChatMessageRow message={message} workspacePath={workspacePath} />;- default:- return <ChatEventRow message={message} />;- }-}-export function estimateChatThreadItemSize(item: ChatThreadItem): number {const contentSize =item.kind === "message"@@ -106,7 +94,10 @@ export function renderChatThreadItem() {const row =item.kind === "message" ? (- renderChatMessage(item.message, workspacePath)+ <ChatTranscriptMessage+ message={item.message}+ workspacePath={workspacePath}+ />) : item.kind === "request_work" ? (<ChatWorkRowkey={item.key}