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 resolveWorkEntryToolPresentation,
8 toolItemForDisplay,
9 type WorkLogPresentationEntry,
10 workEntryIndicatesToolFailure,
11 workEntryDisplayIndicatesToolFailure,
12 workEntryIndicatesToolSuccess,
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 exitCode: 0,
36 ...fields,
37 };
38}
39
40describe("workEntryIndicatesToolFailure", () => {
41 const base = {
42 id: "w1",
43 createdAt: "2026-01-01T00:00:00.000Z",
44 label: "Read",
45 };
46
47 it.each([
48 [{ exitCode: 2 }, true],
49 [{ output: "sh: missing-command: command not found" }, true],
50 [{ output: `${"x".repeat(32_768)} command not found` }, false],
51 ] as const)(
52 "preserves command failure state after removing displayed output: %j",
53 (fields, failed) => {
54 const structuredPayload = commandItem(fields);
55 const entry: WorkLogPresentationEntry = {
56 ...base,
57 tone: "tool",
58 itemType: "command_execution",
59 toolLifecycleStatus: "completed",
60 structuredPayload,
61 };
62 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(failed);
63 expect(workEntryIndicatesToolSuccess(entry)).toBe(!failed);
64 expect(JSON.stringify(toolItemForDisplay(structuredPayload))).not.toContain('"output":');
65 },
66 );
67
68 it("ignores failure phrases in reasoning text", () => {
69 const entry: WorkLogPresentationEntry = {
70 ...base,
71 label: "Thinking",
72 tone: "thinking",
73 itemType: "reasoning",
74 toolLifecycleStatus: "completed",
75 detail: "That exit code 2 is just from listing the deleted directory, which is expected.",
76 };
77 expect(workEntryIndicatesToolFailure(entry)).toBe(false);
78 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
79 });
80
81 it("does not treat error text in a command as rendered failure", () => {
82 const entry = {
83 ...base,
84 label: "Ran command",
85 tone: "tool",
86 toolLifecycleStatus: "completed",
87 command: 'rg "file not found"',
88 detail: "Found 3 matches",
89 } satisfies WorkLogPresentationEntry;
90
91 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(false);
92
93 expect(workEntryIndicatesToolFailure(entry)).toBe(true);
94 expect(workEntryDisplayIndicatesToolFailure({ ...entry, detail: "File not found" })).toBe(true);
95 });
96});
97
98describe("resolveWorkEntryToolPresentation", () => {
99 it("shows returned MCP errors as failures even in the live activity row", () => {
100 const entry: WorkLogPresentationEntry = {
101 id: "clone",
102 createdAt: "2026-09-19T00:00:00.000Z",
103 tone: "tool",
104 label: "T3-code.t3_project_clone",
105 toolLifecycleStatus: "inProgress",
106 itemType: "dynamic_tool",
107 toolData: { output: { isError: true } },
108 };
109 expect(resolveWorkEntryToolPresentation(entry)?.displayName).toBe(
110 "Failed to clone a repository",
111 );
112 expect(workEntryDisplayIndicatesToolFailure(entry)).toBe(true);
113 expect(workEntryIndicatesToolSuccess(entry)).toBe(false);
114 const childFailure = {
115 ...entry,
116 label: "T3-code.task_status",
117 toolLifecycleStatus: "completed" as const,
118 toolData: { output: { taskId: "child", status: "failed", summary: "command not found" } },
119 };
120 expect(workEntryDisplayIndicatesToolFailure(childFailure)).toBe(false);
121 expect(workEntryIndicatesToolSuccess(childFailure)).toBe(true);
122 });
123 it.each([
124 "mcp__t3-code__preview_click",
125 "mcp__t3_code__preview_click",
126 "mcp__t3code__preview_click",
127 "T3-code.preview_click",
128 "t3-code · preview_click completed",
129 "t3_code/preview_click",
130 "preview_click",
131 ])("recognizes browser tool names across providers: %s", (label) => {
132 expect(resolveWorkEntryToolPresentation({ label })).toEqual({
133 displayName: "Clicking in the preview browser",
134 icon: "browser",
135 });
136 });
137
138 it("uses structured MCP identity when the provider supplies a custom title", () => {
139 expect(
140 resolveWorkEntryToolPresentation({
141 label: "Tool call complete",
142 toolTitle: "Inspect the current page",
143 toolData: { server: "t3-code", tool: "preview_snapshot", result: { title: "Example" } },
144 }),
145 ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" });
146 });
147
148 it("uses the summary's state only when the provider omitted a lifecycle status", () => {
149 const entry = { label: "T3-code.preview_click" };
150 expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe(
151 "Clicking in the preview browser",
152 );
153 expect(resolveWorkEntryToolPresentation(entry, "completed")?.displayName).toBe(
154 "Clicked in the preview browser",
155 );
156 expect(
157 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "completed" }, "inProgress")
158 ?.displayName,
159 ).toBe("Clicked in the preview browser");
160 expect(
161 resolveWorkEntryToolPresentation({ ...entry, toolLifecycleStatus: "failed" }, "completed")
162 ?.displayName,
163 ).toBe("Failed to click in the preview browser");
164 });
165
166 it.each([
167 ["https://github.com/acme/web/pull/7", "PR #7"],
168 ["https://gitlab.com/acme/web/-/merge_requests/7", "MR #7"],
169 ])("names a linked change request with its host's term (%s)", (url, target) => {
170 expect(
171 resolveWorkEntryToolPresentation({
172 label: "T3-code.link_pull_request",
173 toolLifecycleStatus: "completed",
174 toolData: { arguments: { url } },
175 })?.displayName,
176 ).toMatch(new RegExp(` ${target}$`));
177 });
178
179 it("does not brand unknown tools or another server's matching tool name", () => {
180 for (const label of [
181 "mcp__github__preview_click",
182 "t3-code.unknown_tool",
183 "t3-code.toString",
184 "Search files",
185 ]) {
186 expect(resolveWorkEntryToolPresentation({ label })).toBeNull();
187 }
188 expect(
189 resolveWorkEntryToolPresentation({
190 label: "preview_click",
191 toolData: { server: "another-server", tool: "preview_click" },
192 }),
193 ).toBeNull();
194 });
195});