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