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 workEntryIndicatesToolFailure,
16 workEntryDisplayIndicatesToolFailure,
17 workEntryIndicatesToolSuccess,
18} from "./presentation.js";
19
20describe("workEntryIndicatesToolFailure", () => {
21 const base = {
22 id: "w1",
23 createdAt: "2026-01-01T00:00:00.000Z",
24 label: "Read",
25 };
26
27 it("is true for error tone", () => {
28 expect(
29 workEntryIndicatesToolFailure({
30 ...base,
31 tone: "error",
32 detail: "nothing special",
33 }),
34 ).toBe(true);
35 });
36
37 it("is true when lifecycle says failed even if detail is empty", () => {
38 expect(
39 workEntryIndicatesToolFailure({
40 ...base,
41 tone: "tool",
42 toolLifecycleStatus: "failed",
43 }),
44 ).toBe(true);
45 });
46
47 it("detects file-not-found style tool output with completed lifecycle", () => {
48 expect(
49 workEntryIndicatesToolFailure({
50 ...base,
51 tone: "tool",
52 toolLifecycleStatus: "completed",
53 detail: "File not found: C:\\foo\\nonexistent.ts",
54 }),
55 ).toBe(true);
56 });
57
58 it("detects glob no files and PowerShell command errors", () => {
59 expect(
60 workEntryIndicatesToolFailure({
61 ...base,
62 label: "Glob",
63 tone: "tool",
64 detail: "No files found",
65 }),
66 ).toBe(true);
67 expect(
68 workEntryIndicatesToolFailure({
69 ...base,
70 label: "Bash",
71 tone: "tool",
72 detail:
73 "The term 'this_is_not_a_command' is not recognized as the name of a cmdlet, function, script file, or operable program.",
74 }),
75 ).toBe(true);
76 });
77
78 it("is false for successful completed tools", () => {
79 expect(
80 workEntryIndicatesToolFailure({
81 ...base,
82 tone: "tool",
83 toolLifecycleStatus: "completed",
84 detail: "Found 3 matching files",
85 }),
86 ).toBe(false);
87 });
88
89 it("does not treat error text in a command as rendered failure", () => {
90 const entry = {
91 label: "Ran command",
92 tone: "tool",
93 toolLifecycleStatus: "completed",
94 command: 'rg "file not found"',
95 detail: "Found 3 matches",
96 } satisfies WorkLogPresentationEntry;
97
98 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
99 // Older activities can store output in this field, so that path stays separate.
100 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
101 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
102 });
103
104 it("treats successful tool rows as success candidates", () => {
105 expect(
106 workEntryIndicatesToolSuccess({
107 ...base,
108 tone: "tool",
109 toolLifecycleStatus: "completed",
110 detail: "ok",
111 }),
112 ).toBe(true);
113 expect(
114 workEntryIndicatesToolSuccess({
115 ...base,
116 tone: "tool",
117 toolLifecycleStatus: "inProgress",
118 detail: "…",
119 }),
120 ).toBe(false);
121 expect(workEntryIndicatesToolSuccess({ ...base, tone: "thinking", detail: "…" })).toBe(false);
122 expect(
123 workEntryIndicatesToolSuccess({ ...base, tone: "tool", toolLifecycleStatus: "stopped" }),
124 ).toBe(false);
125 });
126
127 it("does not run heuristics on non-tool info rows", () => {
128 expect(
129 workEntryIndicatesToolFailure({
130 ...base,
131 label: "Context compacted",
132 tone: "info",
133 detail: "File not found in conversation",
134 }),
135 ).toBe(false);
136 });
137});
138
139describe("summarizeToolGroup", () => {
140 it.each(["command", "file-read", "file-change"])(
141 "keeps %s approvals out of tool execution counts",
142 (requestKind) => {
143 const approvals = [
144 {
145 label: "Approval requested",
146 sourceActivityKind: "approval.requested",
147 tone: "info",
148 requestKind,
149 },
150 {
151 label: "Approval resolved",
152 sourceActivityKind: "approval.resolved",
153 tone: "info",
154 requestKind,
155 },
156 {
157 label: "Provider approval response failed",
158 sourceActivityKind: "provider.approval.respond.failed",
159 tone: "error",
160 },
161 ] satisfies WorkLogPresentationEntry[];
162
163 expect(
164 summarizeToolGroup([
165 ...approvals,
166 { label: "Read", tone: "tool", itemType: "dynamic_tool_call" },
167 ]),
168 ).toBe("Received 3 updates and used 1 tool");
169 expect(summarizeToolGroup(approvals)).toBe("Received 3 updates");
170 expect(toolGroupSummaryKind(approvals)).toBe("update");
171 },
172 );
173
174 it("deduplicates named sources ahead of ordinary actions", () => {
175 const source = { key: "browser-use:chrome", name: "Chrome", kind: "integration" as const };
176 expect(
177 summarizeToolGroup([
178 { label: "Open page", tone: "tool", toolSource: source },
179 { label: "Inspect page", tone: "tool", toolSource: source },
180 {
181 label: "Ran command",
182 tone: "tool",
183 itemType: "command_execution",
184 command: "git status",
185 },
186 ]),
187 ).toBe("Used Chrome integration and ran 1 command");
188 });
189
190 it("omits the integration suffix for special browser and computer sources", () => {
191 expect(
192 summarizeToolGroup([
193 {
194 label: "Inspect page",
195 tone: "tool",
196 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
197 },
198 {
199 label: "Click",
200 tone: "tool",
201 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
202 },
203 ]),
204 ).toBe("Used Browser and Computer Use");
205 });
206});
207
208describe("resolveWorkEntryToolPresentation", () => {
209 it.each([
210 "mcp__t3-code__preview_click",
211 "mcp__t3_code__preview_click",
212 "mcp__t3code__preview_click",
213 "T3-code.preview_click",
214 "t3-code · preview_click completed",
215 "t3_code/preview_click",
216 "preview_click",
217 ])("recognizes browser tool names across providers: %s", (label) => {
218 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
219 displayName: "Clicking in the preview browser",
220 icon: "browser",
221 });
222 });
223
224 it("labels device tools with the device icon", () => {
225 expect(
226 resolveWorkEntryToolPresentation({
227 label: "mcp__t3-code__device_open",
228 toolLifecycleStatus: "completed",
229 }),
230 ).toEqual({ displayName: "Opened a device in the Device panel", icon: "device" });
231 expect(resolveWorkEntryToolPresentation({ label: "t3-code · device_screenshot" })).toEqual({
232 displayName: "Taking a screenshot of the device",
233 icon: "device",
234 });
235 });
236
237 it("uses structured MCP identity when the provider supplies a custom title", () => {
238 expect(
239 resolveWorkEntryToolPresentation({
240 label: "Tool call complete",
241 toolTitle: "Inspect the current page",
242 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
243 }),
244 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
245 });
246
247 it.each([
248 ["inProgress", "Clicking in the preview browser"],
249 ["completed", "Clicked in the preview browser"],
250 ["failed", "Failed to click in the preview browser"],
251 ["declined", "Declined to click in the preview browser"],
252 ["stopped", "Stopped clicking in the preview browser"],
253 ["unknown", "Clicking in the preview browser"],
254 ])("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
255 expect(
256 resolveWorkEntryToolPresentation({
257 label: "T3-code.preview_click",
258 toolLifecycleStatus,
259 }),
260 ).toEqual({ displayName, icon: "browser" });
261 });
262
263 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
264 const entry = { label: "T3-code.preview_click" };
265 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
266 "Clicking in the preview browser",
267 );
268 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
269 "Clicked in the preview browser",
270 );
271 expect(
272 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
273 ?.displayName,
274 ).toBe("Clicked in the preview browser");
275 expect(
276 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
277 ?.displayName,
278 ).toBe("Failed to click in the preview browser");
279 });
280
281 it.each([
282 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
283 [
284 "preview_set_appearance",
285 "Setting preview browser appearance",
286 "Set preview browser appearance",
287 ],
288 [
289 "preview_snapshot",
290 "Taking a snapshot of the preview page",
291 "Took a snapshot of the preview page",
292 ],
293 [
294 "preview_recording_stop",
295 "Stopping recording the preview browser",
296 "Stopped recording the preview browser",
297 ],
298 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
299 ["t3_thread_update", "Updating T3 thread metadata", "Updated T3 thread metadata"],
300 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
301 [
302 "t3_worktree_handoff",
303 "Handing off thread to a git worktree",
304 "Handed off thread to a git worktree",
305 ],
306 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
307 const entry = { label: `t3-code.${tool}` };
308 expect(
309 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
310 ?.displayName,
311 ).toBe(running);
312 expect(
313 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
314 ).toBe(completed);
315 });
316
317 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
318 expect(
319 resolveWorkEntryToolPresentation({
320 label: "mcp__t3_code__task_status",
321 toolTitle: "Check the child task",
322 }),
323 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
324 });
325
326 it("does not brand unknown tools or another server's matching tool name", () => {
327 for (const label of [
328 "mcp__github__preview_click",
329 "t3-code.unknown_tool",
330 "t3-code.toString",
331 "Search files",
332 ]) {
333 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
334 }
335 expect(
336 resolveWorkEntryToolPresentation({
337 label: "preview_click",
338 toolData: { server: "another-server", tool: "preview_click" },
339 }),
340 ).toBeNull();
341 });
342});
343
344describe("browser group summaries", () => {
345 const browserEntry: WorkLogPresentationEntry = {
346 label: "MCP tool call",
347 toolData: { server: "t3-code", tool: "preview_click" },
348 itemType: "mcp_tool_call",
349 toolLifecycleStatus: "completed",
350 tone: "tool",
351 };
352 const commandEntry: WorkLogPresentationEntry = {
353 label: "Ran command",
354 command: "/bin/bash -lc 'vp test run'",
355 itemType: "command_execution",
356 toolLifecycleStatus: "completed",
357 tone: "tool",
358 };
359
360 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
361 const entries = Array.from({ length: count }, (_, index) => ({
362 ...browserEntry,
363 toolCallId: `browser-${index}`,
364 }));
365 expect(summarizeToolGroup(entries)).toBe(
366 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
367 );
368 expect(toolGroupSummaryKind(entries)).toBe("browser");
369 });
370
371 it("combines command and browser counts in a single sentence", () => {
372 const entries = [
373 ...Array.from({ length: 4 }, () => commandEntry),
374 ...Array.from({ length: 15 }, () => browserEntry),
375 ];
376 expect(summarizeToolGroup(entries)).toBe("Ran 4 commands and used browser 15 times");
377 expect(toolGroupSummaryKind(entries)).toBe("mixed");
378 });
379
380 it("preserves first-seen action ordering alongside non-browser tools", () => {
381 expect(
382 summarizeToolGroup([
383 browserEntry,
384 commandEntry,
385 {
386 ...browserEntry,
387 toolData: { server: "t3-code", tool: "task_status" },
388 },
389 ]),
390 ).toBe("Used browser 1 time, ran 1 command, and used 1 tool");
391 });
392
393 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
394 expect(
395 summarizeToolGroup([
396 {
397 ...browserEntry,
398 command: "node inspect-page.js",
399 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
400 },
401 ]),
402 ).toBe("Used browser 1 time");
403 });
404
405 it("keeps foreign tools and web searches out of the browser count", () => {
406 expect(
407 summarizeToolGroup([
408 browserEntry,
409 {
410 ...browserEntry,
411 label: "preview_click",
412 toolData: { server: "another-server", tool: "preview_click" },
413 },
414 { label: "Search", tone: "tool", itemType: "web_search" },
415 ]),
416 ).toBe("Used browser 1 time, used 1 tool, and searched the web 1 time");
417 });
418
419 it("keeps browser screenshots in the browser count while preserving their image path", () => {
420 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
421 expect(summarizeToolGroup([entry])).toBe("Used browser 1 time");
422 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
423 });
424});
425
426describe("command work-log details", () => {
427 it("extracts Claude result blocks and projected output", () => {
428 expect(
429 extractCommandOutputText({
430 result: {
431 content: [
432 { type: "text", text: "first" },
433 { type: "text", text: "second" },
434 ],
435 },
436 }),
437 ).toBe("first\nsecond");
438 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
439 "projected summary",
440 );
441 });
442
443 it("only removes a detail with the matching tool-name prefix", () => {
444 expect(
445 commandDetailRepeatsCommand({
446 detail: "Bash: printf hello",
447 command: "printf hello",
448 rawCommand: null,
449 toolName: "Bash",
450 data: { toolName: "Bash", command: "printf hello" },
451 }),
452 ).toBe(true);
453 expect(
454 commandDetailRepeatsCommand({
455 detail: "warning: printf hello",
456 command: "printf hello",
457 rawCommand: null,
458 toolName: "Bash",
459 data: { toolName: "Bash", command: "printf hello" },
460 }),
461 ).toBe(false);
462 });
463
464 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
465 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
466 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
467 expect(
468 commandDetailRepeatsCommand({
469 detail: truncated,
470 command,
471 rawCommand: null,
472 toolName: "Bash",
473 data: { toolName: "Bash", command },
474 }),
475 ).toBe(true);
476 expect(
477 commandDetailRepeatsCommand({
478 detail: "Bash: printf hello...",
479 command: "printf goodbye",
480 rawCommand: null,
481 toolName: "Bash",
482 data: { toolName: "Bash", command: "printf goodbye" },
483 }),
484 ).toBe(false);
485 });
486
487 it("treats ACP command echoes as synthetic even without a tool kind", () => {
488 expect(
489 commandDetailRepeatsCommand({
490 detail: "pnpm test",
491 command: "pnpm test",
492 rawCommand: null,
493 toolName: undefined,
494 data: { toolCallId: "tool-1", command: "pnpm test" },
495 }),
496 ).toBe(true);
497 expect(
498 commandDetailRepeatsCommand({
499 detail: "pnpm test",
500 command: "pnpm test",
501 rawCommand: null,
502 toolName: undefined,
503 data: { command: "pnpm test" },
504 }),
505 ).toBe(false);
506 });
507});
508
509describe("workEntryViewedImagePath", () => {
510 const entry = { label: "Read", tone: "tool" } as const;
511
512 it("returns a single image path from supported read entries", () => {
513 expect(
514 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
515 ).toBe("assets/a.png");
516 expect(
517 workEntryViewedImagePath({
518 ...entry,
519 itemType: "dynamic_tool_call",
520 toolTitle: "Read file",
521 detail: "C:\\workspace\\a.webp",
522 }),
523 ).toBe("C:\\workspace\\a.webp");
524 expect(
525 workEntryViewedImagePath({
526 ...entry,
527 itemType: "dynamic_tool_call",
528 detail: 'Read: {"file_path":"truncated..."}',
529 viewedImagePath: " /workspace/reference image.webp ",
530 }),
531 ).toBe("/workspace/reference image.webp");
532 });
533
534 it("rejects non-image, multi-line, and non-read details", () => {
535 expect(
536 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.txt" }),
537 ).toBeNull();
538 expect(
539 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.png\nb.png" }),
540 ).toBeNull();
541 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
542 });
543});
544
545describe("toolGroupAction", () => {
546 it("groups legacy Claude image reads with other reads", () => {
547 expect(
548 toolGroupAction({
549 label: "Tool call",
550 tone: "tool",
551 itemType: "dynamic_tool_call",
552 viewedImagePath: "/workspace/reference.png",
553 }),
554 ).toBe("read");
555 });
556});
557
558describe("resolveViewedImageAsset", () => {
559 const threadId = ThreadId.make("thread-1");
560
561 it("serves t3 attachment paths in place like any other host path", () => {
562 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
563 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
564 resource: { _tag: "media-file", threadId, path },
565 alt: "11111111-1111-4111-8111-111111111111.png",
566 srcFragment: "",
567 });
568 });
569
570 it("normalizes workspace image sources", () => {
571 expect(
572 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
573 threadId,
574 workspaceRoot: "/workspace",
575 }),
576 ).toEqual({
577 resource: {
578 _tag: "media-file",
579 threadId,
580 path: "/workspace/screens/logo.svg",
581 },
582 alt: "logo.svg",
583 srcFragment: "#mark",
584 });
585 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
586 });
587});
588
589describe("pull request tool presentation", () => {
590 it.each([
591 "mcp__t3-code__link_pull_request",
592 "mcp__t3_code__link_pull_request",
593 "T3-code · link_pull_request",
594 "t3code/link_pull_request",
595 "link_pull_request",
596 ])("recognizes the native linking tool: %s", (label) => {
597 const entry = { label, tone: "tool" as const, toolLifecycleStatus: "completed" };
598 expect(resolveWorkEntryToolPresentation(entry)).toMatchObject({
599 displayName: "Linked a pull request",
600 icon: "pull-request",
601 });
602 expect(toolGroupAction(entry)).toBe("link-pr");
603 });
604
605 it.each([
606 ["inProgress", "Linking PR #42"],
607 ["completed", "Linked PR #42"],
608 ["failed", "Failed to link PR #42"],
609 ["declined", "Declined to link PR #42"],
610 ["stopped", "Stopped linking PR #42"],
611 ])("describes the target and %s status", (toolLifecycleStatus, displayName) => {
612 expect(
613 resolveWorkEntryToolPresentation({
614 label: "MCP tool call",
615 toolTitle: "Custom title",
616 toolLifecycleStatus,
617 toolData: {
618 server: "t3-code",
619 tool: "link_pull_request",
620 arguments: { url: "https://github.com/acme/web/pull/42" },
621 },
622 })?.displayName,
623 ).toBe(displayName);
624 });
625
626 it("recognizes unlink targets supplied as repository and number", () => {
627 expect(
628 resolveWorkEntryToolPresentation({
629 label: "MCP tool call",
630 toolLifecycleStatus: "completed",
631 toolData: {
632 toolName: "mcp__t3-code__unlink_pull_request",
633 rawInput: { repository: "acme/web", number: 42 },
634 },
635 }),
636 ).toMatchObject({ displayName: "Unlinked PR #42", icon: "pull-request", action: "unlink-pr" });
637 });
638
639 it("summarizes native PR work separately from ordinary tools and integration metadata", () => {
640 const link: WorkLogPresentationEntry = {
641 label: "T3-code · link_pull_request",
642 tone: "tool",
643 itemType: "mcp_tool_call",
644 toolLifecycleStatus: "completed",
645 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
646 };
647 const list: WorkLogPresentationEntry = {
648 ...link,
649 label: "T3-code · list_thread_pull_requests",
650 };
651 expect(summarizeToolGroup([link, link, list])).toBe(
652 "Linked 2 pull requests and checked linked pull requests",
653 );
654 expect(summarizeToolGroup([{ ...link, label: "T3-code · unlink_pull_request" }])).toBe(
655 "Unlinked 1 pull request",
656 );
657 expect(toolGroupSummaryKind([link, link, list])).toBe("pull-request");
658 expect(summarizeToolGroup([list, list])).toBe("Checked linked pull requests 2 times");
659 expect(
660 resolveWorkEntryToolPresentation({ label: "mcp__another-server__link_pull_request" }),
661 ).toBeNull();
662 });
663});
664
665describe("device group summaries", () => {
666 const deviceEntry = (tool: string): WorkLogPresentationEntry => ({
667 label: "MCP tool call",
668 toolData: { server: "t3-code", tool },
669 itemType: "mcp_tool_call",
670 toolLifecycleStatus: "completed",
671 tone: "tool",
672 });
673
674 it.each(["device_list", "device_open", "device_screenshot", "device_close"])(
675 "recognizes %s as device controls",
676 (tool) => {
677 const entry = deviceEntry(tool);
678 expect(summarizeToolGroup([entry])).toBe("Used device controls 1 time");
679 expect(toolGroupSummaryKind([entry])).toBe("device");
680 },
681 );
682
683 it("summarizes device calls alongside shell commands", () => {
684 expect(
685 summarizeToolGroup([
686 { label: "Ran command", itemType: "command_execution", command: "pwd", tone: "tool" },
687 deviceEntry("device_list"),
688 deviceEntry("device_open"),
689 ]),
690 ).toBe("Ran 1 command and used device controls 2 times");
691 });
692
693 it("recognizes Claude tool names and preserves screenshot previews", () => {
694 const entry = {
695 ...deviceEntry("device_screenshot"),
696 toolData: { toolName: "mcp__t3_code__device_screenshot" },
697 viewedImagePath: "/workspace/device.png",
698 };
699 expect(summarizeToolGroup([entry])).toBe("Used device controls 1 time");
700 expect(workEntryViewedImagePath(entry)).toBe("/workspace/device.png");
701 });
702
703 it("does not classify another server's tools as T3 device controls", () => {
704 expect(
705 summarizeToolGroup([
706 {
707 ...deviceEntry("device_open"),
708 toolData: { server: "another-server", tool: "device_open" },
709 },
710 ]),
711 ).toBe("Used 1 tool");
712 });
713});