Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { ThreadId } from "@t3tools/contracts";
4
5import {
6 commandDetailRepeatsCommand,
7 extractCommandOutputText,
8 resolveViewedImageAsset,
9 resolveWorkEntryToolPresentation,
10 summarizeToolGroup,
11 toolGroupAction,
12 toolGroupSummaryKind,
13 type WorkLogPresentationEntry,
14 workEntryViewedImagePath,
15} from "./presentation.js";
16
17describe("summarizeToolGroup", () => {
18 it("deduplicates named sources ahead of ordinary actions", () => {
19 const source = { key: "browser-use:chrome", name: "Chrome", kind: "integration" as const };
20 expect(
21 summarizeToolGroup([
22 { label: "Open page", tone: "tool", toolSource: source },
23 { label: "Inspect page", tone: "tool", toolSource: source },
24 {
25 label: "Ran command",
26 tone: "tool",
27 itemType: "command_execution",
28 command: "git status",
29 },
30 ]),
31 ).toBe("Used Chrome integration and ran 1 command");
32 });
33
34 it("omits the integration suffix for special browser and computer sources", () => {
35 expect(
36 summarizeToolGroup([
37 {
38 label: "Inspect page",
39 tone: "tool",
40 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
41 },
42 {
43 label: "Click",
44 tone: "tool",
45 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
46 },
47 ]),
48 ).toBe("Used Browser and Computer Use");
49 });
50});
51
52describe("resolveWorkEntryToolPresentation", () => {
53 it.each([
54 "mcp__t3-code__preview_click",
55 "mcp__t3_code__preview_click",
56 "mcp__t3code__preview_click",
57 "T3-code.preview_click",
58 "t3-code · preview_click completed",
59 "t3_code/preview_click",
60 "preview_click",
61 ])("recognizes browser tool names across providers: %s", (label) => {
62 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
63 displayName: "Clicking in the preview browser",
64 icon: "browser",
65 });
66 });
67
68 it("uses structured MCP identity when the provider supplies a custom title", () => {
69 expect(
70 resolveWorkEntryToolPresentation({
71 label: "Tool call complete",
72 toolTitle: "Inspect the current page",
73 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
74 }),
75 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
76 });
77
78 it.each([
79 ["inProgress", "Clicking in the preview browser"],
80 ["completed", "Clicked in the preview browser"],
81 ["failed", "Failed to click in the preview browser"],
82 ["declined", "Declined to click in the preview browser"],
83 ["stopped", "Stopped clicking in the preview browser"],
84 ["unknown", "Clicking in the preview browser"],
85 ])("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
86 expect(
87 resolveWorkEntryToolPresentation({
88 label: "T3-code.preview_click",
89 toolLifecycleStatus,
90 }),
91 ).toEqual({ displayName, icon: "browser" });
92 });
93
94 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
95 const entry = { label: "T3-code.preview_click" };
96 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
97 "Clicking in the preview browser",
98 );
99 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
100 "Clicked in the preview browser",
101 );
102 expect(
103 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
104 ?.displayName,
105 ).toBe("Clicked in the preview browser");
106 expect(
107 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
108 ?.displayName,
109 ).toBe("Failed to click in the preview browser");
110 });
111
112 it.each([
113 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
114 [
115 "preview_set_appearance",
116 "Setting preview browser appearance",
117 "Set preview browser appearance",
118 ],
119 [
120 "preview_snapshot",
121 "Taking a snapshot of the preview page",
122 "Took a snapshot of the preview page",
123 ],
124 [
125 "preview_recording_stop",
126 "Stopping recording the preview browser",
127 "Stopped recording the preview browser",
128 ],
129 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
130 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
131 [
132 "t3_worktree_handoff",
133 "Handing off thread to a git worktree",
134 "Handed off thread to a git worktree",
135 ],
136 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
137 const entry = { label: `t3-code.${tool}` };
138 expect(
139 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
140 ?.displayName,
141 ).toBe(running);
142 expect(
143 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
144 ).toBe(completed);
145 });
146
147 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
148 expect(
149 resolveWorkEntryToolPresentation({
150 label: "mcp__t3_code__task_status",
151 toolTitle: "Check the child task",
152 }),
153 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
154 });
155
156 it("does not brand unknown tools or another server's matching tool name", () => {
157 for (const label of [
158 "mcp__github__preview_click",
159 "t3-code.unknown_tool",
160 "t3-code.toString",
161 "Search files",
162 ]) {
163 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
164 }
165 expect(
166 resolveWorkEntryToolPresentation({
167 label: "preview_click",
168 toolData: { server: "another-server", tool: "preview_click" },
169 }),
170 ).toBeNull();
171 });
172});
173
174describe("browser group summaries", () => {
175 const browserEntry: WorkLogPresentationEntry = {
176 label: "MCP tool call",
177 toolData: { server: "t3-code", tool: "preview_click" },
178 itemType: "mcp_tool_call",
179 toolLifecycleStatus: "completed",
180 tone: "tool",
181 };
182 const commandEntry: WorkLogPresentationEntry = {
183 label: "Ran command",
184 command: "/bin/bash -lc 'vp test run'",
185 itemType: "command_execution",
186 toolLifecycleStatus: "completed",
187 tone: "tool",
188 };
189
190 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
191 const entries = Array.from({ length: count }, (_, index) => ({
192 ...browserEntry,
193 toolCallId: `browser-${index}`,
194 }));
195 expect(summarizeToolGroup(entries)).toBe(
196 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
197 );
198 expect(toolGroupSummaryKind(entries)).toBe("browser");
199 });
200
201 it("combines command and browser counts in a single sentence", () => {
202 const entries = [
203 ...Array.from({ length: 4 }, () => commandEntry),
204 ...Array.from({ length: 15 }, () => browserEntry),
205 ];
206 expect(summarizeToolGroup(entries)).toBe("Ran 4 commands and used browser 15 times");
207 expect(toolGroupSummaryKind(entries)).toBe("mixed");
208 });
209
210 it("preserves first-seen action ordering alongside non-browser tools", () => {
211 expect(
212 summarizeToolGroup([
213 browserEntry,
214 commandEntry,
215 {
216 ...browserEntry,
217 toolData: { server: "t3-code", tool: "task_status" },
218 },
219 ]),
220 ).toBe("Used browser 1 time, ran 1 command, and used 1 tool");
221 });
222
223 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
224 expect(
225 summarizeToolGroup([
226 {
227 ...browserEntry,
228 command: "node inspect-page.js",
229 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
230 },
231 ]),
232 ).toBe("Used browser 1 time");
233 });
234
235 it("keeps foreign tools and web searches out of the browser count", () => {
236 expect(
237 summarizeToolGroup([
238 browserEntry,
239 {
240 ...browserEntry,
241 label: "preview_click",
242 toolData: { server: "another-server", tool: "preview_click" },
243 },
244 { label: "Search", tone: "tool", itemType: "web_search" },
245 ]),
246 ).toBe("Used browser 1 time, used 1 tool, and searched the web 1 time");
247 });
248
249 it("keeps browser screenshots in the browser count while preserving their image path", () => {
250 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
251 expect(summarizeToolGroup([entry])).toBe("Used browser 1 time");
252 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
253 });
254});
255
256describe("command work-log details", () => {
257 it("extracts Claude result blocks and projected output", () => {
258 expect(
259 extractCommandOutputText({
260 result: {
261 content: [
262 { type: "text", text: "first" },
263 { type: "text", text: "second" },
264 ],
265 },
266 }),
267 ).toBe("first\nsecond");
268 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
269 "projected summary",
270 );
271 });
272
273 it("only removes a detail with the matching tool-name prefix", () => {
274 expect(
275 commandDetailRepeatsCommand({
276 detail: "Bash: printf hello",
277 command: "printf hello",
278 rawCommand: null,
279 toolName: "Bash",
280 data: { toolName: "Bash", command: "printf hello" },
281 }),
282 ).toBe(true);
283 expect(
284 commandDetailRepeatsCommand({
285 detail: "warning: printf hello",
286 command: "printf hello",
287 rawCommand: null,
288 toolName: "Bash",
289 data: { toolName: "Bash", command: "printf hello" },
290 }),
291 ).toBe(false);
292 });
293
294 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
295 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
296 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
297 expect(
298 commandDetailRepeatsCommand({
299 detail: truncated,
300 command,
301 rawCommand: null,
302 toolName: "Bash",
303 data: { toolName: "Bash", command },
304 }),
305 ).toBe(true);
306 expect(
307 commandDetailRepeatsCommand({
308 detail: "Bash: printf hello...",
309 command: "printf goodbye",
310 rawCommand: null,
311 toolName: "Bash",
312 data: { toolName: "Bash", command: "printf goodbye" },
313 }),
314 ).toBe(false);
315 });
316
317 it("treats ACP command echoes as synthetic even without a tool kind", () => {
318 expect(
319 commandDetailRepeatsCommand({
320 detail: "pnpm test",
321 command: "pnpm test",
322 rawCommand: null,
323 toolName: undefined,
324 data: { toolCallId: "tool-1", command: "pnpm test" },
325 }),
326 ).toBe(true);
327 expect(
328 commandDetailRepeatsCommand({
329 detail: "pnpm test",
330 command: "pnpm test",
331 rawCommand: null,
332 toolName: undefined,
333 data: { command: "pnpm test" },
334 }),
335 ).toBe(false);
336 });
337});
338
339describe("workEntryViewedImagePath", () => {
340 const entry = { label: "Read", tone: "tool" } as const;
341
342 it("returns a single image path from supported read entries", () => {
343 expect(
344 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
345 ).toBe("assets/a.png");
346 expect(
347 workEntryViewedImagePath({
348 ...entry,
349 itemType: "dynamic_tool_call",
350 toolTitle: "Read file",
351 detail: "C:\\workspace\\a.webp",
352 }),
353 ).toBe("C:\\workspace\\a.webp");
354 expect(
355 workEntryViewedImagePath({
356 ...entry,
357 itemType: "dynamic_tool_call",
358 detail: 'Read: {"file_path":"truncated..."}',
359 viewedImagePath: " /workspace/reference image.webp ",
360 }),
361 ).toBe("/workspace/reference image.webp");
362 });
363
364 it("rejects non-image, multi-line, and non-read details", () => {
365 expect(
366 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.txt" }),
367 ).toBeNull();
368 expect(
369 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.png\nb.png" }),
370 ).toBeNull();
371 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
372 });
373});
374
375describe("toolGroupAction", () => {
376 it("groups legacy Claude image reads with other reads", () => {
377 expect(
378 toolGroupAction({
379 label: "Tool call",
380 tone: "tool",
381 itemType: "dynamic_tool_call",
382 viewedImagePath: "/workspace/reference.png",
383 }),
384 ).toBe("read");
385 });
386});
387
388describe("resolveViewedImageAsset", () => {
389 const threadId = ThreadId.make("thread-1");
390
391 it("serves t3 attachment paths in place like any other host path", () => {
392 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
393 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
394 resource: { _tag: "media-file", threadId, path },
395 alt: "11111111-1111-4111-8111-111111111111.png",
396 srcFragment: "",
397 });
398 });
399
400 it("normalizes workspace image sources", () => {
401 expect(
402 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
403 threadId,
404 workspaceRoot: "/workspace",
405 }),
406 ).toEqual({
407 resource: {
408 _tag: "media-file",
409 threadId,
410 path: "/workspace/screens/logo.svg",
411 },
412 alt: "logo.svg",
413 srcFragment: "#mark",
414 });
415 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
416 });
417});