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