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

presentation.test.ts

191 lines6.4 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 resolveWorkEntryToolPresentation,
8 toolItemForDisplay,
9 type WorkLogPresentationEntry,
10 workEntryIndicatesToolFailure,
11 workEntryDisplayIndicatesToolFailure,
12} from "./presentation.js";
13​
14function commandItem(
15 fields: Partial<Extract<OrchestrationV2TurnItem, { type: "command_execution" }>> = {},
16): OrchestrationV2TurnItem {
17 return {
18 id: TurnItemId.make("command"),
19 threadId: ThreadId.make("thread"),
20 runId: null,
21 nodeId: null,
22 providerThreadId: null,
23 providerTurnId: null,
24 nativeItemRef: null,
25 parentItemId: null,
26 ordinal: 1,
27 status: "completed",
28 title: null,
29 startedAt: null,
30 completedAt: null,
31 updatedAt: DateTime.makeUnsafe("2026-09-08T00:00:00.000Z"),
32 type: "command_execution",
33 input: 'rg "command not found"',
34 exitCode: 0,
35 ...fields,
36 };
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("ignores failure phrases in reasoning text", () => {
67 const entry: WorkLogPresentationEntry = {
68 ...base,
69 label: "Thinking",
70 tone: "thinking",
71 itemType: "reasoning",
72 toolLifecycleStatus: "completed",
73 detail: "That exit code 2 is just from listing the deleted directory, which is expected.",
74 };
75 expect(workEntryIndicatesToolFailure(entry)).toBe(false);
76 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
77 });
78​
79 it("does not treat error text in a command as rendered failure", () => {
80 const entry = {
81 ...base,
82 label: "Ran command",
83 tone: "tool",
84 toolLifecycleStatus: "completed",
85 command: 'rg "file not found"',
86 detail: "Found 3 matches",
87 } satisfies WorkLogPresentationEntry;
88​
89 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
90​
91 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
92 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
93 });
94});
95​
96describe("resolveWorkEntryToolPresentation", () => {
97 it("shows returned MCP errors as failures even in the live activity row", () => {
98 const entry: WorkLogPresentationEntry = {
99 id: "clone",
100 createdAt: "2026-09-19T00:00:00.000Z",
101 tone: "tool",
102 label: "T3-code.t3_project_clone",
103 toolLifecycleStatus: "inProgress",
104 itemType: "dynamic_tool",
105 toolData: { output: { isError: true } },
106 };
107 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
108 "Failed to clone a repository",
109 );
110 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
111 const childFailure = {
112 ...entry,
113 label: "T3-code.task_status",
114 toolLifecycleStatus: "completed" as const,
115 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
116 };
117 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
118 });
119 it.each([
120 "mcp__t3-code__preview_click",
121 "mcp__t3_code__preview_click",
122 "mcp__t3code__preview_click",
123 "T3-code.preview_click",
124 "t3-code · preview_click completed",
125 "t3_code/preview_click",
126 "preview_click",
127 ])("recognizes browser tool names across providers: %s", (label) => {
128 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
129 displayName: "Clicking in the preview browser",
130 icon: "browser",
131 });
132 });
133​
134 it("uses structured MCP identity when the provider supplies a custom title", () => {
135 expect(
136 resolveWorkEntryToolPresentation({
137 label: "Tool call complete",
138 toolTitle: "Inspect the current page",
139 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
140 }),
141 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
142 });
143​
144 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
145 const entry = { label: "T3-code.preview_click" };
146 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
147 "Clicking in the preview browser",
148 );
149 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
150 "Clicked in the preview browser",
151 );
152 expect(
153 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
154 ?.displayName,
155 ).toBe("Clicked in the preview browser");
156 expect(
157 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
158 ?.displayName,
159 ).toBe("Failed to click in the preview browser");
160 });
161​
162 it.each([
163 ["https://github.com/acme/web/pull/7", "PR #7"],
164 ["https://gitlab.com/acme/web/-/merge_requests/7", "MR #7"],
165 ])("names a linked change request with its host's term (%s)", (url, target) => {
166 expect(
167 resolveWorkEntryToolPresentation({
168 label: "T3-code.link_pull_request",
169 toolLifecycleStatus: "completed",
170 toolData: { arguments: { url } },
171 })?.displayName,
172 ).toMatch(new RegExp(` ${target}$`));
173 });
174​
175 it("does not brand unknown tools or another server's matching tool name", () => {
176 for (const label of [
177 "mcp__github__preview_click",
178 "t3-code.unknown_tool",
179 "t3-code.toString",
180 "Search files",
181 ]) {
182 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
183 }
184 expect(
185 resolveWorkEntryToolPresentation({
186 label: "preview_click",
187 toolData: { server: "another-server", tool: "preview_click" },
188 }),
189 ).toBeNull();
190 });
191});