File size: 5,853 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// @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(
      <ToolCardShell
        content={content}
        icon={<span />}
        title="Send file"
        defaultExpanded
      >
        <div>hello.txt</div>
      </ToolCardShell>,
    );

    const details = screen.getByText("hello.txt").closest("details");
    expect(details).toHaveAttribute("open");
  });

  it("keeps ordinary tool details collapsed and unmounted", () => {
    const { container } = render(
      <ToolCardShell content={content} icon={<span />} title="Ordinary tool">
        <div>raw output</div>
      </ToolCardShell>,
    );

    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(
      <ToolCardShell
        content={content}
        icon={<span />}
        title="Read file"
        summaryAction={
          <button
            type="button"
            onClick={(event) => {
              event.preventDefault();
              event.stopPropagation();
            }}
          >
            Preview
          </button>
        }
      />,
    );

    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(
      <ToolCardShell content={content} icon={<span />} 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(
      <ToolCardShell
        content={runningContent}
        icon={<span data-testid="completion-icon" />}
        title="Shell"
        isStreaming
      />,
    );
    const spinner = container.querySelector('[class*="toolCallSpinner"]');

    expect(spinner).not.toBeNull();

    rerender(
      <ToolCardShell
        content={content}
        icon={<span data-testid="completion-icon" />}
        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(
      <ToolCardShell content={content} icon={<span />} title="Shell">
        <div>Expensive output</div>
      </ToolCardShell>,
    );

    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(
      <ToolCardShell
        content={runningContent}
        icon={<span />}
        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(
      <ToolCardShell
        content={streamingInputContent}
        icon={<span />}
        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");
  });
});