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

presentation.test.ts

451 lines14.3 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} from "./presentation.js";
16​
17describe("summarizeToolGroup", () => {
18 it.each(["command", "file-read", "file-change"])(
19 "keeps %s approvals out of tool execution counts",
20 (requestKind) => {
21 const approvals = [
22 {
23 label: "Approval requested",
24 sourceActivityKind: "approval.requested",
25 tone: "info",
26 requestKind,
27 },
28 {
29 label: "Approval resolved",
30 sourceActivityKind: "approval.resolved",
31 tone: "info",
32 requestKind,
33 },
34 {
35 label: "Provider approval response failed",
36 sourceActivityKind: "provider.approval.respond.failed",
37 tone: "error",
38 },
39 ] satisfies WorkLogPresentationEntry[];
40​
41 expect(
42 summarizeToolGroup([
43 ...approvals,
44 { label: "Read", tone: "tool", itemType: "dynamic_tool_call" },
45 ]),
46 ).toBe("Received 3 updates and used 1 tool");
47 expect(summarizeToolGroup(approvals)).toBe("Received 3 updates");
48 expect(toolGroupSummaryKind(approvals)).toBe("update");
49 },
50 );
51​
52 it("deduplicates named sources ahead of ordinary actions", () => {
53 const source = { key: "browser-use:chrome", name: "Chrome", kind: "integration" as const };
54 expect(
55 summarizeToolGroup([
56 { label: "Open page", tone: "tool", toolSource: source },
57 { label: "Inspect page", tone: "tool", toolSource: source },
58 {
59 label: "Ran command",
60 tone: "tool",
61 itemType: "command_execution",
62 command: "git status",
63 },
64 ]),
65 ).toBe("Used Chrome integration and ran 1 command");
66 });
67​
68 it("omits the integration suffix for special browser and computer sources", () => {
69 expect(
70 summarizeToolGroup([
71 {
72 label: "Inspect page",
73 tone: "tool",
74 toolSource: { key: "browser-use", name: "Browser", kind: "browser" },
75 },
76 {
77 label: "Click",
78 tone: "tool",
79 toolSource: { key: "computer-use", name: "Computer Use", kind: "computer" },
80 },
81 ]),
82 ).toBe("Used Browser and Computer Use");
83 });
84});
85​
86describe("resolveWorkEntryToolPresentation", () => {
87 it.each([
88 "mcp__t3-code__preview_click",
89 "mcp__t3_code__preview_click",
90 "mcp__t3code__preview_click",
91 "T3-code.preview_click",
92 "t3-code · preview_click completed",
93 "t3_code/preview_click",
94 "preview_click",
95 ])("recognizes browser tool names across providers: %s", (label) => {
96 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
97 displayName: "Clicking in the preview browser",
98 icon: "browser",
99 });
100 });
101​
102 it("uses structured MCP identity when the provider supplies a custom title", () => {
103 expect(
104 resolveWorkEntryToolPresentation({
105 label: "Tool call complete",
106 toolTitle: "Inspect the current page",
107 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
108 }),
109 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
110 });
111​
112 it.each([
113 ["inProgress", "Clicking in the preview browser"],
114 ["completed", "Clicked in the preview browser"],
115 ["failed", "Failed to click in the preview browser"],
116 ["declined", "Declined to click in the preview browser"],
117 ["stopped", "Stopped clicking in the preview browser"],
118 ["unknown", "Clicking in the preview browser"],
119 ])("describes the tool's own %s state", (toolLifecycleStatus, displayName) => {
120 expect(
121 resolveWorkEntryToolPresentation({
122 label: "T3-code.preview_click",
123 toolLifecycleStatus,
124 }),
125 ).toEqual({ displayName, icon: "browser" });
126 });
127​
128 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
129 const entry = { label: "T3-code.preview_click" };
130 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
131 "Clicking in the preview browser",
132 );
133 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
134 "Clicked in the preview browser",
135 );
136 expect(
137 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
138 ?.displayName,
139 ).toBe("Clicked in the preview browser");
140 expect(
141 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
142 ?.displayName,
143 ).toBe("Failed to click in the preview browser");
144 });
145​
146 it.each([
147 ["preview_type", "Typing in the preview browser", "Typed in the preview browser"],
148 [
149 "preview_set_appearance",
150 "Setting preview browser appearance",
151 "Set preview browser appearance",
152 ],
153 [
154 "preview_snapshot",
155 "Taking a snapshot of the preview page",
156 "Took a snapshot of the preview page",
157 ],
158 [
159 "preview_recording_stop",
160 "Stopping recording the preview browser",
161 "Stopped recording the preview browser",
162 ],
163 ["t3_thread_read", "Reading a T3 thread", "Read a T3 thread"],
164 ["t3_thread_send", "Sending to a T3 thread", "Sent to a T3 thread"],
165 [
166 "t3_worktree_handoff",
167 "Handing off thread to a git worktree",
168 "Handed off thread to a git worktree",
169 ],
170 ])("preserves verb forms and the rest of %s's label", (tool, running, completed) => {
171 const entry = { label: `t3-code.${tool}` };
172 expect(
173 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "inProgress" })
174 ?.displayName,
175 ).toBe(running);
176 expect(
177 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" })?.displayName,
178 ).toBe(completed);
179 });
180​
181 it("keeps T3 branding for non-browser tools and falls back to the original tool label", () => {
182 expect(
183 resolveWorkEntryToolPresentation({
184 label: "mcp__t3_code__task_status",
185 toolTitle: "Check the child task",
186 }),
187 ).toEqual({ displayName: "Getting delegated task status", icon: "t3-code" });
188 });
189​
190 it("does not brand unknown tools or another server's matching tool name", () => {
191 for (const label of [
192 "mcp__github__preview_click",
193 "t3-code.unknown_tool",
194 "t3-code.toString",
195 "Search files",
196 ]) {
197 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
198 }
199 expect(
200 resolveWorkEntryToolPresentation({
201 label: "preview_click",
202 toolData: { server: "another-server", tool: "preview_click" },
203 }),
204 ).toBeNull();
205 });
206});
207​
208describe("browser group summaries", () => {
209 const browserEntry: WorkLogPresentationEntry = {
210 label: "MCP tool call",
211 toolData: { server: "t3-code", tool: "preview_click" },
212 itemType: "mcp_tool_call",
213 toolLifecycleStatus: "completed",
214 tone: "tool",
215 };
216 const commandEntry: WorkLogPresentationEntry = {
217 label: "Ran command",
218 command: "/bin/bash -lc 'vp test run'",
219 itemType: "command_execution",
220 toolLifecycleStatus: "completed",
221 tone: "tool",
222 };
223​
224 it.each([1, 18])("counts %s browser calls separately from generic tools", (count) => {
225 const entries = Array.from({ length: count }, (_, index) => ({
226 ...browserEntry,
227 toolCallId: `browser-${index}`,
228 }));
229 expect(summarizeToolGroup(entries)).toBe(
230 `Used browser ${count} ${count === 1 ? "time" : "times"}`,
231 );
232 expect(toolGroupSummaryKind(entries)).toBe("browser");
233 });
234​
235 it("combines command and browser counts in a single sentence", () => {
236 const entries = [
237 ...Array.from({ length: 4 }, () => commandEntry),
238 ...Array.from({ length: 15 }, () => browserEntry),
239 ];
240 expect(summarizeToolGroup(entries)).toBe("Ran 4 commands and used browser 15 times");
241 expect(toolGroupSummaryKind(entries)).toBe("mixed");
242 });
243​
244 it("preserves first-seen action ordering alongside non-browser tools", () => {
245 expect(
246 summarizeToolGroup([
247 browserEntry,
248 commandEntry,
249 {
250 ...browserEntry,
251 toolData: { server: "t3-code", tool: "task_status" },
252 },
253 ]),
254 ).toBe("Used browser 1 time, ran 1 command, and used 1 tool");
255 });
256​
257 it("recognizes Claude browser identity without treating script metadata as a shell command", () => {
258 expect(
259 summarizeToolGroup([
260 {
261 ...browserEntry,
262 command: "node inspect-page.js",
263 toolData: { toolName: "mcp__t3_code__preview_evaluate" },
264 },
265 ]),
266 ).toBe("Used browser 1 time");
267 });
268​
269 it("keeps foreign tools and web searches out of the browser count", () => {
270 expect(
271 summarizeToolGroup([
272 browserEntry,
273 {
274 ...browserEntry,
275 label: "preview_click",
276 toolData: { server: "another-server", tool: "preview_click" },
277 },
278 { label: "Search", tone: "tool", itemType: "web_search" },
279 ]),
280 ).toBe("Used browser 1 time, used 1 tool, and searched the web 1 time");
281 });
282​
283 it("keeps browser screenshots in the browser count while preserving their image path", () => {
284 const entry = { ...browserEntry, viewedImagePath: "/workspace/page.png" };
285 expect(summarizeToolGroup([entry])).toBe("Used browser 1 time");
286 expect(workEntryViewedImagePath(entry)).toBe("/workspace/page.png");
287 });
288});
289​
290describe("command work-log details", () => {
291 it("extracts Claude result blocks and projected output", () => {
292 expect(
293 extractCommandOutputText({
294 result: {
295 content: [
296 { type: "text", text: "first" },
297 { type: "text", text: "second" },
298 ],
299 },
300 }),
301 ).toBe("first\nsecond");
302 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
303 "projected summary",
304 );
305 });
306​
307 it("only removes a detail with the matching tool-name prefix", () => {
308 expect(
309 commandDetailRepeatsCommand({
310 detail: "Bash: printf hello",
311 command: "printf hello",
312 rawCommand: null,
313 toolName: "Bash",
314 data: { toolName: "Bash", command: "printf hello" },
315 }),
316 ).toBe(true);
317 expect(
318 commandDetailRepeatsCommand({
319 detail: "warning: printf hello",
320 command: "printf hello",
321 rawCommand: null,
322 toolName: "Bash",
323 data: { toolName: "Bash", command: "printf hello" },
324 }),
325 ).toBe(false);
326 });
327​
328 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
329 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
330 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
331 expect(
332 commandDetailRepeatsCommand({
333 detail: truncated,
334 command,
335 rawCommand: null,
336 toolName: "Bash",
337 data: { toolName: "Bash", command },
338 }),
339 ).toBe(true);
340 expect(
341 commandDetailRepeatsCommand({
342 detail: "Bash: printf hello...",
343 command: "printf goodbye",
344 rawCommand: null,
345 toolName: "Bash",
346 data: { toolName: "Bash", command: "printf goodbye" },
347 }),
348 ).toBe(false);
349 });
350​
351 it("treats ACP command echoes as synthetic even without a tool kind", () => {
352 expect(
353 commandDetailRepeatsCommand({
354 detail: "pnpm test",
355 command: "pnpm test",
356 rawCommand: null,
357 toolName: undefined,
358 data: { toolCallId: "tool-1", command: "pnpm test" },
359 }),
360 ).toBe(true);
361 expect(
362 commandDetailRepeatsCommand({
363 detail: "pnpm test",
364 command: "pnpm test",
365 rawCommand: null,
366 toolName: undefined,
367 data: { command: "pnpm test" },
368 }),
369 ).toBe(false);
370 });
371});
372​
373describe("workEntryViewedImagePath", () => {
374 const entry = { label: "Read", tone: "tool" } as const;
375​
376 it("returns a single image path from supported read entries", () => {
377 expect(
378 workEntryViewedImagePath({ ...entry, requestKind: "file-read", detail: " assets/a.png " }),
379 ).toBe("assets/a.png");
380 expect(
381 workEntryViewedImagePath({
382 ...entry,
383 itemType: "dynamic_tool_call",
384 toolTitle: "Read file",
385 detail: "C:\\workspace\\a.webp",
386 }),
387 ).toBe("C:\\workspace\\a.webp");
388 expect(
389 workEntryViewedImagePath({
390 ...entry,
391 itemType: "dynamic_tool_call",
392 detail: 'Read: {"file_path":"truncated..."}',
393 viewedImagePath: " /workspace/reference image.webp ",
394 }),
395 ).toBe("/workspace/reference image.webp");
396 });
397​
398 it("rejects non-image, multi-line, and non-read details", () => {
399 expect(
400 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.txt" }),
401 ).toBeNull();
402 expect(
403 workEntryViewedImagePath({ ...entry, itemType: "image_view", detail: "a.png\nb.png" }),
404 ).toBeNull();
405 expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
406 });
407});
408​
409describe("toolGroupAction", () => {
410 it("groups legacy Claude image reads with other reads", () => {
411 expect(
412 toolGroupAction({
413 label: "Tool call",
414 tone: "tool",
415 itemType: "dynamic_tool_call",
416 viewedImagePath: "/workspace/reference.png",
417 }),
418 ).toBe("read");
419 });
420});
421​
422describe("resolveViewedImageAsset", () => {
423 const threadId = ThreadId.make("thread-1");
424​
425 it("serves t3 attachment paths in place like any other host path", () => {
426 const path = "/Users/demo/.t3/dev/attachments/11111111-1111-4111-8111-111111111111.png";
427 expect(resolveViewedImageAsset(path, { threadId, workspaceRoot: "/workspace" })).toEqual({
428 resource: { _tag: "media-file", threadId, path },
429 alt: "11111111-1111-4111-8111-111111111111.png",
430 srcFragment: "",
431 });
432 });
433​
434 it("normalizes workspace image sources", () => {
435 expect(
436 resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
437 threadId,
438 workspaceRoot: "/workspace",
439 }),
440 ).toEqual({
441 resource: {
442 _tag: "media-file",
443 threadId,
444 path: "/workspace/screens/logo.svg",
445 },
446 alt: "logo.svg",
447 srcFragment: "#mark",
448 });
449 expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
450 });
451});