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

presentation.test.ts

246 lines7.9 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 commandDetailRepeatsCommand,
8 extractCommandOutputText,
9 resolveWorkEntryToolPresentation,
10 toolItemForDisplay,
11 type WorkLogPresentationEntry,
12 workEntryIndicatesToolFailure,
13 workEntryDisplayIndicatesToolFailure,
14 workEntryIndicatesToolSuccess,
15} from "./presentation.js";
16​
17function commandItem(
18 fields: Partial<Extract<OrchestrationV2TurnItem, { type: "command_execution" }>> = {},
19): OrchestrationV2TurnItem {
20 return {
21 id: TurnItemId.make("command"),
22 threadId: ThreadId.make("thread"),
23 runId: null,
24 nodeId: null,
25 providerThreadId: null,
26 providerTurnId: null,
27 nativeItemRef: null,
28 parentItemId: null,
29 ordinal: 1,
30 status: "completed",
31 title: null,
32 startedAt: null,
33 completedAt: null,
34 updatedAt: DateTime.makeUnsafe("2026-09-08T00:00:00.000Z"),
35 type: "command_execution",
36 input: 'rg "command not found"',
37 exitCode: 0,
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(workEntryIndicatesToolSuccess(entry)).toBe(!failed);
66 expect(JSON.stringify(toolItemForDisplay(structuredPayload))).not.toContain('"output":');
67 },
68 );
69​
70 it("ignores failure phrases in reasoning text", () => {
71 const entry: WorkLogPresentationEntry = {
72 ...base,
73 label: "Thinking",
74 tone: "thinking",
75 itemType: "reasoning",
76 toolLifecycleStatus: "completed",
77 detail: "That exit code 2 is just from listing the deleted directory, which is expected.",
78 };
79 expect(workEntryIndicatesToolFailure(entry)).toBe(false);
80 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
81 });
82​
83 it("does not treat error text in a command as rendered failure", () => {
84 const entry = {
85 ...base,
86 label: "Ran command",
87 tone: "tool",
88 toolLifecycleStatus: "completed",
89 command: 'rg "file not found"',
90 detail: "Found 3 matches",
91 } satisfies WorkLogPresentationEntry;
92​
93 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
94​
95 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
96 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
97 });
98});
99​
100describe("resolveWorkEntryToolPresentation", () => {
101 it("shows returned MCP errors as failures even in the live activity row", () => {
102 const entry: WorkLogPresentationEntry = {
103 id: "clone",
104 createdAt: "2026-09-19T00:00:00.000Z",
105 tone: "tool",
106 label: "T3-code.t3_project_clone",
107 toolLifecycleStatus: "inProgress",
108 itemType: "dynamic_tool",
109 toolData: { output: { isError: true } },
110 };
111 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
112 "Failed to clone a repository",
113 );
114 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
115 expect(workEntryIndicatesToolSuccess(entry)).toBe(false);
116 const childFailure = {
117 ...entry,
118 label: "T3-code.task_status",
119 toolLifecycleStatus: "completed" as const,
120 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
121 };
122 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
123 expect(workEntryIndicatesToolSuccess(childFailure)).toBe(true);
124 });
125 it.each([
126 "mcp__t3-code__preview_click",
127 "mcp__t3_code__preview_click",
128 "mcp__t3code__preview_click",
129 "T3-code.preview_click",
130 "t3-code · preview_click completed",
131 "t3_code/preview_click",
132 "preview_click",
133 ])("recognizes browser tool names across providers: %s", (label) => {
134 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
135 displayName: "Clicking in the preview browser",
136 icon: "browser",
137 });
138 });
139​
140 it("uses structured MCP identity when the provider supplies a custom title", () => {
141 expect(
142 resolveWorkEntryToolPresentation({
143 label: "Tool call complete",
144 toolTitle: "Inspect the current page",
145 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
146 }),
147 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
148 });
149​
150 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
151 const entry = { label: "T3-code.preview_click" };
152 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
153 "Clicking in the preview browser",
154 );
155 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
156 "Clicked in the preview browser",
157 );
158 expect(
159 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
160 ?.displayName,
161 ).toBe("Clicked in the preview browser");
162 expect(
163 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
164 ?.displayName,
165 ).toBe("Failed to click in the preview browser");
166 });
167​
168 it("does not brand unknown tools or another server's matching tool name", () => {
169 for (const label of [
170 "mcp__github__preview_click",
171 "t3-code.unknown_tool",
172 "t3-code.toString",
173 "Search files",
174 ]) {
175 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
176 }
177 expect(
178 resolveWorkEntryToolPresentation({
179 label: "preview_click",
180 toolData: { server: "another-server", tool: "preview_click" },
181 }),
182 ).toBeNull();
183 });
184});
185​
186describe("command work-log details", () => {
187 it("extracts Claude result blocks and projected output", () => {
188 expect(
189 extractCommandOutputText({
190 result: {
191 content: [
192 { type: "text", text: "first" },
193 { type: "text", text: "second" },
194 ],
195 },
196 }),
197 ).toBe("first\nsecond");
198 expect(extractCommandOutputText({ rawOutput: { content: "projected summary" } })).toBe(
199 "projected summary",
200 );
201 });
202​
203 it("treats an ingestion-truncated echo of a long command as a repeat", () => {
204 const command = `git add -A && git commit -m "${"x".repeat(200)}"`;
205 const truncated = `Bash: ${command}`.slice(0, 177) + "...";
206 expect(
207 commandDetailRepeatsCommand({
208 detail: truncated,
209 command,
210 rawCommand: null,
211 toolName: "Bash",
212 data: { toolName: "Bash", command },
213 }),
214 ).toBe(true);
215 expect(
216 commandDetailRepeatsCommand({
217 detail: "Bash: printf hello...",
218 command: "printf goodbye",
219 rawCommand: null,
220 toolName: "Bash",
221 data: { toolName: "Bash", command: "printf goodbye" },
222 }),
223 ).toBe(false);
224 });
225​
226 it("treats ACP command echoes as synthetic even without a tool kind", () => {
227 expect(
228 commandDetailRepeatsCommand({
229 detail: "pnpm test",
230 command: "pnpm test",
231 rawCommand: null,
232 toolName: undefined,
233 data: { toolCallId: "tool-1", command: "pnpm test" },
234 }),
235 ).toBe(true);
236 expect(
237 commandDetailRepeatsCommand({
238 detail: "pnpm test",
239 command: "pnpm test",
240 rawCommand: null,
241 toolName: undefined,
242 data: { command: "pnpm test" },
243 }),
244 ).toBe(false);
245 });
246});