diff --git a/web/src/components/app/AppFeedback.command.test.tsx b/web/src/components/app/AppFeedback.command.test.tsx new file mode 100644 index 000000000..a86f73d42 --- /dev/null +++ b/web/src/components/app/AppFeedback.command.test.tsx @@ -0,0 +1,117 @@ +// @vitest-environment happy-dom +import { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; + +import { useUIStore } from "../../stores/useUIStore"; +import { AppFeedback } from "./AppFeedback"; + +vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) })); + +const ACP = + "io_error: runtime_error: Antigravity ACP session initialization failed. Complete separate ACP login with `cccc setup --runtime antigravity --runtime-mode acp --login`. ACP session/new failed (code -32000): Authentication required"; +const COMMAND = "cccc setup --runtime antigravity --runtime-mode acp --login"; + +describe("an error that names a command to run", () => { + let host: HTMLDivElement; + let root: Root; + const writeText = vi.fn(); + const render = (errorMsg: string, dismissError = vi.fn()) => + act(async () => + root.render( + , + ), + ); + const alert = () => host.querySelector('[role="alert"]')!; + const copyKey = () => + [...alert().querySelectorAll("button")].find((button) => + button.textContent?.startsWith("commandError.cop"), + )!; + + beforeEach(() => { + Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true }); + writeText.mockReset().mockResolvedValue(undefined); + // The browser boundary: the clipboard is where a copied command has to arrive. + vi.stubGlobal("navigator", { ...navigator, clipboard: { writeText } }); + host = document.createElement("div"); + document.body.append(host); + root = createRoot(host); + }); + afterEach(async () => { + await act(async () => root.unmount()); + host.remove(); + vi.unstubAllGlobals(); + vi.useRealTimers(); + useUIStore.getState().dismissError(); + }); + + it("says what to do, shows the command on its own, and copies exactly that command", async () => { + await render(ACP); + + expect(alert().querySelector("[data-command-error]")?.getAttribute("data-command-error")).toBe( + "antigravity_acp_login", + ); + expect(alert().textContent).toContain("commandError.antigravityAcpLogin"); + expect(alert().querySelector("code")?.textContent).toBe(COMMAND); + + await act(async () => copyKey().click()); + + expect(writeText).toHaveBeenCalledExactlyOnceWith(COMMAND); + expect(copyKey().textContent).toBe("commandError.copied"); + }); + + it("keeps the daemon's own words available under the summary", async () => { + await render(ACP); + + const details = alert().querySelector("details")!; + expect(details.open).toBe(false); + expect(details.querySelector("p")?.textContent).toBe(ACP); + }); + + it("says so when the command could not be copied", async () => { + writeText.mockRejectedValue(new Error("fixture: clipboard denied")); + // The fallback copy through a hidden field is refused as well. + const execCommand = vi.fn(() => false); + Object.defineProperty(document, "execCommand", { value: execCommand, configurable: true }); + await render(ACP); + + await act(async () => copyKey().click()); + + expect(copyKey().textContent).toBe("commandError.copyFailed"); + }); + + it("leaves an error without a command as the plain message it was", async () => { + await render("daemon unavailable"); + + expect(alert().querySelector("[data-command-error]")).toBeNull(); + expect(alert().querySelector("details")).toBeNull(); + expect(alert().textContent).toBe("daemon unavailable×"); + }); + + it("stays on screen until dismissed, while a plain error still clears by itself", async () => { + vi.useFakeTimers(); + + useUIStore.getState().showError(ACP); + vi.advanceTimersByTime(60_000); + expect(useUIStore.getState().errorMsg).toBe(ACP); + + // A later plain error replaces it and goes away on the usual schedule. + useUIStore.getState().showError("daemon unavailable"); + vi.advanceTimersByTime(7_999); + expect(useUIStore.getState().errorMsg).toBe("daemon unavailable"); + vi.advanceTimersByTime(1); + expect(useUIStore.getState().errorMsg).toBe(""); + + // And a command error shown after a plain one is not cleared by the plain one's timer. + useUIStore.getState().showError("daemon unavailable"); + useUIStore.getState().showError(ACP); + vi.advanceTimersByTime(60_000); + expect(useUIStore.getState().errorMsg).toBe(ACP); + }); +}); diff --git a/web/src/components/app/AppFeedback.tsx b/web/src/components/app/AppFeedback.tsx index 9b8278203..21cd86203 100644 --- a/web/src/components/app/AppFeedback.tsx +++ b/web/src/components/app/AppFeedback.tsx @@ -1,4 +1,7 @@ +import { useState } from "react"; import { useTranslation } from "react-i18next"; +import { type CommandError, commandInError } from "../../utils/commandError"; +import { copyTextToClipboard } from "../../utils/copy"; type AppNotice = { message: string; actionLabel?: string; onAction?: () => void }; @@ -35,6 +38,52 @@ export function FeedbackText({ text }: { text: string }) { ); } +/** + * An error whose fix is a command: says what to do in plain words, shows the command on its own + * line with a key that copies it, and keeps the original message one click away. + */ +function CommandErrorBody({ error, original }: { error: CommandError; original: string }) { + const { t } = useTranslation("layout"); + const [copied, setCopied] = useState<"" | "done" | "failed">(""); + const copy = async () => { + setCopied((await copyTextToClipboard(error.command)) ? "done" : "failed"); + }; + return ( +
+

+ {t( + error.kind === "antigravity_acp_login" + ? "commandError.antigravityAcpLogin" + : "commandError.generic", + )} +

+
+ + {error.command} + + +
+
+ {t("commandError.details")} +

{original}

+
+
+ ); +} + type AppFeedbackProps = { webReadOnly: boolean; errorMsg: string; @@ -52,6 +101,8 @@ export function AppFeedback({ }: AppFeedbackProps) { const { t } = useTranslation(["layout", "common"]); + const commandError = errorMsg ? commandInError(errorMsg) : null; + const handleNoticeAction = () => { if (!notice?.onAction) return; notice.onAction(); @@ -66,12 +117,16 @@ export function AppFeedback({
{errorMsg ? (
- - - + {commandError ? ( + + ) : ( + + + + )}