Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

presentation.test.ts

1,083 lines35.3 KB

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 };
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.each([
257 [["Foo", "Foo"], "Used Foo integration and ran 1 command"],
258 [["Foo", "Bar", "Foo"], "Used Foo and Bar integrations and ran 1 command"],
259 ] as const)(
260 "uses the displayed source names for integration plurality: %j",
261 (names, expected) => {
262 expect(
263 summarizeToolGroup([
264 ...names.map((name, index) =>
265 entry(`integration-${index}`, {
266 toolSource: { key: `integration-${index}`, name, kind: "integration" },
267 }),
268 ),
269 entry("command", {
270 itemType: "command_execution",
271 command: "git status",
272 }),
273 ]).summary,
274 ).toBe(expected);
275 },
276 );
277​
278 it("omits the integration suffix for special browser and computer sources", () => {
279 expect(
280 summarizeToolGroup([
281 entry("inspect", {
282 label: "Inspect page",
283 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
284 }),
285 entry("click", {
286 label: "Click",
287 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
288 }),
289 ]).summary,
290 ).toBe("Used Browser and Computer Use");
291 });
292​
293 it("names Computer Use once when native-app and fallback sources share its label", () => {
294 expect(
295 summarizeToolGroup([
296 entry("inspect-app", {
297 label: "Inspect app",
298 toolSource: {
299 key: "native-app:com.t3tools.t3code",
300 name: "Computer Use",
301 kind: "computer",
302 },
303 }),
304 entry("failed-sky", {
305 label: "Restrict the breakpoint to Git actions",
306 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
307 }),
308 ]).summary,
309 ).toBe("Used Computer Use");
310 });
311});
312​
313describe("resolveWorkEntryToolPresentation", () => {
314 it("presents and summarizes every T3 tool using the same structured identity", () => {
315 for (const tool of T3_MCP_TOOL_NAMES) {
316 const entry: WorkLogPresentationEntry = {
317 id: tool,
318 createdAt: "2026-09-19T00:00:00.000Z",
319 tone: "tool",
320 label: "Custom provider title",
321 toolData: { server: "t3-code", tool },
322 toolLifecycleStatus: "completed",
323 itemType: "dynamic_tool",
324 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
325 };
326 const presentation = resolveWorkEntryToolPresentation(entry);
327 expect(presentation, tool).not.toBeNull();
328 expect(presentation?.displayName, tool).not.toContain(tool);
329 const summary = summarizeToolGroup([entry]);
330 expect(summary.summary, tool).not.toMatch(/Used (?:1 tool|T3 Code integration)/);
331 expect(summary.hasFailure, tool).toBe(false);
332 const failed = { ...entry, toolLifecycleStatus: "failed" as const };
333 expect(resolveWorkEntryToolPresentation(failed)?.displayName, tool).toMatch(/^Failed to /);
334 expect(summarizeToolGroup([failed]).hasFailure, tool).toBe(true);
335 expect(summarizeToolGroup([failed]).summary, tool).toMatch(
336 /^(?:Tried to |Requested thread creation)/,
337 );
338 }
339 });
340​
341 it.each([
342 ["t3_project_list", "Listing projects", "Listed projects"],
343 ["t3_project_clone", "Cloning a repository", "Cloned a repository"],
344 ["t3_project_create", "Registering a project", "Registered a project"],
345 ["t3_thread_launch", "Launching a project thread", "Launched a project thread"],
346 ["t3_queue_edit", "Editing a queued message", "Edited a queued message"],
347 ["t3_pending_request_respond", "Answering pending questions", "Answered pending questions"],
348 ["t3_thread_configure", "Setting thread model", "Set thread model"],
349 ["t3_thread_fork", "Forking this thread", "Requested a fork of this thread"],
350 ["t3_thread_send_attachments", "Sending attachments", "Sent attachments"],
351 ["run_scheduled_task_now", "Running a scheduled task", "Requested a run of a scheduled task"],
352 ["t3_workflow_stop", "Stopping a workflow", "Requested a stop for a workflow"],
353 ])("labels %s through its lifecycle", (tool, running, completed) => {
354 expect(resolveWorkEntryToolPresentation({ label: `T3-code.${tool}` })?.displayName).toBe(
355 running,
356 );
357 expect(
358 resolveWorkEntryToolPresentation({
359 label: `T3-code.${tool}`,
360 toolLifecycleStatus: "completed",
361 })?.displayName,
362 ).toBe(completed);
363 });
364​
365 it("summarizes workflow stop targets separately from their parent thread", () => {
366 const entry = {
367 id: "workflow-stop",
368 createdAt: "2026-10-07T00:00:00.000Z",
369 tone: "tool",
370 label: "Custom title",
371 itemType: "dynamic_tool",
372 toolLifecycleStatus: "completed",
373 toolData: {
374 server: "t3-code",
375 tool: "t3_workflow_stop",
376 arguments: { threadId: "parent", subagentId: "workflow-1" },
377 result: {},
378 },
379 } satisfies WorkLogPresentationEntry;
380 expect(
381 summarizeToolGroup([
382 entry,
383 entry,
384 {
385 ...entry,
386 toolData: {
387 ...entry.toolData,
388 arguments: { threadId: "parent", subagentId: "workflow-2" },
389 },
390 },
391 {
392 ...entry,
393 toolData: {
394 ...entry.toolData,
395 tool: "t3_thread_interrupt",
396 arguments: { threadId: "parent" },
397 },
398 },
399 ]),
400 ).toEqual({
401 summary: "Requested stops for 2 workflows and requested interrupts for 1 thread",
402 hasFailure: false,
403 });
404 });
405​
406 it("summarizes project tools from MCP arguments and results without claiming failed effects", () => {
407 const entry: WorkLogPresentationEntry = {
408 id: "clone",
409 createdAt: "2026-09-19T00:00:00.000Z",
410 tone: "tool",
411 label: "Custom title",
412 itemType: "dynamic_tool",
413 toolLifecycleStatus: "completed",
414 toolData: {
415 server: "t3-code",
416 tool: "t3_project_clone",
417 arguments: { url: "https://github.com/acme/repo" },
418 result: { cwd: "/tmp/repo" },
419 },
420 };
421 const list = { ...entry, toolData: { server: "t3-code", tool: "t3_project_list" } };
422 expect(summarizeToolGroup([list, entry])).toEqual({
423 summary: "Listed projects 1 time and cloned 1 repository",
424 hasFailure: false,
425 });
426 const failed = {
427 ...entry,
428 toolData: { toolName: "T3-code.t3_project_clone", rawOutput: { isError: true } },
429 };
430 expect(summarizeToolGroup([entry, failed])).toEqual({
431 summary: "Cloned 1 repository",
432 hasFailure: true,
433 });
434 });
435​
436 it("does not summarize a foreign structured identity as T3 work", () => {
437 const entry: WorkLogPresentationEntry = {
438 id: "foreign",
439 createdAt: "2026-09-19T00:00:00.000Z",
440 tone: "tool",
441 label: "t3_project_clone",
442 toolLifecycleStatus: "completed",
443 toolData: { server: "another-server", tool: "t3_project_clone" },
444 };
445 expect(summarizeToolGroup([entry]).summary).toBe("Used 1 tool");
446 });
447​
448 it("shows returned MCP errors as failures even in the live activity row", () => {
449 const entry: WorkLogPresentationEntry = {
450 id: "clone",
451 createdAt: "2026-09-19T00:00:00.000Z",
452 tone: "tool",
453 label: "T3-code.t3_project_clone",
454 toolLifecycleStatus: "inProgress",
455 itemType: "dynamic_tool",
456 toolData: { output: { isError: true } },
457 };
458 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
459 "Failed to clone a repository",
460 );
461 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
462 expect(workEntryIndicatesToolSuccess(entry)).toBe(false);
463 const childFailure = {
464 ...entry,
465 label: "T3-code.task_status",
466 toolLifecycleStatus: "completed" as const,
467 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
468 };
469 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
470 expect(workEntryIndicatesToolSuccess(childFailure)).toBe(true);
471 });
472 it.each([
473 "mcp__t3-code__preview_click",
474 "mcp__t3_code__preview_click",
475 "mcp__t3code__preview_click",
476 "T3-code.preview_click",
477 "t3-code · preview_click completed",
478 "t3_code/preview_click",
479 "preview_click",
480 ])("recognizes browser tool names across providers: %s", (label) => {
481 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
482 displayName: "Clicking in the preview browser",
483 icon: "browser",
484 });
485 });
486​
487 it("labels device tools with the device icon", () => {
488 expect(
489 resolveWorkEntryToolPresentation({
490 label: "mcp__t3-code__device_open",
491 toolLifecycleStatus: "completed",
492 }),
493 ).toEqual({ displayName: "Opened a device in the Device panel", icon: "device" });
494 expect(resolveWorkEntryToolPresentation({ label: "t3-code · device_screenshot" })).toEqual({
495 displayName: "Taking a screenshot of the device",
496 icon: "device",
497 });
498 });
499​
500 it("uses structured MCP identity when the provider supplies a custom title", () => {
501 expect(
502 resolveWorkEntryToolPresentation({
503 label: "Tool call complete",
504 toolTitle: "Inspect the current page",
505 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
506 }),
507 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
508 });
509​
510 it.each([
511 ["inProgress", "Clicking in the preview browser"],
512 ["completed", "Clicked in the preview browser"],
513 ["failed", "Failed to click in the preview browser"],
514 ["declined", "Declined to click in the preview browser"],
515 ["stopped", "Stopped clicking in the preview browser"],
516 ["unknown", "Clicking in the preview browser"],
517 ] as const)("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
518 expect(
519 resolveWorkEntryToolPresentation({
520 label: "T3-code.preview_click",
521 toolLifecycleStatus: toolLifecycleStatus as WorkLogToolLifecycleStatus,
522 }),
523 ).toEqual({ displayName, icon: "browser" });
524 });
525​
526 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
527 const entry = { label: "T3-code.preview_click" };
528 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
529 "Clicking in the preview browser",
530 );
531 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
532 "Clicked in the preview browser",
533 );
534 expect(
535 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
536 ?.displayName,
537 ).toBe("Clicked in the preview browser");
538 expect(
539 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
540 ?.displayName,
541 ).toBe("Failed to click in the preview browser");
542 });
543​
544 it.each([
545 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
546 [
547 "preview_set_appearance",
548 "Setting preview browser appearance",
549 "Set preview browser appearance",
550 ],
551 [
552 "preview_snapshot",
553 "Taking a snapshot of the preview page",
554 "Took a snapshot of the preview page",
555 ],
556 [
557 "preview_recording_stop",
558 "Stopping recording the preview browser",
559 "Stopped recording the preview browser",
560 ],
561 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
562 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
563 [
564 "t3_worktree_handoff",
565 "Handing off thread to a git worktree",
566 "Handed off thread to a git worktree",
567 ],
568 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
569 const entry = { label: `t3-code.${tool}` };
570 expect(
571 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
572 ?.displayName,
573 ).toBe(running);
574 expect(
575 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
576 ).toBe(completed);
577 });
578​
579 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
580 expect(
581 resolveWorkEntryToolPresentation({
582 label: "mcp__t3_code__task_status",
583 toolTitle: "Check the child task",
584 }),
585 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
586 });
587​
588 it("does not brand unknown tools or another server's matching tool name", () => {
589 for (const label of [
590 "mcp__github__preview_click",
591 "t3-code.unknown_tool",
592 "t3-code.toString",
593 "Search files",
594 ]) {
595 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
596 }
597 expect(
598 resolveWorkEntryToolPresentation({
599 label: "preview_click",
600 toolData: { server: "another-server", tool: "preview_click" },
601 }),
602 ).toBeNull();
603 });
604});
605​
606describe("browser group summaries", () => {
607 const summarizeGroupLabel = (entries: ReadonlyArray<WorkLogPresentationEntry>) =>
608 summarizeToolGroup(entries).summary;
609 const browserEntry: WorkLogPresentationEntry = {
610 id: "browser",
611 createdAt: "2026-09-01T00:00:00Z",
612 label: "MCP tool call",
613 toolData: { server: "t3-code", tool: "preview_click" },
614 itemType: "dynamic_tool",
615 toolLifecycleStatus: "completed",
616 tone: "tool",
617 };
618 const commandEntry: WorkLogPresentationEntry = {
619 id: "command",
620 createdAt: "2026-09-01T00:00:00Z",
621 label: "Ran command",
622 command: "/bin/bash -lc 'vp test run'",
623 itemType: "command_execution",
624 toolLifecycleStatus: "completed",
625 tone: "tool",
626 };
627​
628 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
629 const entries = Array.from({ length: count }, (_, index) => ({
630 ...browserEntry,
631 toolCallId: `browser-${index}`,
632 }));
633 expect(summarizeGroupLabel(entries)).toBe(
634 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
635 );
636 expect(toolGroupSummaryKind(entries)).toBe("browser");
637 });
638​
639 it("combines command and browser counts in a single sentence", () => {
640 const entries = [
641 ...Array.from({ length: 4 }, () => commandEntry),
642 ...Array.from({ length: 15 }, () => browserEntry),
643 ];
644 expect(summarizeGroupLabel(entries)).toBe("Ran 4 commands and used browser 15 times");
645 expect(toolGroupSummaryKind(entries)).toBe("mixed");
646 });
647​
648 it("preserves first-seen action ordering alongside non-browser tools", () => {
649 expect(
650 summarizeGroupLabel([
651 browserEntry,
652 commandEntry,
653 {
654 ...browserEntry,
655 toolData: { server: "t3-code", tool: "task_status" },
656 },
657 ]),
658 ).toBe("Used browser 1 time, ran 1 command, and performed 1 other action");
659 });
660​
661 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
662 expect(
663 summarizeGroupLabel([
664 {
665 ...browserEntry,
666 command: "node inspect-page.js",
667 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
668 },
669 ]),
670 ).toBe("Used browser 1 time");
671 });
672​
673 it("keeps foreign tools and web searches out of the browser count", () => {
674 expect(
675 summarizeGroupLabel([
676 browserEntry,
677 {
678 ...browserEntry,
679 label: "preview_click",
680 toolData: { server: "another-server", tool: "preview_click" },
681 },
682 {
683 id: "search",
684 createdAt: "2026-09-01T00:00:00Z",
685 label: "Search",
686 tone: "tool",
687 itemType: "web_search",
688 },
689 ]),
690 ).toBe("Used browser 1 time, searched the web 1 time, and performed 1 other action");
691 });
692​
693 it("keeps browser screenshots in the browser count while preserving their image path", () => {
694 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
695 expect(summarizeGroupLabel([entry])).toBe("Used browser 1 time");
696 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
697 });
698});
699​
700describe("command work-log details", () => {
701 it("extracts Claude result blocks and projected output", () => {
702 expect(
703 extractCommandOutputText({
704 result: {
705 content: [
706 { type: "text", text: "first" },
707 { type: "text", text: "second" },
708 ],
709 },
710 }),
711 ).toBe("first\nsecond");
712 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
713 "projected summary",
714 );
715 });
716​
717 it("only removes a detail with the matching tool-name prefix", () => {
718 expect(
719 commandDetailRepeatsCommand({
720 detail: "Bash: printf hello",
721 command: "printf hello",
722 rawCommand: null,
723 toolName: "Bash",
724 data: { toolName: "Bash", command: "printf hello" },
725 }),
726 ).toBe(true);
727 expect(
728 commandDetailRepeatsCommand({
729 detail: "warning: printf hello",
730 command: "printf hello",
731 rawCommand: null,
732 toolName: "Bash",
733 data: { toolName: "Bash", command: "printf hello" },
734 }),
735 ).toBe(false);
736 });
737​
738 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
739 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
740 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
741 expect(
742 commandDetailRepeatsCommand({
743 detail: truncated,
744 command,
745 rawCommand: null,
746 toolName: "Bash",
747 data: { toolName: "Bash", command },
748 }),
749 ).toBe(true);
750 expect(
751 commandDetailRepeatsCommand({
752 detail: "Bash: printf hello...",
753 command: "printf goodbye",
754 rawCommand: null,
755 toolName: "Bash",
756 data: { toolName: "Bash", command: "printf goodbye" },
757 }),
758 ).toBe(false);
759 });
760​
761 it("treats ACP command echoes as synthetic even without a tool kind", () => {
762 expect(
763 commandDetailRepeatsCommand({
764 detail: "pnpm test",
765 command: "pnpm test",
766 rawCommand: null,
767 toolName: undefined,
768 data: { toolCallId: "tool-1", command: "pnpm test" },
769 }),
770 ).toBe(true);
771 expect(
772 commandDetailRepeatsCommand({
773 detail: "pnpm test",
774 command: "pnpm test",
775 rawCommand: null,
776 toolName: undefined,
777 data: { command: "pnpm test" },
778 }),
779 ).toBe(false);
780 });
781});
782​
783describe("workEntryViewedImagePath", () => {
784 const entry = {
785 id: "entry-1",
786 createdAt: "2026-01-01T00:00:00Z",
787 label: "Read",
788 tone: "tool",
789 } as const;
790​
791 it("returns a single image path from supported read entries", () => {
792 expect(
793 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
794 ).toBe("assets/a.png");
795 expect(
796 workEntryViewedImagePath({
797 ...entry,
798 itemType: "dynamic_tool",
799 toolTitle: "Read file",
800 detail: "C:\\workspace\\a.webp",
801 }),
802 ).toBe("C:\\workspace\\a.webp");
803 expect(
804 workEntryViewedImagePath({
805 ...entry,
806 itemType: "dynamic_tool",
807 detail: 'Read: {"file_path":"truncated..."}',
808 viewedImagePath: " /workspace/reference image.webp ",
809 }),
810 ).toBe("/workspace/reference image.webp");
811 });
812​
813 it("rejects non-image, multi-line, and non-read details", () => {
814 expect(
815 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: "a.txt" }),
816 ).toBeNull();
817 expect(
818 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: "a.png\nb.png" }),
819 ).toBeNull();
820 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
821 });
822});
823​
824describe("toolGroupAction", () => {
825 it("groups legacy Claude image reads with other reads", () => {
826 expect(
827 toolGroupAction({
828 id: "legacy-read",
829 createdAt: "2026-09-01T00:00:00Z",
830 label: "Tool call",
831 tone: "tool",
832 itemType: "dynamic_tool",
833 viewedImagePath: "/workspace/reference.png",
834 }),
835 ).toBe("read");
836 });
837​
838 it("groups Claude Read and Grep from toolName, not file contents", () => {
839 expect(
840 toolGroupAction({
841 id: "read",
842 createdAt: "2026-09-01T00:00:00Z",
843 label: "Read",
844 tone: "tool",
845 itemType: "dynamic_tool",
846 toolTitle: "Read",
847 toolData: { input: { file_path: "src/env.ts" } },
848 structuredPayload: {
849 type: "dynamic_tool",
850 toolName: "Read",
851 input: { file_path: "src/env.ts" },
852 } as NonNullable<WorkLogPresentationEntry["structuredPayload"]>,
853 }),
854 ).toBe("read");
855 expect(
856 toolGroupAction({
857 id: "grep",
858 createdAt: "2026-09-01T00:00:00Z",
859 label: "Grep",
860 tone: "tool",
861 itemType: "dynamic_tool",
862 toolTitle: "Grep",
863 toolData: { input: { pattern: "TODO", path: "apps/web" } },
864 structuredPayload: {
865 type: "dynamic_tool",
866 toolName: "Grep",
867 input: { pattern: "TODO", path: "apps/web" },
868 } as NonNullable<WorkLogPresentationEntry["structuredPayload"]>,
869 }),
870 ).toBe("code-search");
871 });
872});
873​
874describe("resolveViewedImageAsset", () => {
875 const threadId = ThreadId.make("thread-1");
876​
877 it("serves t3 attachment paths in place like any other host path", () => {
878 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
879 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
880 resource: { _tag: "media-file", threadId, path },
881 alt: "11111111-1111-4111-8111-111111111111.png",
882 srcFragment: "",
883 });
884 });
885​
886 it("normalizes workspace image sources", () => {
887 expect(
888 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
889 threadId,
890 workspaceRoot: "/workspace",
891 }),
892 ).toEqual({
893 resource: {
894 _tag: "media-file",
895 threadId,
896 path: "/workspace/screens/logo.svg",
897 },
898 alt: "logo.svg",
899 srcFragment: "#mark",
900 });
901 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
902 });
903});
904​
905describe("pull request tool presentation", () => {
906 it.each([
907 "mcp__t3-code__link_pull_request",
908 "mcp__t3_code__link_pull_request",
909 "T3-code · link_pull_request",
910 "t3code/link_pull_request",
911 "link_pull_request",
912 ])("recognizes the native linking tool: %s", (label) => {
913 const entry: WorkLogPresentationEntry = {
914 id: "link",
915 createdAt: "2026-09-10T00:00:00.000Z",
916 label,
917 tone: "tool",
918 toolLifecycleStatus: "completed",
919 };
920 expect(resolveWorkEntryToolPresentation(entry)).toMatchObject({
921 displayName: "Linked a pull request",
922 icon: "pull-request",
923 });
924 expect(toolGroupAction(entry)).toBe("link-pr");
925 });
926​
927 it.each([
928 ["inProgress", "Linking PR #42"],
929 ["completed", "Linked PR #42"],
930 ["failed", "Failed to link PR #42"],
931 ["declined", "Declined to link PR #42"],
932 ["stopped", "Stopped linking PR #42"],
933 ] as const)("describes the target and %s status", (toolLifecycleStatus, displayName) => {
934 expect(
935 resolveWorkEntryToolPresentation({
936 label: "MCP tool call",
937 toolTitle: "Custom title",
938 toolLifecycleStatus,
939 toolData: {
940 server: "t3-code",
941 tool: "link_pull_request",
942 arguments: { url: "https://github.com/acme/web/pull/42" },
943 },
944 })?.displayName,
945 ).toBe(displayName);
946 });
947​
948 it("recognizes unlink targets supplied as repository and number", () => {
949 expect(
950 resolveWorkEntryToolPresentation({
951 label: "MCP tool call",
952 toolLifecycleStatus: "completed",
953 toolData: {
954 toolName: "mcp__t3-code__unlink_pull_request",
955 rawInput: { repository: "acme/web", number: 42 },
956 },
957 }),
958 ).toMatchObject({ displayName: "Unlinked PR #42", icon: "pull-request", action: "unlink-pr" });
959 });
960​
961 it("summarizes native PR work separately from ordinary tools and integration metadata", () => {
962 const link: WorkLogPresentationEntry = {
963 id: "link",
964 createdAt: "2026-09-10T00:00:00.000Z",
965 label: "T3-code · link_pull_request",
966 tone: "tool",
967 itemType: "dynamic_tool",
968 toolLifecycleStatus: "completed",
969 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
970 };
971 const list: WorkLogPresentationEntry = {
972 ...link,
973 label: "T3-code · list_thread_pull_requests",
974 };
975 expect(summarizeToolGroup([link, link, list]).summary).toBe(
976 "Linked 2 pull requests and checked linked pull requests",
977 );
978 expect(summarizeToolGroup([{ ...link, label: "T3-code · unlink_pull_request" }]).summary).toBe(
979 "Unlinked 1 pull request",
980 );
981 expect(toolGroupSummaryKind([link, link, list])).toBe("pull-request");
982 expect(summarizeToolGroup([list, list]).summary).toBe("Checked linked pull requests 2 times");
983 expect(
984 resolveWorkEntryToolPresentation({ label: "mcp__another-server__link_pull_request" }),
985 ).toBeNull();
986 });
987});
988​
989describe("device group summaries", () => {
990 const deviceEntry = (tool: string): WorkLogPresentationEntry => ({
991 id: tool,
992 createdAt: "2026-09-10T00:00:00.000Z",
993 label: "MCP tool call",
994 toolData: { server: "t3-code", tool },
995 itemType: "dynamic_tool",
996 toolLifecycleStatus: "completed",
997 tone: "tool",
998 });
999​
1000 it.each(["device_list", "device_open", "device_screenshot", "device_close"])(
1001 "recognizes %s as device controls",
1002 (tool) => {
1003 const entry = deviceEntry(tool);
1004 expect(summarizeToolGroup([entry]).summary).toBe("Used device controls 1 time");
1005 expect(toolGroupSummaryKind([entry])).toBe("device");
1006 },
1007 );
1008​
1009 it("summarizes device calls alongside shell commands", () => {
1010 expect(
1011 summarizeToolGroup([
1013 id: "command",
1014 createdAt: "2026-09-10T00:00:00.000Z",
1015 label: "Ran command",
1016 itemType: "command_execution",
1017 command: "pwd",
1018 tone: "tool",
1019 },
1020 deviceEntry("device_list"),
1021 deviceEntry("device_open"),
1022 ]).summary,
1023 ).toBe("Ran 1 command and used device controls 2 times");
1024 });
1025​
1026 it("recognizes Claude tool names and preserves screenshot previews", () => {
1027 const entry = {
1028 ...deviceEntry("device_screenshot"),
1029 toolData: { toolName: "mcp__t3_code__device_screenshot" },
1030 viewedImagePath: "/workspace/device.png",
1031 };
1032 expect(summarizeToolGroup([entry]).summary).toBe("Used device controls 1 time");
1033 expect(workEntryViewedImagePath(entry)).toBe("/workspace/device.png");
1034 });
1035​
1036 it("does not classify another server's tools as T3 device controls", () => {
1037 expect(
1038 summarizeToolGroup([
1040 ...deviceEntry("device_open"),
1041 toolData: { server: "another-server", tool: "device_open" },
1042 },
1043 ]).summary,
1044 ).toBe("Used 1 tool");
1045 });
1046});
1047​
1048describe("liveThoughtLine", () => {
1049 it("keeps the first sentence, including a closing quote", () => {
1050 expect(
1051 liveThoughtLine(
1052 'Found the cause: the repo has no commits, so `git worktree add` fails with "invalid reference: main." Now checking the UI.',
1053 ),
1054 ).toBe(
1055 'Found the cause: the repo has no commits, so git worktree add fails with "invalid reference: main."',
1056 );
1057 });
1058​
1059 it("does not cut at dots inside file names or long dashes", () => {
1060 expect(
1061 liveThoughtLine("I read ThreadLaunchService.ts \u2014 it skips the fetch. Next step."),
1062 ).toBe("I read ThreadLaunchService.ts \u2014 it skips the fetch.");
1063 });
1064​
1065 it("uses a bold-only opening line as the whole line", () => {
1066 expect(
1067 liveThoughtLine("**Narrowing dispatch files**\n\nI should check the adapter. Then more."),
1068 ).toBe("Narrowing dispatch files");
1069 expect(liveThoughtLine("**Narrowing dispatch files**\r\n\r\nI should check the adapter.")).toBe(
1070 "Narrowing dispatch files",
1071 );
1072 });
1073​
1074 it("returns unpunctuated text whole and flattens markdown", () => {
1075 expect(liveThoughtLine("- Checking [the docs](https://x.dev) for **limits**")).toBe(
1076 "Checking the docs for limits",
1077 );
1078 expect(liveThoughtLine("This is *really* ~~not~~ _fine_ in snake_case_names.")).toBe(
1079 "This is really not fine in snake_case_names.",
1080 );
1081 expect(liveThoughtLine(" ")).toBe("");
1082 });
1083});