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

presentation.test.ts

294 lines9.8 KB

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