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

presentation.test.ts

712 lines22.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ThreadId } from "@t3tools/contracts";
4​
5import {
6 commandDetailRepeatsCommand,
7 extractCommandOutputText,
8 resolveViewedImageAsset,
9 resolveWorkEntryToolPresentation,
10 summarizeToolGroup,
11 toolGroupAction,
12 toolGroupSummaryKind,
13 type WorkLogPresentationEntry,
14 workEntryViewedImagePath,
15 workEntryIndicatesToolFailure,
16 workEntryDisplayIndicatesToolFailure,
17 workEntryIndicatesToolSuccess,
18} from "./presentation.js";
19​
20describe("workEntryIndicatesToolFailure", () => {
21 const base = {
22 id: "w1",
23 createdAt: "2026-01-01T00:00:00.000Z",
24 label: "Read",
25 };
26​
27 it("is true for error tone", () => {
28 expect(
29 workEntryIndicatesToolFailure({
30 ...base,
31 tone: "error",
32 detail: "nothing special",
33 }),
34 ).toBe(true);
35 });
36​
37 it("is true when lifecycle says failed even if detail is empty", () => {
38 expect(
39 workEntryIndicatesToolFailure({
40 ...base,
41 tone: "tool",
42 toolLifecycleStatus: "failed",
43 }),
44 ).toBe(true);
45 });
46​
47 it("detects file-not-found style tool output with completed lifecycle", () => {
48 expect(
49 workEntryIndicatesToolFailure({
50 ...base,
51 tone: "tool",
52 toolLifecycleStatus: "completed",
53 detail: "File not found: C:\\foo\\nonexistent.ts",
54 }),
55 ).toBe(true);
56 });
57​
58 it("detects glob no files and PowerShell command errors", () => {
59 expect(
60 workEntryIndicatesToolFailure({
61 ...base,
62 label: "Glob",
63 tone: "tool",
64 detail: "No files found",
65 }),
66 ).toBe(true);
67 expect(
68 workEntryIndicatesToolFailure({
69 ...base,
70 label: "Bash",
71 tone: "tool",
72 detail:
73 "The term 'this_is_not_a_command' is not recognized as the name of a cmdlet, function, script file, or operable program.",
74 }),
75 ).toBe(true);
76 });
77​
78 it("is false for successful completed tools", () => {
79 expect(
80 workEntryIndicatesToolFailure({
81 ...base,
82 tone: "tool",
83 toolLifecycleStatus: "completed",
84 detail: "Found 3 matching files",
85 }),
86 ).toBe(false);
87 });
88​
89 it("does not treat error text in a command as rendered failure", () => {
90 const entry = {
91 label: "Ran command",
92 tone: "tool",
93 toolLifecycleStatus: "completed",
94 command: 'rg "file not found"',
95 detail: "Found 3 matches",
96 } satisfies WorkLogPresentationEntry;
97​
98 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
99 // Older activities can store output in this field, so that path stays separate.
100 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
101 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
102 });
103​
104 it("treats successful tool rows as success candidates", () => {
105 expect(
106 workEntryIndicatesToolSuccess({
107 ...base,
108 tone: "tool",
109 toolLifecycleStatus: "completed",
110 detail: "ok",
111 }),
112 ).toBe(true);
113 expect(
114 workEntryIndicatesToolSuccess({
115 ...base,
116 tone: "tool",
117 toolLifecycleStatus: "inProgress",
118 detail: "…",
119 }),
120 ).toBe(false);
121 expect(workEntryIndicatesToolSuccess({ ...base, tone: "thinking", detail: "…" })).toBe(false);
122 expect(
123 workEntryIndicatesToolSuccess({ ...base, tone: "tool", toolLifecycleStatus: "stopped" }),
124 ).toBe(false);
125 });
126​
127 it("does not run heuristics on non-tool info rows", () => {
128 expect(
129 workEntryIndicatesToolFailure({
130 ...base,
131 label: "Context compacted",
132 tone: "info",
133 detail: "File not found in conversation",
134 }),
135 ).toBe(false);
136 });
137});
138​
139describe("summarizeToolGroup", () => {
140 it.each(["command", "file-read", "file-change"])(
141 "keeps %s approvals out of tool execution counts",
142 (requestKind) => {
143 const approvals = [
144 {
145 label: "Approval requested",
146 sourceActivityKind: "approval.requested",
147 tone: "info",
148 requestKind,
149 },
150 {
151 label: "Approval resolved",
152 sourceActivityKind: "approval.resolved",
153 tone: "info",
154 requestKind,
155 },
156 {
157 label: "Provider approval response failed",
158 sourceActivityKind: "provider.approval.respond.failed",
159 tone: "error",
160 },
161 ] satisfies WorkLogPresentationEntry[];
162​
163 expect(
164 summarizeToolGroup([
165 ...approvals,
166 { label: "Read", tone: "tool", itemType: "dynamic_tool_call" },
167 ]),
168 ).toBe("Received 3 updates and used 1 tool");
169 expect(summarizeToolGroup(approvals)).toBe("Received 3 updates");
170 expect(toolGroupSummaryKind(approvals)).toBe("update");
171 },
172 );
173​
174 it("deduplicates named sources ahead of ordinary actions", () => {
175 const source = { key: "browser-use:chrome", name: "Chrome", kind: "integration" as const };
176 expect(
177 summarizeToolGroup([
178 { label: "Open page", tone: "tool", toolSource: source },
179 { label: "Inspect page", tone: "tool", toolSource: source },
180 {
181 label: "Ran command",
182 tone: "tool",
183 itemType: "command_execution",
184 command: "git status",
185 },
186 ]),
187 ).toBe("Used Chrome integration and ran 1 command");
188 });
189​
190 it("omits the integration suffix for special browser and computer sources", () => {
191 expect(
192 summarizeToolGroup([
193 {
194 label: "Inspect page",
195 tone: "tool",
196 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
197 },
198 {
199 label: "Click",
200 tone: "tool",
201 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
202 },
203 ]),
204 ).toBe("Used Browser and Computer Use");
205 });
206});
207​
208describe("resolveWorkEntryToolPresentation", () => {
209 it.each([
210 "mcp__t3-code__preview_click",
211 "mcp__t3_code__preview_click",
212 "mcp__t3code__preview_click",
213 "T3-code.preview_click",
214 "t3-code · preview_click completed",
215 "t3_code/preview_click",
216 "preview_click",
217 ])("recognizes browser tool names across providers: %s", (label) => {
218 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
219 displayName: "Clicking in the preview browser",
220 icon: "browser",
221 });
222 });
223​
224 it("labels device tools with the device icon", () => {
225 expect(
226 resolveWorkEntryToolPresentation({
227 label: "mcp__t3-code__device_open",
228 toolLifecycleStatus: "completed",
229 }),
230 ).toEqual({ displayName: "Opened a device in the Device panel", icon: "device" });
231 expect(resolveWorkEntryToolPresentation({ label: "t3-code · device_screenshot" })).toEqual({
232 displayName: "Taking a screenshot of the device",
233 icon: "device",
234 });
235 });
236​
237 it("uses structured MCP identity when the provider supplies a custom title", () => {
238 expect(
239 resolveWorkEntryToolPresentation({
240 label: "Tool call complete",
241 toolTitle: "Inspect the current page",
242 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
243 }),
244 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
245 });
246​
247 it.each([
248 ["inProgress", "Clicking in the preview browser"],
249 ["completed", "Clicked in the preview browser"],
250 ["failed", "Failed to click in the preview browser"],
251 ["declined", "Declined to click in the preview browser"],
252 ["stopped", "Stopped clicking in the preview browser"],
253 ["unknown", "Clicking in the preview browser"],
254 ])("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
255 expect(
256 resolveWorkEntryToolPresentation({
257 label: "T3-code.preview_click",
258 toolLifecycleStatus,
259 }),
260 ).toEqual({ displayName, icon: "browser" });
261 });
262​
263 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
264 const entry = { label: "T3-code.preview_click" };
265 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
266 "Clicking in the preview browser",
267 );
268 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
269 "Clicked in the preview browser",
270 );
271 expect(
272 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
273 ?.displayName,
274 ).toBe("Clicked in the preview browser");
275 expect(
276 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
277 ?.displayName,
278 ).toBe("Failed to click in the preview browser");
279 });
280​
281 it.each([
282 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
283 [
284 "preview_set_appearance",
285 "Setting preview browser appearance",
286 "Set preview browser appearance",
287 ],
288 [
289 "preview_snapshot",
290 "Taking a snapshot of the preview page",
291 "Took a snapshot of the preview page",
292 ],
293 [
294 "preview_recording_stop",
295 "Stopping recording the preview browser",
296 "Stopped recording the preview browser",
297 ],
298 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
299 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
300 [
301 "t3_worktree_handoff",
302 "Handing off thread to a git worktree",
303 "Handed off thread to a git worktree",
304 ],
305 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
306 const entry = { label: `t3-code.${tool}` };
307 expect(
308 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
309 ?.displayName,
310 ).toBe(running);
311 expect(
312 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
313 ).toBe(completed);
314 });
315​
316 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
317 expect(
318 resolveWorkEntryToolPresentation({
319 label: "mcp__t3_code__task_status",
320 toolTitle: "Check the child task",
321 }),
322 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
323 });
324​
325 it("does not brand unknown tools or another server's matching tool name", () => {
326 for (const label of [
327 "mcp__github__preview_click",
328 "t3-code.unknown_tool",
329 "t3-code.toString",
330 "Search files",
331 ]) {
332 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
333 }
334 expect(
335 resolveWorkEntryToolPresentation({
336 label: "preview_click",
337 toolData: { server: "another-server", tool: "preview_click" },
338 }),
339 ).toBeNull();
340 });
341});
342​
343describe("browser group summaries", () => {
344 const browserEntry: WorkLogPresentationEntry = {
345 label: "MCP tool call",
346 toolData: { server: "t3-code", tool: "preview_click" },
347 itemType: "mcp_tool_call",
348 toolLifecycleStatus: "completed",
349 tone: "tool",
350 };
351 const commandEntry: WorkLogPresentationEntry = {
352 label: "Ran command",
353 command: "/bin/bash -lc 'vp test run'",
354 itemType: "command_execution",
355 toolLifecycleStatus: "completed",
356 tone: "tool",
357 };
358​
359 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
360 const entries = Array.from({ length: count }, (_, index) => ({
361 ...browserEntry,
362 toolCallId: `browser-${index}`,
363 }));
364 expect(summarizeToolGroup(entries)).toBe(
365 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
366 );
367 expect(toolGroupSummaryKind(entries)).toBe("browser");
368 });
369​
370 it("combines command and browser counts in a single sentence", () => {
371 const entries = [
372 ...Array.from({ length: 4 }, () => commandEntry),
373 ...Array.from({ length: 15 }, () => browserEntry),
374 ];
375 expect(summarizeToolGroup(entries)).toBe("Ran 4 commands and used browser 15 times");
376 expect(toolGroupSummaryKind(entries)).toBe("mixed");
377 });
378​
379 it("preserves first-seen action ordering alongside non-browser tools", () => {
380 expect(
381 summarizeToolGroup([
382 browserEntry,
383 commandEntry,
384 {
385 ...browserEntry,
386 toolData: { server: "t3-code", tool: "task_status" },
387 },
388 ]),
389 ).toBe("Used browser 1 time, ran 1 command, and used 1 tool");
390 });
391​
392 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
393 expect(
394 summarizeToolGroup([
395 {
396 ...browserEntry,
397 command: "node inspect-page.js",
398 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
399 },
400 ]),
401 ).toBe("Used browser 1 time");
402 });
403​
404 it("keeps foreign tools and web searches out of the browser count", () => {
405 expect(
406 summarizeToolGroup([
407 browserEntry,
408 {
409 ...browserEntry,
410 label: "preview_click",
411 toolData: { server: "another-server", tool: "preview_click" },
412 },
413 { label: "Search", tone: "tool", itemType: "web_search" },
414 ]),
415 ).toBe("Used browser 1 time, used 1 tool, and searched the web 1 time");
416 });
417​
418 it("keeps browser screenshots in the browser count while preserving their image path", () => {
419 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
420 expect(summarizeToolGroup([entry])).toBe("Used browser 1 time");
421 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
422 });
423});
424​
425describe("command work-log details", () => {
426 it("extracts Claude result blocks and projected output", () => {
427 expect(
428 extractCommandOutputText({
429 result: {
430 content: [
431 { type: "text", text: "first" },
432 { type: "text", text: "second" },
433 ],
434 },
435 }),
436 ).toBe("first\nsecond");
437 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
438 "projected summary",
439 );
440 });
441​
442 it("only removes a detail with the matching tool-name prefix", () => {
443 expect(
444 commandDetailRepeatsCommand({
445 detail: "Bash: printf hello",
446 command: "printf hello",
447 rawCommand: null,
448 toolName: "Bash",
449 data: { toolName: "Bash", command: "printf hello" },
450 }),
451 ).toBe(true);
452 expect(
453 commandDetailRepeatsCommand({
454 detail: "warning: printf hello",
455 command: "printf hello",
456 rawCommand: null,
457 toolName: "Bash",
458 data: { toolName: "Bash", command: "printf hello" },
459 }),
460 ).toBe(false);
461 });
462​
463 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
464 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
465 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
466 expect(
467 commandDetailRepeatsCommand({
468 detail: truncated,
469 command,
470 rawCommand: null,
471 toolName: "Bash",
472 data: { toolName: "Bash", command },
473 }),
474 ).toBe(true);
475 expect(
476 commandDetailRepeatsCommand({
477 detail: "Bash: printf hello...",
478 command: "printf goodbye",
479 rawCommand: null,
480 toolName: "Bash",
481 data: { toolName: "Bash", command: "printf goodbye" },
482 }),
483 ).toBe(false);
484 });
485​
486 it("treats ACP command echoes as synthetic even without a tool kind", () => {
487 expect(
488 commandDetailRepeatsCommand({
489 detail: "pnpm test",
490 command: "pnpm test",
491 rawCommand: null,
492 toolName: undefined,
493 data: { toolCallId: "tool-1", command: "pnpm test" },
494 }),
495 ).toBe(true);
496 expect(
497 commandDetailRepeatsCommand({
498 detail: "pnpm test",
499 command: "pnpm test",
500 rawCommand: null,
501 toolName: undefined,
502 data: { command: "pnpm test" },
503 }),
504 ).toBe(false);
505 });
506});
507​
508describe("workEntryViewedImagePath", () => {
509 const entry = { label: "Read", tone: "tool" } as const;
510​
511 it("returns a single image path from supported read entries", () => {
512 expect(
513 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
514 ).toBe("assets/a.png");
515 expect(
516 workEntryViewedImagePath({
517 ...entry,
518 itemType: "dynamic_tool_call",
519 toolTitle: "Read file",
520 detail: "C:\\workspace\\a.webp",
521 }),
522 ).toBe("C:\\workspace\\a.webp");
523 expect(
524 workEntryViewedImagePath({
525 ...entry,
526 itemType: "dynamic_tool_call",
527 detail: 'Read: {"file_path":"truncated..."}',
528 viewedImagePath: " /workspace/reference image.webp ",
529 }),
530 ).toBe("/workspace/reference image.webp");
531 });
532​
533 it("rejects non-image, multi-line, and non-read details", () => {
534 expect(
535 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.txt" }),
536 ).toBeNull();
537 expect(
538 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.png\nb.png" }),
539 ).toBeNull();
540 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
541 });
542});
543​
544describe("toolGroupAction", () => {
545 it("groups legacy Claude image reads with other reads", () => {
546 expect(
547 toolGroupAction({
548 label: "Tool call",
549 tone: "tool",
550 itemType: "dynamic_tool_call",
551 viewedImagePath: "/workspace/reference.png",
552 }),
553 ).toBe("read");
554 });
555});
556​
557describe("resolveViewedImageAsset", () => {
558 const threadId = ThreadId.make("thread-1");
559​
560 it("serves t3 attachment paths in place like any other host path", () => {
561 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
562 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
563 resource: { _tag: "media-file", threadId, path },
564 alt: "11111111-1111-4111-8111-111111111111.png",
565 srcFragment: "",
566 });
567 });
568​
569 it("normalizes workspace image sources", () => {
570 expect(
571 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
572 threadId,
573 workspaceRoot: "/workspace",
574 }),
575 ).toEqual({
576 resource: {
577 _tag: "media-file",
578 threadId,
579 path: "/workspace/screens/logo.svg",
580 },
581 alt: "logo.svg",
582 srcFragment: "#mark",
583 });
584 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
585 });
586});
587​
588describe("pull request tool presentation", () => {
589 it.each([
590 "mcp__t3-code__link_pull_request",
591 "mcp__t3_code__link_pull_request",
592 "T3-code · link_pull_request",
593 "t3code/link_pull_request",
594 "link_pull_request",
595 ])("recognizes the native linking tool: %s", (label) => {
596 const entry = { label, tone: "tool" as const, toolLifecycleStatus: "completed" };
597 expect(resolveWorkEntryToolPresentation(entry)).toMatchObject({
598 displayName: "Linked a pull request",
599 icon: "pull-request",
600 });
601 expect(toolGroupAction(entry)).toBe("link-pr");
602 });
603​
604 it.each([
605 ["inProgress", "Linking PR #42"],
606 ["completed", "Linked PR #42"],
607 ["failed", "Failed to link PR #42"],
608 ["declined", "Declined to link PR #42"],
609 ["stopped", "Stopped linking PR #42"],
610 ])("describes the target and %s status", (toolLifecycleStatus, displayName) => {
611 expect(
612 resolveWorkEntryToolPresentation({
613 label: "MCP tool call",
614 toolTitle: "Custom title",
615 toolLifecycleStatus,
616 toolData: {
617 server: "t3-code",
618 tool: "link_pull_request",
619 arguments: { url: "https://github.com/acme/web/pull/42" },
620 },
621 })?.displayName,
622 ).toBe(displayName);
623 });
624​
625 it("recognizes unlink targets supplied as repository and number", () => {
626 expect(
627 resolveWorkEntryToolPresentation({
628 label: "MCP tool call",
629 toolLifecycleStatus: "completed",
630 toolData: {
631 toolName: "mcp__t3-code__unlink_pull_request",
632 rawInput: { repository: "acme/web", number: 42 },
633 },
634 }),
635 ).toMatchObject({ displayName: "Unlinked PR #42", icon: "pull-request", action: "unlink-pr" });
636 });
637​
638 it("summarizes native PR work separately from ordinary tools and integration metadata", () => {
639 const link: WorkLogPresentationEntry = {
640 label: "T3-code · link_pull_request",
641 tone: "tool",
642 itemType: "mcp_tool_call",
643 toolLifecycleStatus: "completed",
644 toolSource: { key: "t3-code", name: "T3 Code", kind: "integration" },
645 };
646 const list: WorkLogPresentationEntry = {
647 ...link,
648 label: "T3-code · list_thread_pull_requests",
649 };
650 expect(summarizeToolGroup([link, link, list])).toBe(
651 "Linked 2 pull requests and checked linked pull requests",
652 );
653 expect(summarizeToolGroup([{ ...link, label: "T3-code · unlink_pull_request" }])).toBe(
654 "Unlinked 1 pull request",
655 );
656 expect(toolGroupSummaryKind([link, link, list])).toBe("pull-request");
657 expect(summarizeToolGroup([list, list])).toBe("Checked linked pull requests 2 times");
658 expect(
659 resolveWorkEntryToolPresentation({ label: "mcp__another-server__link_pull_request" }),
660 ).toBeNull();
661 });
662});
663​
664describe("device group summaries", () => {
665 const deviceEntry = (tool: string): WorkLogPresentationEntry => ({
666 label: "MCP tool call",
667 toolData: { server: "t3-code", tool },
668 itemType: "mcp_tool_call",
669 toolLifecycleStatus: "completed",
670 tone: "tool",
671 });
672​
673 it.each(["device_list", "device_open", "device_screenshot", "device_close"])(
674 "recognizes %s as device controls",
675 (tool) => {
676 const entry = deviceEntry(tool);
677 expect(summarizeToolGroup([entry])).toBe("Used device controls 1 time");
678 expect(toolGroupSummaryKind([entry])).toBe("device");
679 },
680 );
681​
682 it("summarizes device calls alongside shell commands", () => {
683 expect(
684 summarizeToolGroup([
685 { label: "Ran command", itemType: "command_execution", command: "pwd", tone: "tool" },
686 deviceEntry("device_list"),
687 deviceEntry("device_open"),
688 ]),
689 ).toBe("Ran 1 command and used device controls 2 times");
690 });
691​
692 it("recognizes Claude tool names and preserves screenshot previews", () => {
693 const entry = {
694 ...deviceEntry("device_screenshot"),
695 toolData: { toolName: "mcp__t3_code__device_screenshot" },
696 viewedImagePath: "/workspace/device.png",
697 };
698 expect(summarizeToolGroup([entry])).toBe("Used device controls 1 time");
699 expect(workEntryViewedImagePath(entry)).toBe("/workspace/device.png");
700 });
701​
702 it("does not classify another server's tools as T3 device controls", () => {
703 expect(
704 summarizeToolGroup([
705 {
706 ...deviceEntry("device_open"),
707 toolData: { server: "another-server", tool: "device_open" },
708 },
709 ]),
710 ).toBe("Used 1 tool");
711 });
712});