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

presentation.test.ts

317 lines10.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { ThreadId, TurnItemId, type OrchestrationV2TurnItem } from "@t3tools/contracts";
4import * as DateTime from "effect/DateTime";
5import { compactT3McpToolOutput } from "@t3tools/shared/toolOutput";
6​
7import {
8 resolveViewedImageAsset,
9 resolveWorkEntryToolPresentation,
10 toolItemForDisplay,
11 type WorkLogPresentationEntry,
12 workEntryIndicatesToolFailure,
13 workEntryDisplayIndicatesToolFailure,
14 workEntriesViewedImagePaths,
15 workEntriesViewedImages,
16 workEntryViewedImagePath,
17} from "./presentation.js";
18​
19function commandItem(
20 fields: Partial<Extract<OrchestrationV2TurnItem, { type: "command_execution" }>> = {},
21): OrchestrationV2TurnItem {
22 return {
23 id: TurnItemId.make("command"),
24 threadId: ThreadId.make("thread"),
25 runId: null,
26 nodeId: null,
27 providerThreadId: null,
28 providerTurnId: null,
29 nativeItemRef: null,
30 parentItemId: null,
31 ordinal: 1,
32 status: "completed",
33 title: null,
34 startedAt: null,
35 completedAt: null,
36 updatedAt: DateTime.makeUnsafe("2026-09-08T00:00:00.000Z"),
37 type: "command_execution",
38 input: 'rg "command not found"',
39 ...fields,
40 };
42​
43describe("workEntryIndicatesToolFailure", () => {
44 const base = {
45 id: "w1",
46 createdAt: "2026-01-01T00:00:00.000Z",
47 label: "Read",
48 };
49​
50 it.each([
51 [{ exitCode: 2 }, true],
52 [{ output: "sh: missing-command: command not found" }, true],
53 [{ output: `${"x".repeat(32_768)} command not found` }, false],
54 ] as const)(
55 "preserves command failure state after removing displayed output: %j",
56 (fields, failed) => {
57 const structuredPayload = commandItem(fields);
58 const entry: WorkLogPresentationEntry = {
59 ...base,
60 tone: "tool",
61 itemType: "command_execution",
62 toolLifecycleStatus: "completed",
63 structuredPayload,
64 };
65 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(failed);
66 expect(JSON.stringify(toolItemForDisplay(structuredPayload))).not.toContain('"output":');
67 },
68 );
69​
70 it.each([
71 "logs/app.log:12: Error: ENOENT: no such file or directory",
72 "fixtures/ci.txt:3: Process exited with exit code 2",
73 ])("trusts a zero exit code over failure text in the output and command: %s", (output) => {
74 const entry: WorkLogPresentationEntry = {
75 ...base,
76 tone: "tool",
77 itemType: "command_execution",
78 toolLifecycleStatus: "completed",
79 command: 'rg -n "ENOENT|exit code" logs fixtures',
80 structuredPayload: commandItem({ output, exitCode: 0, outputIndicatesFailure: true }),
81 };
82 expect(workEntryIndicatesToolFailure(entry)).toBe(false);
83 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
84 });
85​
86 it("ignores failure phrases in reasoning text", () => {
87 const entry: WorkLogPresentationEntry = {
88 ...base,
89 label: "Thinking",
90 tone: "thinking",
91 itemType: "reasoning",
92 toolLifecycleStatus: "completed",
93 detail: "That exit code 2 is just from listing the deleted directory, which is expected.",
94 };
95 expect(workEntryIndicatesToolFailure(entry)).toBe(false);
96 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
97 });
98​
99 it("does not treat error text in a command as rendered failure", () => {
100 const entry = {
101 ...base,
102 label: "Ran command",
103 tone: "tool",
104 toolLifecycleStatus: "completed",
105 command: 'rg "file not found"',
106 detail: "Found 3 matches",
107 } satisfies WorkLogPresentationEntry;
108​
109 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
110​
111 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
112 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
113 });
114});
115​
116describe("resolveWorkEntryToolPresentation", () => {
117 it("shows returned MCP errors as failures even in the live activity row", () => {
118 const entry: WorkLogPresentationEntry = {
119 id: "clone",
120 createdAt: "2026-09-19T00:00:00.000Z",
121 tone: "tool",
122 label: "T3-code.t3_project_clone",
123 toolLifecycleStatus: "inProgress",
124 itemType: "dynamic_tool",
125 toolData: { output: { isError: true } },
126 };
127 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
128 "Failed to clone a repository",
129 );
130 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
131 const childFailure = {
132 ...entry,
133 label: "T3-code.task_status",
134 toolLifecycleStatus: "completed" as const,
135 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
136 };
137 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
138 });
139 it.each([
140 "mcp__t3-code__preview_click",
141 "mcp__t3_code__preview_click",
142 "mcp__t3code__preview_click",
143 "T3-code.preview_click",
144 "t3-code · preview_click completed",
145 "t3_code/preview_click",
146 "preview_click",
147 ])("recognizes browser tool names across providers: %s", (label) => {
148 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
149 displayName: "Clicking in the preview browser",
150 icon: "browser",
151 });
152 });
153​
154 it("uses structured MCP identity when the provider supplies a custom title", () => {
155 expect(
156 resolveWorkEntryToolPresentation({
157 label: "Tool call complete",
158 toolTitle: "Inspect the current page",
159 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
160 }),
161 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
162 });
163​
164 it("names the reported outcome when it contradicts the row's verb", () => {
165 const row = (tool: string, output: unknown) =>
166 resolveWorkEntryToolPresentation({
167 label: `T3-code.${tool}`,
168 toolLifecycleStatus: "completed",
169 toolData: { output: compactT3McpToolOutput(`t3-code.${tool}`, output) },
170 })?.displayName;
171 expect(row("pull_request_create", { number: 470, created: false })).toBe(
172 "Found 1 open pull request",
173 );
174 expect(row("pull_request_create", { number: 470, created: true })).toBe(
175 "Opened a pull request",
176 );
177 expect(row("ci_rerun", { number: 883, checks: [] })).toBe("Found no failed checks to re-run");
178 expect(row("ci_rerun", { number: 883, checks: [{ name: "ci", rerun: true }] })).toBe(
179 "Re-ran CI",
180 );
181 expect(row("pull_request_merge", { number: 1, state: "open" })).toBe(
182 "Queued 1 pull request to merge",
183 );
184 });
185​
186 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
187 const entry = { label: "T3-code.preview_click" };
188 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
189 "Clicking in the preview browser",
190 );
191 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
192 "Clicked in the preview browser",
193 );
194 expect(
195 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
196 ?.displayName,
197 ).toBe("Clicked in the preview browser");
198 expect(
199 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
200 ?.displayName,
201 ).toBe("Failed to click in the preview browser");
202 });
203​
204 it.each([
205 ["https://github.com/acme/web/pull/7", "PR #7"],
206 ["https://gitlab.com/acme/web/-/merge_requests/7", "MR #7"],
207 ])("names a linked change request with its host's term (%s)", (url, target) => {
208 expect(
209 resolveWorkEntryToolPresentation({
210 label: "T3-code.link_pull_request",
211 toolLifecycleStatus: "completed",
212 toolData: { arguments: { url } },
213 })?.displayName,
214 ).toMatch(new RegExp(` ${target}$`));
215 });
216​
217 it("does not brand unknown tools or another server's matching tool name", () => {
218 for (const label of [
219 "mcp__github__preview_click",
220 "t3-code.unknown_tool",
221 "t3-code.toString",
222 "Search files",
223 ]) {
224 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
225 }
226 expect(
227 resolveWorkEntryToolPresentation({
228 label: "preview_click",
229 toolData: { server: "another-server", tool: "preview_click" },
230 }),
231 ).toBeNull();
232 });
233});
234​
235describe("resolveViewedImageAsset", () => {
236 it("requests the file the agent read when its name holds %, # or ?", () => {
237 const path = "/Users/me/.grok/sessions/%2FUsers%2Fme%2Fproject/shot #1?/1.jpg";
238 expect(
239 resolveViewedImageAsset(path, { threadId: ThreadId.make("thread"), workspaceRoot: "/repo" }),
240 ).toEqual({
241 resource: { _tag: "media-file", threadId: ThreadId.make("thread"), path },
242 alt: "1.jpg",
243 srcFragment: "",
244 });
245 });
246​
247 it("still decodes a file URL", () => {
248 expect(
249 resolveViewedImageAsset("file:///repo/my%20shot.png", { threadId: ThreadId.make("thread") })
250 ?.resource.path,
251 ).toBe("/repo/my shot.png");
252 });
253});
254​
255describe("workEntryViewedImagePath", () => {
256 it("withholds a Read's image preview until the Read has finished", () => {
257 const read: WorkLogPresentationEntry = {
258 id: "read",
259 createdAt: "2026-01-01T00:00:00.000Z",
260 label: "Read",
261 tone: "tool",
262 itemType: "dynamic_tool",
263 viewedImagePath: "/repo/out/later3.png",
264 };
265​
266 expect(workEntryViewedImagePath({ ...read, toolLifecycleStatus: "inProgress" })).toBeNull();
267 expect(workEntryViewedImagePath({ ...read, toolLifecycleStatus: "completed" })).toBe(
268 "/repo/out/later3.png",
269 );
270 });
271});
272​
273describe("workEntriesViewedImages", () => {
274 const read = (
275 id: string,
276 path: string,
277 toolLifecycleStatus: NonNullable<WorkLogPresentationEntry["toolLifecycleStatus"]>,
278 ): WorkLogPresentationEntry => ({
279 id,
280 createdAt: "2026-01-01T00:00:00.000Z",
281 label: "Read",
282 tone: "tool",
283 itemType: "dynamic_tool",
284 viewedImagePath: path,
285 toolLifecycleStatus,
286 });
287​
288 it("marks an image pending until a Read of it finishes", () => {
289 expect(
290 workEntriesViewedImages([
291 read("done", "/repo/a.png", "completed"),
292 read("running", "/repo/b.png", "inProgress"),
293 read("missing", "/repo/c.png", "failed"),
294 read("again", "/repo/a.png", "inProgress"),
295 ]),
296 ).toEqual([
297 { path: "/repo/a.png", pending: false },
298 { path: "/repo/b.png", pending: true },
299 ]);
300 expect(
301 workEntriesViewedImages([
302 read("running", "/repo/b.png", "inProgress"),
303 read("done", "/repo/b.png", "completed"),
304 ]),
305 ).toEqual([{ path: "/repo/b.png", pending: false }]);
306 });
307​
308 it("orders the finished images like the strip shows them", () => {
309 expect(
310 workEntriesViewedImagePaths([
311 read("first", "/repo/a.png", "inProgress"),
312 read("other", "/repo/b.png", "completed"),
313 read("again", "/repo/a.png", "completed"),
314 ]),
315 ).toEqual(["/repo/a.png", "/repo/b.png"]);
316 });
317});