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

presentation.test.ts

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