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

presentation.test.ts

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