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

presentation.test.ts

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