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

presentation.test.ts

573 lines17.5 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("uses structured MCP identity when the provider supplies a custom title", () => {
225 expect(
226 resolveWorkEntryToolPresentation({
227 label: "Tool call complete",
228 toolTitle: "Inspect the current page",
229 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
230 }),
231 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
232 });
233​
234 it.each([
235 ["inProgress", "Clicking in the preview browser"],
236 ["completed", "Clicked in the preview browser"],
237 ["failed", "Failed to click in the preview browser"],
238 ["declined", "Declined to click in the preview browser"],
239 ["stopped", "Stopped clicking in the preview browser"],
240 ["unknown", "Clicking in the preview browser"],
241 ])("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
242 expect(
243 resolveWorkEntryToolPresentation({
244 label: "T3-code.preview_click",
245 toolLifecycleStatus,
246 }),
247 ).toEqual({ displayName, icon: "browser" });
248 });
249​
250 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
251 const entry = { label: "T3-code.preview_click" };
252 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
253 "Clicking in the preview browser",
254 );
255 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
256 "Clicked in the preview browser",
257 );
258 expect(
259 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
260 ?.displayName,
261 ).toBe("Clicked in the preview browser");
262 expect(
263 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
264 ?.displayName,
265 ).toBe("Failed to click in the preview browser");
266 });
267​
268 it.each([
269 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
270 [
271 "preview_set_appearance",
272 "Setting preview browser appearance",
273 "Set preview browser appearance",
274 ],
275 [
276 "preview_snapshot",
277 "Taking a snapshot of the preview page",
278 "Took a snapshot of the preview page",
279 ],
280 [
281 "preview_recording_stop",
282 "Stopping recording the preview browser",
283 "Stopped recording the preview browser",
284 ],
285 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
286 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
287 [
288 "t3_worktree_handoff",
289 "Handing off thread to a git worktree",
290 "Handed off thread to a git worktree",
291 ],
292 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
293 const entry = { label: `t3-code.${tool}` };
294 expect(
295 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
296 ?.displayName,
297 ).toBe(running);
298 expect(
299 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
300 ).toBe(completed);
301 });
302​
303 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
304 expect(
305 resolveWorkEntryToolPresentation({
306 label: "mcp__t3_code__task_status",
307 toolTitle: "Check the child task",
308 }),
309 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
310 });
311​
312 it("does not brand unknown tools or another server's matching tool name", () => {
313 for (const label of [
314 "mcp__github__preview_click",
315 "t3-code.unknown_tool",
316 "t3-code.toString",
317 "Search files",
318 ]) {
319 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
320 }
321 expect(
322 resolveWorkEntryToolPresentation({
323 label: "preview_click",
324 toolData: { server: "another-server", tool: "preview_click" },
325 }),
326 ).toBeNull();
327 });
328});
329​
330describe("browser group summaries", () => {
331 const browserEntry: WorkLogPresentationEntry = {
332 label: "MCP tool call",
333 toolData: { server: "t3-code", tool: "preview_click" },
334 itemType: "mcp_tool_call",
335 toolLifecycleStatus: "completed",
336 tone: "tool",
337 };
338 const commandEntry: WorkLogPresentationEntry = {
339 label: "Ran command",
340 command: "/bin/bash -lc 'vp test run'",
341 itemType: "command_execution",
342 toolLifecycleStatus: "completed",
343 tone: "tool",
344 };
345​
346 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
347 const entries = Array.from({ length: count }, (_, index) => ({
348 ...browserEntry,
349 toolCallId: `browser-${index}`,
350 }));
351 expect(summarizeToolGroup(entries)).toBe(
352 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
353 );
354 expect(toolGroupSummaryKind(entries)).toBe("browser");
355 });
356​
357 it("combines command and browser counts in a single sentence", () => {
358 const entries = [
359 ...Array.from({ length: 4 }, () => commandEntry),
360 ...Array.from({ length: 15 }, () => browserEntry),
361 ];
362 expect(summarizeToolGroup(entries)).toBe("Ran 4 commands and used browser 15 times");
363 expect(toolGroupSummaryKind(entries)).toBe("mixed");
364 });
365​
366 it("preserves first-seen action ordering alongside non-browser tools", () => {
367 expect(
368 summarizeToolGroup([
369 browserEntry,
370 commandEntry,
371 {
372 ...browserEntry,
373 toolData: { server: "t3-code", tool: "task_status" },
374 },
375 ]),
376 ).toBe("Used browser 1 time, ran 1 command, and used 1 tool");
377 });
378​
379 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
380 expect(
381 summarizeToolGroup([
382 {
383 ...browserEntry,
384 command: "node inspect-page.js",
385 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
386 },
387 ]),
388 ).toBe("Used browser 1 time");
389 });
390​
391 it("keeps foreign tools and web searches out of the browser count", () => {
392 expect(
393 summarizeToolGroup([
394 browserEntry,
395 {
396 ...browserEntry,
397 label: "preview_click",
398 toolData: { server: "another-server", tool: "preview_click" },
399 },
400 { label: "Search", tone: "tool", itemType: "web_search" },
401 ]),
402 ).toBe("Used browser 1 time, used 1 tool, and searched the web 1 time");
403 });
404​
405 it("keeps browser screenshots in the browser count while preserving their image path", () => {
406 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
407 expect(summarizeToolGroup([entry])).toBe("Used browser 1 time");
408 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
409 });
410});
411​
412describe("command work-log details", () => {
413 it("extracts Claude result blocks and projected output", () => {
414 expect(
415 extractCommandOutputText({
416 result: {
417 content: [
418 { type: "text", text: "first" },
419 { type: "text", text: "second" },
420 ],
421 },
422 }),
423 ).toBe("first\nsecond");
424 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
425 "projected summary",
426 );
427 });
428​
429 it("only removes a detail with the matching tool-name prefix", () => {
430 expect(
431 commandDetailRepeatsCommand({
432 detail: "Bash: printf hello",
433 command: "printf hello",
434 rawCommand: null,
435 toolName: "Bash",
436 data: { toolName: "Bash", command: "printf hello" },
437 }),
438 ).toBe(true);
439 expect(
440 commandDetailRepeatsCommand({
441 detail: "warning: printf hello",
442 command: "printf hello",
443 rawCommand: null,
444 toolName: "Bash",
445 data: { toolName: "Bash", command: "printf hello" },
446 }),
447 ).toBe(false);
448 });
449​
450 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
451 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
452 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
453 expect(
454 commandDetailRepeatsCommand({
455 detail: truncated,
456 command,
457 rawCommand: null,
458 toolName: "Bash",
459 data: { toolName: "Bash", command },
460 }),
461 ).toBe(true);
462 expect(
463 commandDetailRepeatsCommand({
464 detail: "Bash: printf hello...",
465 command: "printf goodbye",
466 rawCommand: null,
467 toolName: "Bash",
468 data: { toolName: "Bash", command: "printf goodbye" },
469 }),
470 ).toBe(false);
471 });
472​
473 it("treats ACP command echoes as synthetic even without a tool kind", () => {
474 expect(
475 commandDetailRepeatsCommand({
476 detail: "pnpm test",
477 command: "pnpm test",
478 rawCommand: null,
479 toolName: undefined,
480 data: { toolCallId: "tool-1", command: "pnpm test" },
481 }),
482 ).toBe(true);
483 expect(
484 commandDetailRepeatsCommand({
485 detail: "pnpm test",
486 command: "pnpm test",
487 rawCommand: null,
488 toolName: undefined,
489 data: { command: "pnpm test" },
490 }),
491 ).toBe(false);
492 });
493});
494​
495describe("workEntryViewedImagePath", () => {
496 const entry = { label: "Read", tone: "tool" } as const;
497​
498 it("returns a single image path from supported read entries", () => {
499 expect(
500 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
501 ).toBe("assets/a.png");
502 expect(
503 workEntryViewedImagePath({
504 ...entry,
505 itemType: "dynamic_tool_call",
506 toolTitle: "Read file",
507 detail: "C:\\workspace\\a.webp",
508 }),
509 ).toBe("C:\\workspace\\a.webp");
510 expect(
511 workEntryViewedImagePath({
512 ...entry,
513 itemType: "dynamic_tool_call",
514 detail: 'Read: {"file_path":"truncated..."}',
515 viewedImagePath: " /workspace/reference image.webp ",
516 }),
517 ).toBe("/workspace/reference image.webp");
518 });
519​
520 it("rejects non-image, multi-line, and non-read details", () => {
521 expect(
522 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.txt" }),
523 ).toBeNull();
524 expect(
525 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.png\nb.png" }),
526 ).toBeNull();
527 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
528 });
529});
530​
531describe("toolGroupAction", () => {
532 it("groups legacy Claude image reads with other reads", () => {
533 expect(
534 toolGroupAction({
535 label: "Tool call",
536 tone: "tool",
537 itemType: "dynamic_tool_call",
538 viewedImagePath: "/workspace/reference.png",
539 }),
540 ).toBe("read");
541 });
542});
543​
544describe("resolveViewedImageAsset", () => {
545 const threadId = ThreadId.make("thread-1");
546​
547 it("serves t3 attachment paths in place like any other host path", () => {
548 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
549 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
550 resource: { _tag: "media-file", threadId, path },
551 alt: "11111111-1111-4111-8111-111111111111.png",
552 srcFragment: "",
553 });
554 });
555​
556 it("normalizes workspace image sources", () => {
557 expect(
558 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
559 threadId,
560 workspaceRoot: "/workspace",
561 }),
562 ).toEqual({
563 resource: {
564 _tag: "media-file",
565 threadId,
566 path: "/workspace/screens/logo.svg",
567 },
568 alt: "logo.svg",
569 srcFragment: "#mark",
570 });
571 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
572 });
573});