fix: support fallback copy for webui replies

This commit is contained in:
chengyongru
2026-06-02 14:08:55 +08:00
committed by Xubin Ren
parent 1886d22352
commit 3e98a03188
3 changed files with 109 additions and 2 deletions
+66
View File
@@ -167,6 +167,72 @@ describe("MessageBubble", () => {
);
});
it("copies completed assistant replies with the textarea fallback", async () => {
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: undefined,
});
const execCommand = vi.fn().mockReturnValue(true);
Object.defineProperty(document, "execCommand", {
configurable: true,
value: execCommand,
});
const message: UIMessage = {
id: "a-copy-fallback",
role: "assistant",
content: "Fallback copy reply.",
createdAt: Date.now(),
};
try {
render(<MessageBubble message={message} />);
fireEvent.click(screen.getByRole("button", { name: "Copy reply" }));
await waitFor(() => expect(execCommand).toHaveBeenCalledWith("copy"));
await waitFor(() =>
expect(screen.getByRole("button", { name: "Copied reply" })).toBeInTheDocument(),
);
} finally {
Reflect.deleteProperty(navigator, "clipboard");
Reflect.deleteProperty(document, "execCommand");
}
});
it("falls back when the Clipboard API rejects assistant reply copy", async () => {
const writeText = vi.fn().mockRejectedValue(new Error("not allowed"));
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: { writeText },
});
const execCommand = vi.fn().mockReturnValue(true);
Object.defineProperty(document, "execCommand", {
configurable: true,
value: execCommand,
});
const message: UIMessage = {
id: "a-copy-reject",
role: "assistant",
content: "Rejected clipboard copy.",
createdAt: Date.now(),
};
try {
render(<MessageBubble message={message} />);
fireEvent.click(screen.getByRole("button", { name: "Copy reply" }));
expect(writeText).toHaveBeenCalledWith("Rejected clipboard copy.");
await waitFor(() => expect(execCommand).toHaveBeenCalledWith("copy"));
await waitFor(() =>
expect(screen.getByRole("button", { name: "Copied reply" })).toBeInTheDocument(),
);
} finally {
Reflect.deleteProperty(navigator, "clipboard");
Reflect.deleteProperty(document, "execCommand");
}
});
it("does not show copy actions for streaming placeholders", () => {
const message: UIMessage = {
id: "a-streaming",