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

presentation.test.ts

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