// @vitest-environment jsdom
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));
vi.mock("./ToolCallSessionContext", () => ({
useToolCallSessionId: () => "",
}));
vi.mock("../../../../hooks/useToolCallControl", () => ({
useToolCallControl: () => ({
bannerVisible: false,
offloadRemaining: 12,
killRemaining: 30,
defaultPolicy: "keep_foreground",
maxInternalTimeoutSecs: null,
elapsed: 0,
toggleBanner: vi.fn(),
closeBanner: vi.fn(),
updateRemaining: vi.fn(),
}),
}));
vi.mock("./ToolCallControlPopover", () => ({
OffloadBanner: () => null,
}));
import ToolCardShell from "./ToolCardShell";
import type { ToolCallContent } from "./types";
const content: ToolCallContent = {
type: "tool_call",
id: "call-1",
name: "execute_shell_command",
params: {},
result: "output",
status: "done",
};
const runningContent: ToolCallContent = {
...content,
params: { command: "python verbose_script.py" },
status: "calling",
};
const streamingInputContent: ToolCallContent = {
...runningContent,
inputProgress: {
preview: '{"path":"notes.txt"}',
truncated: false,
},
};
describe("ToolCardShell lazy body", () => {
it("opens file-facing results by default when requested", () => {
render(
}
title="Send file"
defaultExpanded
>
hello.txt
,
);
const details = screen.getByText("hello.txt").closest("details");
expect(details).toHaveAttribute("open");
});
it("keeps ordinary tool details collapsed and unmounted", () => {
const { container } = render(
} title="Ordinary tool">
raw output
,
);
expect(container.querySelector("details")).not.toHaveAttribute("open");
expect(screen.queryByText("raw output")).not.toBeInTheDocument();
});
it("does not toggle the tool when its summary action is clicked", () => {
const { container } = render(
}
title="Read file"
summaryAction={
}
/>,
);
fireEvent.click(screen.getByRole("button", { name: "Preview" }));
expect(container.querySelector("details")).not.toHaveAttribute("open");
});
it("keeps the full tool title available when the label is truncated", () => {
const title = `Run ${"long-command-argument ".repeat(40)}`;
const { container } = render(
} title={title} />,
);
const label = container.querySelector(`[title]`);
expect(label).not.toBeNull();
expect(label).toHaveAttribute("title", title);
expect(label).toHaveTextContent(title.trim());
});
it("replaces the spinner node when the tool finishes", () => {
const { container, rerender } = render(
}
title="Shell"
isStreaming
/>,
);
const spinner = container.querySelector('[class*="toolCallSpinner"]');
expect(spinner).not.toBeNull();
rerender(
}
title="Shell"
/>,
);
const completionIcon = screen.getByTestId("completion-icon");
expect(spinner).not.toBeInTheDocument();
expect(completionIcon.parentElement).not.toBe(spinner);
});
it("mounts the body only after the first expansion", () => {
const { container } = render(
} title="Shell">
Expensive output
,
);
expect(screen.queryByText("Expensive output")).not.toBeInTheDocument();
const details = container.querySelector("details");
expect(details).not.toBeNull();
details!.open = true;
fireEvent(details!, new Event("toggle"));
expect(screen.getByText("Expensive output")).toBeInTheDocument();
details!.open = false;
fireEvent(details!, new Event("toggle"));
expect(screen.getByText("Expensive output")).toBeInTheDocument();
});
it("groups Parameters and Runtime in one metadata panel", () => {
const { container } = render(
}
title="Shell"
isStreaming
defaultExpanded
/>,
);
const metadata = container.querySelector('[class*="toolCallMetadata"]');
expect(metadata).not.toBeNull();
expect(metadata).toHaveTextContent("Parameters");
expect(metadata).toHaveTextContent("Runtime");
});
it("keeps streaming input quiet in the summary and preserves its preview", () => {
const { container } = render(
}
title="Read file"
isStreaming
defaultExpanded
/>,
);
const summary = container.querySelector("summary");
expect(summary).toHaveTextContent("tool.loading");
expect(summary).not.toHaveTextContent("tool.inputProgress");
const previewTitle = screen.getByText("tool.rawInputPreview");
const previewBlock = previewTitle.parentElement?.parentElement;
expect(previewBlock).toHaveTextContent("path");
expect(previewBlock).toHaveTextContent("notes.txt");
});
});