Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { ThreadId, TurnItemId, type OrchestrationV2TurnItem } from "@t3tools/contracts";
4import * as DateTime from "effect/DateTime";
5import { T3_MCP_TOOL_NAMES } from "@t3tools/shared/t3McpToolPresentation";
6
7import {
8 commandDetailRepeatsCommand,
9 extractCommandOutputText,
10 resolveViewedImageAsset,
11 resolveWorkEntryToolPresentation,
12 summarizeToolGroup,
13 toolGroupAction,
14 toolGroupSummaryKind,
15 toolItemForDisplay,
16 type WorkLogPresentationEntry,
17 type WorkLogToolLifecycleStatus,
18 workEntryViewedImagePath,
19 workEntryIndicatesToolFailure,
20 workEntryDisplayIndicatesToolFailure,
21 workEntryIndicatesToolSuccess,
22} from "./presentation.js";
23
24function commandItem(
25 fields: Partial<Extract<OrchestrationV2TurnItem, { type: "command_execution" }>> = {},
26): OrchestrationV2TurnItem {
27 return {
28 id: TurnItemId.make("command"),
29 threadId: ThreadId.make("thread"),
30 runId: null,
31 nodeId: null,
32 providerThreadId: null,
33 providerTurnId: null,
34 nativeItemRef: null,
35 parentItemId: null,
36 ordinal: 1,
37 status: "completed",
38 title: null,
39 startedAt: null,
40 completedAt: null,
41 updatedAt: DateTime.makeUnsafe("2026-09-08T00:00:00.000Z"),
42 type: "command_execution",
43 input: 'rg "command not found"',
44 exitCode: 0,
45 ...fields,
46 };
47}
48
49describe("workEntryIndicatesToolFailure", () => {
50 const base = {
51 id: "w1",
52 createdAt: "2026-01-01T00:00:00.000Z",
53 label: "Read",
54 };
55
56 it.each([
57 [{ outputIndicatesFailure: true }, true],
58 [{ exitCode: 2 }, true],
59 [{ output: "sh: missing-command: command not found" }, true],
60 [{ output: "Found 3 matches" }, false],
61 [{ output: `${"x".repeat(32_768)} command not found` }, false],
62 [{}, false],
63 ] as const)(
64 "preserves command failure state after removing displayed output: %j",
65 (fields, failed) => {
66 const structuredPayload = commandItem(fields);
67 const entry: WorkLogPresentationEntry = {
68 ...base,
69 tone: "tool",
70 itemType: "command_execution",
71 toolLifecycleStatus: "completed",
72 structuredPayload,
73 };
74 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(failed);
75 expect(workEntryIndicatesToolSuccess(entry)).toBe(!failed);
76 expect(JSON.stringify(toolItemForDisplay(structuredPayload))).not.toContain('"output":');
77 },
78 );
79
80 it("is true for error tone", () => {
81 expect(
82 workEntryIndicatesToolFailure({
83 ...base,
84 tone: "error",
85 detail: "nothing special",
86 }),
87 ).toBe(true);
88 });
89
90 it("is true when lifecycle says failed even if detail is empty", () => {
91 expect(
92 workEntryIndicatesToolFailure({
93 ...base,
94 tone: "tool",
95 toolLifecycleStatus: "failed",
96 }),
97 ).toBe(true);
98 });
99
100 it("detects file-not-found style tool output with completed lifecycle", () => {
101 expect(
102 workEntryIndicatesToolFailure({
103 ...base,
104 tone: "tool",
105 toolLifecycleStatus: "completed",
106 detail: "File not found: C:\\foo\\nonexistent.ts",
107 }),
108 ).toBe(true);
109 });
110
111 it("detects glob no files and PowerShell command errors", () => {
112 expect(
113 workEntryIndicatesToolFailure({
114 ...base,
115 label: "Glob",
116 tone: "tool",
117 detail: "No files found",
118 }),
119 ).toBe(true);
120 expect(
121 workEntryIndicatesToolFailure({
122 ...base,
123 label: "Bash",
124 tone: "tool",
125 detail:
126 "The term 'this_is_not_a_command' is not recognized as the name of a cmdlet, function, script file, or operable program.",
127 }),
128 ).toBe(true);
129 });
130
131 it("is false for successful completed tools", () => {
132 expect(
133 workEntryIndicatesToolFailure({
134 ...base,
135 tone: "tool",
136 toolLifecycleStatus: "completed",
137 detail: "Found 3 matching files",
138 }),
139 ).toBe(false);
140 });
141
142 it("does not treat error text in a command as rendered failure", () => {
143 const entry = {
144 ...base,
145 label: "Ran command",
146 tone: "tool",
147 toolLifecycleStatus: "completed",
148 command: 'rg "file not found"',
149 detail: "Found 3 matches",
150 } satisfies WorkLogPresentationEntry;
151
152 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
153 // Older activities can store output in this field, so that path stays separate.
154 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
155 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
156 });
157
158 it("treats successful tool rows as success candidates", () => {
159 expect(
160 workEntryIndicatesToolSuccess({
161 ...base,
162 tone: "tool",
163 toolLifecycleStatus: "completed",
164 detail: "ok",
165 }),
166 ).toBe(true);
167 expect(
168 workEntryIndicatesToolSuccess({
169 ...base,
170 tone: "tool",
171 toolLifecycleStatus: "inProgress",
172 detail: "…",
173 }),
174 ).toBe(false);
175 expect(workEntryIndicatesToolSuccess({ ...base, tone: "thinking", detail: "…" })).toBe(false);
176 expect(
177 workEntryIndicatesToolSuccess({ ...base, tone: "tool", toolLifecycleStatus: "stopped" }),
178 ).toBe(false);
179 expect(
180 workEntryIndicatesToolSuccess({ ...base, tone: "tool", toolLifecycleStatus: "idle" }),
181 ).toBe(false);
182 });
183
184 it("does not run heuristics on non-tool info rows", () => {
185 expect(
186 workEntryIndicatesToolFailure({
187 ...base,
188 label: "Context compacted",
189 tone: "info",
190 detail: "File not found in conversation",
191 }),
192 ).toBe(false);
193 });
194});
195
196describe("summarizeToolGroup", () => {
197 const entry = (
198 id: string,
199 overrides: Partial<WorkLogPresentationEntry>,
200 ): WorkLogPresentationEntry => ({
201 id,
202 createdAt: "2026-09-01T00:00:00Z",
203 label: "Tool call",
204 tone: "tool",
205 ...overrides,
206 });
207
208 it("excludes reasoning from mixed tool counts and icons", () => {
209 const thought = entry("thought", {
210 itemType: "reasoning",
211 tone: "thinking",
212 detail: "Check the source",
213 });
214 const command = entry("command", { itemType: "command_execution", command: "vp test run" });
215 expect(summarizeToolGroup([thought, command, { ...thought, id: "thought-2" }])).toEqual({
216 summary: "Ran 1 command",
217 hasFailure: false,
218 });
219 expect(toolGroupSummaryKind([thought, command])).toBe("command");
220 expect(summarizeToolGroup([thought]).summary).toBe("Thought");
221 expect(summarizeToolGroup([thought, { ...thought, id: "thought-2" }]).summary).toBe(
222 "Thought (×2)",
223 );
224 expect(toolGroupSummaryKind([thought])).toBe("reasoning");
225 });
226
227 it("counts created threads alongside adjacent commands", () => {
228 expect(
229 summarizeToolGroup([
230 entry("command", { itemType: "command_execution", command: "vp test run" }),
231 entry("created", {
232 itemType: "thread_created",
233
234 label: "Created thread",
235 }),
236 ]).summary,
237 ).toBe("Ran 1 command and created 1 thread");
238 });
239
240 it("deduplicates named sources ahead of ordinary actions", () => {
241 const source = { key: "browser-use:chrome", name: "Chrome", kind: "integration" as const };
242 expect(
243 summarizeToolGroup([
244 entry("open", { label: "Open page", toolSource: source }),
245 entry("inspect", { label: "Inspect page", toolSource: source }),
246 entry("command", {
247 label: "Ran command",
248 itemType: "command_execution",
249 command: "git status",
250 }),
251 ]).summary,
252 ).toBe("Used Chrome integration and ran 1 command");
253 });
254
255 it("omits the integration suffix for special browser and computer sources", () => {
256 expect(
257 summarizeToolGroup([
258 entry("inspect", {
259 label: "Inspect page",
260 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
261 }),
262 entry("click", {
263 label: "Click",
264 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
265 }),
266 ]).summary,
267 ).toBe("Used Browser and Computer Use");
268 });
269});
270
271describe("resolveWorkEntryToolPresentation", () => {
272 it("presents and summarizes every T3 tool using the same structured identity", () => {
273 for (const tool of T3_MCP_TOOL_NAMES) {
274 const entry: WorkLogPresentationEntry = {
275 id: tool,
276 createdAt: "2026-09-19T00:00:00.000Z",
277 tone: "tool",
278 label: "Custom provider title",
279 toolData: { server: "t3-code", tool },
280 toolLifecycleStatus: "completed",
281 itemType: "dynamic_tool",
282 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
283 };
284 const presentation = resolveWorkEntryToolPresentation(entry);
285 expect(presentation, tool).not.toBeNull();
286 expect(presentation?.displayName, tool).not.toContain(tool);
287 const summary = summarizeToolGroup([entry]);
288 expect(summary.summary, tool).not.toMatch(/Used (?:1 tool|T3 Code integration)/);
289 expect(summary.hasFailure, tool).toBe(false);
290 const failed = { ...entry, toolLifecycleStatus: "failed" as const };
291 expect(resolveWorkEntryToolPresentation(failed)?.displayName, tool).toMatch(/^Failed to /);
292 expect(summarizeToolGroup([failed]).hasFailure, tool).toBe(true);
293 expect(summarizeToolGroup([failed]).summary, tool).toMatch(
294 /^(?:Tried to |Requested thread creation)/,
295 );
296 }
297 });
298
299 it.each([
300 ["t3_project_list", "Listing projects", "Listed projects"],
301 ["t3_project_clone", "Cloning a repository", "Cloned a repository"],
302 ["t3_project_create", "Registering a project", "Registered a project"],
303 ["t3_thread_launch", "Launching a project thread", "Launched a project thread"],
304 ["t3_queue_edit", "Editing a queued message", "Edited a queued message"],
305 ["t3_pending_request_respond", "Answering pending questions", "Answered pending questions"],
306 ["t3_thread_configure", "Setting thread model", "Set thread model"],
307 ["t3_thread_fork", "Forking this thread", "Requested a fork of this thread"],
308 ["t3_thread_send_attachments", "Sending attachments", "Sent attachments"],
309 ["run_scheduled_task_now", "Running a scheduled task", "Requested a run of a scheduled task"],
310 ])("labels %s through its lifecycle", (tool, running, completed) => {
311 expect(resolveWorkEntryToolPresentation({ label: `T3-code.${tool}` })?.displayName).toBe(
312 running,
313 );
314 expect(
315 resolveWorkEntryToolPresentation({
316 label: `T3-code.${tool}`,
317 toolLifecycleStatus: "completed",
318 })?.displayName,
319 ).toBe(completed);
320 });
321
322 it("summarizes project tools from MCP arguments and results without claiming failed effects", () => {
323 const entry: WorkLogPresentationEntry = {
324 id: "clone",
325 createdAt: "2026-09-19T00:00:00.000Z",
326 tone: "tool",
327 label: "Custom title",
328 itemType: "dynamic_tool",
329 toolLifecycleStatus: "completed",
330 toolData: {
331 server: "t3-code",
332 tool: "t3_project_clone",
333 arguments: { url: "https://github.com/acme/repo" },
334 result: { cwd: "/tmp/repo" },
335 },
336 };
337 const list = { ...entry, toolData: { server: "t3-code", tool: "t3_project_list" } };
338 expect(summarizeToolGroup([list, entry])).toEqual({
339 summary: "Listed projects 1 time and cloned 1 repository",
340 hasFailure: false,
341 });
342 const failed = {
343 ...entry,
344 toolData: { toolName: "T3-code.t3_project_clone", rawOutput: { isError: true } },
345 };
346 expect(summarizeToolGroup([entry, failed])).toEqual({
347 summary: "Cloned 1 repository",
348 hasFailure: true,
349 });
350 });
351
352 it("does not summarize a foreign structured identity as T3 work", () => {
353 const entry: WorkLogPresentationEntry = {
354 id: "foreign",
355 createdAt: "2026-09-19T00:00:00.000Z",
356 tone: "tool",
357 label: "t3_project_clone",
358 toolLifecycleStatus: "completed",
359 toolData: { server: "another-server", tool: "t3_project_clone" },
360 };
361 expect(summarizeToolGroup([entry]).summary).toBe("Used 1 tool");
362 });
363
364 it("shows returned MCP errors as failures even in the live activity row", () => {
365 const entry: WorkLogPresentationEntry = {
366 id: "clone",
367 createdAt: "2026-09-19T00:00:00.000Z",
368 tone: "tool",
369 label: "T3-code.t3_project_clone",
370 toolLifecycleStatus: "inProgress",
371 itemType: "dynamic_tool",
372 toolData: { output: { isError: true } },
373 };
374 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
375 "Failed to clone a repository",
376 );
377 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
378 expect(workEntryIndicatesToolSuccess(entry)).toBe(false);
379 const childFailure = {
380 ...entry,
381 label: "T3-code.task_status",
382 toolLifecycleStatus: "completed" as const,
383 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
384 };
385 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
386 expect(workEntryIndicatesToolSuccess(childFailure)).toBe(true);
387 });
388 it.each([
389 "mcp__t3-code__preview_click",
390 "mcp__t3_code__preview_click",
391 "mcp__t3code__preview_click",
392 "T3-code.preview_click",
393 "t3-code · preview_click completed",
394 "t3_code/preview_click",
395 "preview_click",
396 ])("recognizes browser tool names across providers: %s", (label) => {
397 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
398 displayName: "Clicking in the preview browser",
399 icon: "browser",
400 });
401 });
402
403 it("labels device tools with the device icon", () => {
404 expect(
405 resolveWorkEntryToolPresentation({
406 label: "mcp__t3-code__device_open",
407 toolLifecycleStatus: "completed",
408 }),
409 ).toEqual({ displayName: "Opened a device in the Device panel", icon: "device" });
410 expect(resolveWorkEntryToolPresentation({ label: "t3-code · device_screenshot" })).toEqual({
411 displayName: "Taking a screenshot of the device",
412 icon: "device",
413 });
414 });
415
416 it("uses structured MCP identity when the provider supplies a custom title", () => {
417 expect(
418 resolveWorkEntryToolPresentation({
419 label: "Tool call complete",
420 toolTitle: "Inspect the current page",
421 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
422 }),
423 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
424 });
425
426 it.each([
427 ["inProgress", "Clicking in the preview browser"],
428 ["completed", "Clicked in the preview browser"],
429 ["failed", "Failed to click in the preview browser"],
430 ["declined", "Declined to click in the preview browser"],
431 ["stopped", "Stopped clicking in the preview browser"],
432 ["unknown", "Clicking in the preview browser"],
433 ] as const)("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
434 expect(
435 resolveWorkEntryToolPresentation({
436 label: "T3-code.preview_click",
437 toolLifecycleStatus: toolLifecycleStatus as WorkLogToolLifecycleStatus,
438 }),
439 ).toEqual({ displayName, icon: "browser" });
440 });
441
442 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
443 const entry = { label: "T3-code.preview_click" };
444 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
445 "Clicking in the preview browser",
446 );
447 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
448 "Clicked in the preview browser",
449 );
450 expect(
451 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
452 ?.displayName,
453 ).toBe("Clicked in the preview browser");
454 expect(
455 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
456 ?.displayName,
457 ).toBe("Failed to click in the preview browser");
458 });
459
460 it.each([
461 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
462 [
463 "preview_set_appearance",
464 "Setting preview browser appearance",
465 "Set preview browser appearance",
466 ],
467 [
468 "preview_snapshot",
469 "Taking a snapshot of the preview page",
470 "Took a snapshot of the preview page",
471 ],
472 [
473 "preview_recording_stop",
474 "Stopping recording the preview browser",
475 "Stopped recording the preview browser",
476 ],
477 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
478 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
479 [
480 "t3_worktree_handoff",
481 "Handing off thread to a git worktree",
482 "Handed off thread to a git worktree",
483 ],
484 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
485 const entry = { label: `t3-code.${tool}` };
486 expect(
487 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
488 ?.displayName,
489 ).toBe(running);
490 expect(
491 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
492 ).toBe(completed);
493 });
494
495 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
496 expect(
497 resolveWorkEntryToolPresentation({
498 label: "mcp__t3_code__task_status",
499 toolTitle: "Check the child task",
500 }),
501 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
502 });
503
504 it("does not brand unknown tools or another server's matching tool name", () => {
505 for (const label of [
506 "mcp__github__preview_click",
507 "t3-code.unknown_tool",
508 "t3-code.toString",
509 "Search files",
510 ]) {
511 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
512 }
513 expect(
514 resolveWorkEntryToolPresentation({
515 label: "preview_click",
516 toolData: { server: "another-server", tool: "preview_click" },
517 }),
518 ).toBeNull();
519 });
520});
521
522describe("browser group summaries", () => {
523 const summarizeGroupLabel = (entries: ReadonlyArray<WorkLogPresentationEntry>) =>
524 summarizeToolGroup(entries).summary;
525 const browserEntry: WorkLogPresentationEntry = {
526 id: "browser",
527 createdAt: "2026-09-01T00:00:00Z",
528 label: "MCP tool call",
529 toolData: { server: "t3-code", tool: "preview_click" },
530 itemType: "dynamic_tool",
531 toolLifecycleStatus: "completed",
532 tone: "tool",
533 };
534 const commandEntry: WorkLogPresentationEntry = {
535 id: "command",
536 createdAt: "2026-09-01T00:00:00Z",
537 label: "Ran command",
538 command: "/bin/bash -lc 'vp test run'",
539 itemType: "command_execution",
540 toolLifecycleStatus: "completed",
541 tone: "tool",
542 };
543
544 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
545 const entries = Array.from({ length: count }, (_, index) => ({
546 ...browserEntry,
547 toolCallId: `browser-${index}`,
548 }));
549 expect(summarizeGroupLabel(entries)).toBe(
550 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
551 );
552 expect(toolGroupSummaryKind(entries)).toBe("browser");
553 });
554
555 it("combines command and browser counts in a single sentence", () => {
556 const entries = [
557 ...Array.from({ length: 4 }, () => commandEntry),
558 ...Array.from({ length: 15 }, () => browserEntry),
559 ];
560 expect(summarizeGroupLabel(entries)).toBe("Ran 4 commands and used browser 15 times");
561 expect(toolGroupSummaryKind(entries)).toBe("mixed");
562 });
563
564 it("preserves first-seen action ordering alongside non-browser tools", () => {
565 expect(
566 summarizeGroupLabel([
567 browserEntry,
568 commandEntry,
569 {
570 ...browserEntry,
571 toolData: { server: "t3-code", tool: "task_status" },
572 },
573 ]),
574 ).toBe("Used browser 1 time, ran 1 command, and performed 1 other action");
575 });
576
577 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
578 expect(
579 summarizeGroupLabel([
580 {
581 ...browserEntry,
582 command: "node inspect-page.js",
583 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
584 },
585 ]),
586 ).toBe("Used browser 1 time");
587 });
588
589 it("keeps foreign tools and web searches out of the browser count", () => {
590 expect(
591 summarizeGroupLabel([
592 browserEntry,
593 {
594 ...browserEntry,
595 label: "preview_click",
596 toolData: { server: "another-server", tool: "preview_click" },
597 },
598 {
599 id: "search",
600 createdAt: "2026-09-01T00:00:00Z",
601 label: "Search",
602 tone: "tool",
603 itemType: "web_search",
604 },
605 ]),
606 ).toBe("Used browser 1 time, searched the web 1 time, and performed 1 other action");
607 });
608
609 it("keeps browser screenshots in the browser count while preserving their image path", () => {
610 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
611 expect(summarizeGroupLabel([entry])).toBe("Used browser 1 time");
612 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
613 });
614});
615
616describe("command work-log details", () => {
617 it("extracts Claude result blocks and projected output", () => {
618 expect(
619 extractCommandOutputText({
620 result: {
621 content: [
622 { type: "text", text: "first" },
623 { type: "text", text: "second" },
624 ],
625 },
626 }),
627 ).toBe("first\nsecond");
628 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
629 "projected summary",
630 );
631 });
632
633 it("only removes a detail with the matching tool-name prefix", () => {
634 expect(
635 commandDetailRepeatsCommand({
636 detail: "Bash: printf hello",
637 command: "printf hello",
638 rawCommand: null,
639 toolName: "Bash",
640 data: { toolName: "Bash", command: "printf hello" },
641 }),
642 ).toBe(true);
643 expect(
644 commandDetailRepeatsCommand({
645 detail: "warning: printf hello",
646 command: "printf hello",
647 rawCommand: null,
648 toolName: "Bash",
649 data: { toolName: "Bash", command: "printf hello" },
650 }),
651 ).toBe(false);
652 });
653
654 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
655 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
656 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
657 expect(
658 commandDetailRepeatsCommand({
659 detail: truncated,
660 command,
661 rawCommand: null,
662 toolName: "Bash",
663 data: { toolName: "Bash", command },
664 }),
665 ).toBe(true);
666 expect(
667 commandDetailRepeatsCommand({
668 detail: "Bash: printf hello...",
669 command: "printf goodbye",
670 rawCommand: null,
671 toolName: "Bash",
672 data: { toolName: "Bash", command: "printf goodbye" },
673 }),
674 ).toBe(false);
675 });
676
677 it("treats ACP command echoes as synthetic even without a tool kind", () => {
678 expect(
679 commandDetailRepeatsCommand({
680 detail: "pnpm test",
681 command: "pnpm test",
682 rawCommand: null,
683 toolName: undefined,
684 data: { toolCallId: "tool-1", command: "pnpm test" },
685 }),
686 ).toBe(true);
687 expect(
688 commandDetailRepeatsCommand({
689 detail: "pnpm test",
690 command: "pnpm test",
691 rawCommand: null,
692 toolName: undefined,
693 data: { command: "pnpm test" },
694 }),
695 ).toBe(false);
696 });
697});
698
699describe("workEntryViewedImagePath", () => {
700 const entry = {
701 id: "entry-1",
702 createdAt: "2026-01-01T00:00:00Z",
703 label: "Read",
704 tone: "tool",
705 } as const;
706
707 it("returns a single image path from supported read entries", () => {
708 expect(
709 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
710 ).toBe("assets/a.png");
711 expect(
712 workEntryViewedImagePath({
713 ...entry,
714 itemType: "dynamic_tool",
715 toolTitle: "Read file",
716 detail: "C:\\workspace\\a.webp",
717 }),
718 ).toBe("C:\\workspace\\a.webp");
719 expect(
720 workEntryViewedImagePath({
721 ...entry,
722 itemType: "dynamic_tool",
723 detail: 'Read: {"file_path":"truncated..."}',
724 viewedImagePath: " /workspace/reference image.webp ",
725 }),
726 ).toBe("/workspace/reference image.webp");
727 });
728
729 it("rejects non-image, multi-line, and non-read details", () => {
730 expect(
731 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: "a.txt" }),
732 ).toBeNull();
733 expect(
734 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: "a.png\nb.png" }),
735 ).toBeNull();
736 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
737 });
738});
739
740describe("toolGroupAction", () => {
741 it("groups legacy Claude image reads with other reads", () => {
742 expect(
743 toolGroupAction({
744 id: "legacy-read",
745 createdAt: "2026-09-01T00:00:00Z",
746 label: "Tool call",
747 tone: "tool",
748 itemType: "dynamic_tool",
749 viewedImagePath: "/workspace/reference.png",
750 }),
751 ).toBe("read");
752 });
753
754 it("groups Claude Read and Grep from toolName, not file contents", () => {
755 expect(
756 toolGroupAction({
757 id: "read",
758 createdAt: "2026-09-01T00:00:00Z",
759 label: "Read",
760 tone: "tool",
761 itemType: "dynamic_tool",
762 toolTitle: "Read",
763 toolData: { input: { file_path: "src/env.ts" } },
764 structuredPayload: {
765 type: "dynamic_tool",
766 toolName: "Read",
767 input: { file_path: "src/env.ts" },
768 } as NonNullable<WorkLogPresentationEntry["structuredPayload"]>,
769 }),
770 ).toBe("read");
771 expect(
772 toolGroupAction({
773 id: "grep",
774 createdAt: "2026-09-01T00:00:00Z",
775 label: "Grep",
776 tone: "tool",
777 itemType: "dynamic_tool",
778 toolTitle: "Grep",
779 toolData: { input: { pattern: "TODO", path: "apps/web" } },
780 structuredPayload: {
781 type: "dynamic_tool",
782 toolName: "Grep",
783 input: { pattern: "TODO", path: "apps/web" },
784 } as NonNullable<WorkLogPresentationEntry["structuredPayload"]>,
785 }),
786 ).toBe("code-search");
787 });
788});
789
790describe("resolveViewedImageAsset", () => {
791 const threadId = ThreadId.make("thread-1");
792
793 it("serves t3 attachment paths in place like any other host path", () => {
794 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
795 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
796 resource: { _tag: "media-file", threadId, path },
797 alt: "11111111-1111-4111-8111-111111111111.png",
798 srcFragment: "",
799 });
800 });
801
802 it("normalizes workspace image sources", () => {
803 expect(
804 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
805 threadId,
806 workspaceRoot: "/workspace",
807 }),
808 ).toEqual({
809 resource: {
810 _tag: "media-file",
811 threadId,
812 path: "/workspace/screens/logo.svg",
813 },
814 alt: "logo.svg",
815 srcFragment: "#mark",
816 });
817 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
818 });
819});
820
821describe("pull request tool presentation", () => {
822 it.each([
823 "mcp__t3-code__link_pull_request",
824 "mcp__t3_code__link_pull_request",
825 "T3-code · link_pull_request",
826 "t3code/link_pull_request",
827 "link_pull_request",
828 ])("recognizes the native linking tool: %s", (label) => {
829 const entry: WorkLogPresentationEntry = {
830 id: "link",
831 createdAt: "2026-09-10T00:00:00.000Z",
832 label,
833 tone: "tool",
834 toolLifecycleStatus: "completed",
835 };
836 expect(resolveWorkEntryToolPresentation(entry)).toMatchObject({
837 displayName: "Linked a pull request",
838 icon: "pull-request",
839 });
840 expect(toolGroupAction(entry)).toBe("link-pr");
841 });
842
843 it.each([
844 ["inProgress", "Linking PR #42"],
845 ["completed", "Linked PR #42"],
846 ["failed", "Failed to link PR #42"],
847 ["declined", "Declined to link PR #42"],
848 ["stopped", "Stopped linking PR #42"],
849 ] as const)("describes the target and %s status", (toolLifecycleStatus, displayName) => {
850 expect(
851 resolveWorkEntryToolPresentation({
852 label: "MCP tool call",
853 toolTitle: "Custom title",
854 toolLifecycleStatus,
855 toolData: {
856 server: "t3-code",
857 tool: "link_pull_request",
858 arguments: { url: "https://github.com/acme/web/pull/42" },
859 },
860 })?.displayName,
861 ).toBe(displayName);
862 });
863
864 it("recognizes unlink targets supplied as repository and number", () => {
865 expect(
866 resolveWorkEntryToolPresentation({
867 label: "MCP tool call",
868 toolLifecycleStatus: "completed",
869 toolData: {
870 toolName: "mcp__t3-code__unlink_pull_request",
871 rawInput: { repository: "acme/web", number: 42 },
872 },
873 }),
874 ).toMatchObject({ displayName: "Unlinked PR #42", icon: "pull-request", action: "unlink-pr" });
875 });
876
877 it("summarizes native PR work separately from ordinary tools and integration metadata", () => {
878 const link: WorkLogPresentationEntry = {
879 id: "link",
880 createdAt: "2026-09-10T00:00:00.000Z",
881 label: "T3-code · link_pull_request",
882 tone: "tool",
883 itemType: "dynamic_tool",
884 toolLifecycleStatus: "completed",
885 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
886 };
887 const list: WorkLogPresentationEntry = {
888 ...link,
889 label: "T3-code · list_thread_pull_requests",
890 };
891 expect(summarizeToolGroup([link, link, list]).summary).toBe(
892 "Linked 2 pull requests and checked linked pull requests",
893 );
894 expect(summarizeToolGroup([{ ...link, label: "T3-code · unlink_pull_request" }]).summary).toBe(
895 "Unlinked 1 pull request",
896 );
897 expect(toolGroupSummaryKind([link, link, list])).toBe("pull-request");
898 expect(summarizeToolGroup([list, list]).summary).toBe("Checked linked pull requests 2 times");
899 expect(
900 resolveWorkEntryToolPresentation({ label: "mcp__another-server__link_pull_request" }),
901 ).toBeNull();
902 });
903});
904
905describe("device group summaries", () => {
906 const deviceEntry = (tool: string): WorkLogPresentationEntry => ({
907 id: tool,
908 createdAt: "2026-09-10T00:00:00.000Z",
909 label: "MCP tool call",
910 toolData: { server: "t3-code", tool },
911 itemType: "dynamic_tool",
912 toolLifecycleStatus: "completed",
913 tone: "tool",
914 });
915
916 it.each(["device_list", "device_open", "device_screenshot", "device_close"])(
917 "recognizes %s as device controls",
918 (tool) => {
919 const entry = deviceEntry(tool);
920 expect(summarizeToolGroup([entry]).summary).toBe("Used device controls 1 time");
921 expect(toolGroupSummaryKind([entry])).toBe("device");
922 },
923 );
924
925 it("summarizes device calls alongside shell commands", () => {
926 expect(
927 summarizeToolGroup([
928 {
929 id: "command",
930 createdAt: "2026-09-10T00:00:00.000Z",
931 label: "Ran command",
932 itemType: "command_execution",
933 command: "pwd",
934 tone: "tool",
935 },
936 deviceEntry("device_list"),
937 deviceEntry("device_open"),
938 ]).summary,
939 ).toBe("Ran 1 command and used device controls 2 times");
940 });
941
942 it("recognizes Claude tool names and preserves screenshot previews", () => {
943 const entry = {
944 ...deviceEntry("device_screenshot"),
945 toolData: { toolName: "mcp__t3_code__device_screenshot" },
946 viewedImagePath: "/workspace/device.png",
947 };
948 expect(summarizeToolGroup([entry]).summary).toBe("Used device controls 1 time");
949 expect(workEntryViewedImagePath(entry)).toBe("/workspace/device.png");
950 });
951
952 it("does not classify another server's tools as T3 device controls", () => {
953 expect(
954 summarizeToolGroup([
955 {
956 ...deviceEntry("device_open"),
957 toolData: { server: "another-server", tool: "device_open" },
958 },
959 ]).summary,
960 ).toBe("Used 1 tool");
961 });
962});