Open History to see the last commit
1import { ThreadId, TurnItemId } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import { describe, expect, it } from "vite-plus/test";
4import {
5 MAX_TOOL_OUTPUT_IMAGES,
6 MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH,
7 toolOutputImages,
8} from "@t3tools/shared/toolOutput";
9
10import { openCodeToolOutput, openCodeToolTurnItem } from "./toolItems.ts";
11
12describe("openCodeToolOutput", () => {
13 it("preserves inline raster file content and leaves other files unserved", () => {
14 const output = openCodeToolOutput("Preview complete", [
15 { uri: "https://example.com/preview.png", mime: "image/png" },
16 { uri: "file:///tmp/preview.png", mime: "image/png" },
17 { uri: "data:image/svg+xml;base64,AAAA", mime: "image/svg+xml" },
18 { uri: "data:image/png;base64,AAAA", mime: "image/jpeg" },
19 { uri: "data:image/png;base64,", mime: "image/png" },
20 { uri: "data:image/png;base64,iVBORw0KGgo=", mime: "image/png" },
21 ]);
22 expect(toolOutputImages(output)).toEqual([{ mimeType: "image/png", data: "iVBORw0KGgo=" }]);
23 expect(output).toEqual({
24 content: [
25 { type: "text", text: "Preview complete" },
26 { type: "image", mimeType: "image/png", data: "iVBORw0KGgo=" },
27 ],
28 });
29 expect(openCodeToolOutput("Text result")).toBe("Text result");
30 const oversized = "A".repeat(MAX_TOOL_OUTPUT_IMAGE_BASE64_LENGTH + 4);
31 const bounded = openCodeToolOutput(
32 "Preview complete",
33 Array.from({ length: MAX_TOOL_OUTPUT_IMAGES + 2 }, (_, index) => ({
34 uri: `data:image/png;base64,${index === 0 ? oversized : "AQ=="}`,
35 mime: "image/png",
36 })),
37 );
38 expect(typeof bounded === "object" ? bounded.content.length : 0).toBe(
39 MAX_TOOL_OUTPUT_IMAGES + 1,
40 );
41 const images = toolOutputImages(bounded);
42 expect(images[0]?.data?.length).toBe(oversized.length);
43 expect(images[1]?.data).toBe("AQ==");
44 });
45});
46
47describe("openCodeToolTurnItem", () => {
48 it.each([
49 {
50 name: "deletion",
51 toolName: "edit",
52 input: { oldString: "old\n", newString: "", newText: "fallback" },
53 expected: { oldStr: "old\n", newStr: "" },
54 },
55 {
56 name: "whitespace replacement",
57 toolName: "edit",
58 input: { oldString: " \t", newString: "\n\t" },
59 expected: { oldStr: " \t", newStr: "\n\t" },
60 },
61 {
62 name: "empty write",
63 toolName: "write",
64 input: { content: "" },
65 expected: { newStr: "" },
66 },
67 ])("preserves exact file content for $name", ({ toolName, input, expected }) => {
68 const item = openCodeToolTurnItem(
69 {
70 id: TurnItemId.make("tool-item"),
71 threadId: ThreadId.make("thread"),
72 runId: null,
73 nodeId: null,
74 providerThreadId: null,
75 providerTurnId: null,
76 nativeItemRef: null,
77 parentItemId: null,
78 ordinal: 0,
79 status: "completed",
80 title: toolName,
81 startedAt: null,
82 completedAt: null,
83 updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
84 },
85 {
86 name: toolName,
87 input: { filePath: "src/index.ts", ...input },
88 output: undefined,
89 completedMetadata: undefined,
90 },
91 );
92 expect(item).toMatchObject({ type: "file_change", fileName: "src/index.ts", ...expected });
93 });
94
95 it.each([
96 { names: ["first", "second"], listed: true },
97 { names: ["first"], listed: false },
98 ])("names the files an apply_patch added: $names", ({ names, listed }) => {
99 const patch = (name: string) =>
100 `Index: /workspace/${name}.txt\n${"=".repeat(67)}\n--- /workspace/${name}.txt\n+++ /workspace/${name}.txt\n@@ -0,0 +1,1 @@\n+${name}\n`;
101 const diff = `${names.map(patch).join("\n")}\n`;
102 const item = openCodeToolTurnItem(
103 {
104 id: TurnItemId.make("patch-item"),
105 threadId: ThreadId.make("thread"),
106 runId: null,
107 nodeId: null,
108 providerThreadId: null,
109 providerTurnId: null,
110 nativeItemRef: null,
111 parentItemId: null,
112 ordinal: 0,
113 status: "completed",
114 title: "apply_patch",
115 startedAt: null,
116 completedAt: null,
117 updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
118 },
119 // Captured from OpenCode 1.19.4.
120 {
121 name: "apply_patch",
122 input: { patchText: names.map((name) => `*** Add File: ${name}.txt\n+${name}`).join("\n") },
123 output: "Success. Updated the following files",
124 completedMetadata: {
125 diff,
126 files: names.map((name) => ({ filePath: `/workspace/${name}.txt`, type: "add" })),
127 },
128 },
129 );
130 const changes = names.map((name) => ({ operation: "add", path: `/workspace/${name}.txt` }));
131 expect(item).toMatchObject({ fileName: "/workspace/first.txt", diffStr: diff });
132 expect(item.type === "file_change" && item.changes).toEqual(listed ? changes : undefined);
133 });
134
135 it("splits grep output into one result per matched line", () => {
136 const item = openCodeToolTurnItem(
137 {
138 id: TurnItemId.make("grep-item"),
139 threadId: ThreadId.make("thread"),
140 runId: null,
141 nodeId: null,
142 providerThreadId: null,
143 providerTurnId: null,
144 nativeItemRef: null,
145 parentItemId: null,
146 ordinal: 0,
147 status: "completed",
148 title: "grep",
149 startedAt: null,
150 completedAt: null,
151 updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
152 },
153 {
154 name: "grep",
155 input: { pattern: "export const \\w+", path: "/repo/src" },
156 // Captured from OpenCode 2.4.1.
157 output:
158 'Found 2 matches\n/repo/src/one.ts:\n Line 1: export const heading = "intro";\n\n\n/repo/src/two.ts:\n Line 1: export const second = 2;',
159 completedMetadata: undefined,
160 },
161 );
162 expect(item).toMatchObject({
163 type: "file_search",
164 pattern: "export const \\w+",
165 results: [
166 { fileName: "/repo/src/one.ts", line: 1, preview: 'export const heading = "intro";' },
167 { fileName: "/repo/src/two.ts", line: 1, preview: "export const second = 2;" },
168 ],
169 });
170 });
171
172 it("keeps glob paths that look like grep matches", () => {
173 const item = openCodeToolTurnItem(
174 {
175 id: TurnItemId.make("glob-item"),
176 threadId: ThreadId.make("thread"),
177 runId: null,
178 nodeId: null,
179 providerThreadId: null,
180 providerTurnId: null,
181 nativeItemRef: null,
182 parentItemId: null,
183 ordinal: 0,
184 status: "completed",
185 title: "glob",
186 startedAt: null,
187 completedAt: null,
188 updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
189 },
190 {
191 name: "glob",
192 input: { pattern: "*.ts", path: "/workspace/src" },
193 // Captured from OpenCode 1.19.4.
194 output: "/workspace/src/index.ts\n/workspace/src/rules:12:strict.ts",
195 completedMetadata: { count: 2, truncated: false },
196 },
197 );
198 expect(item).toMatchObject({
199 type: "file_search",
200 title: "Searched files *.ts in src",
201 results: [
202 {
203 fileName: "/workspace/src",
204 preview: "/workspace/src/index.ts\n/workspace/src/rules:12:strict.ts",
205 },
206 ],
207 });
208 });
209
210 it.each([
211 { toolName: "grep", field: "preview" },
212 { toolName: "websearch", field: "snippet" },
213 ])("keeps the $toolName preview clip on a whole character", ({ toolName, field }) => {
214 const item = openCodeToolTurnItem(
215 {
216 id: TurnItemId.make("search-item"),
217 threadId: ThreadId.make("thread"),
218 runId: null,
219 nodeId: null,
220 providerThreadId: null,
221 providerTurnId: null,
222 nativeItemRef: null,
223 parentItemId: null,
224 ordinal: 0,
225 status: "completed",
226 title: toolName,
227 startedAt: null,
228 completedAt: null,
229 updatedAt: DateTime.makeUnsafe("2026-11-04T00:00:00Z"),
230 },
231 {
232 name: toolName,
233 input: { query: "example" },
234 output: `${"a".repeat(7_999)}😀a`,
235 completedMetadata: undefined,
236 },
237 );
238 expect(item).toMatchObject({ results: [{ [field]: "a".repeat(7_999) }] });
239 });
240});