Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { getDiffLineStat, getRenderablePatch, resolveFileDiffPath } from "./diffRendering";
3import { toolCallDiff } from "./toolCallDiff";
4
5describe("toolCallDiff", () => {
6 it("builds a diff from the before and after snippets of a completed edit", () => {
7 const { patch, content } = toolCallDiff(
8 {
9 fileName: "/workspace/src/file with spaces.ts",
10 oldStr: " validateRetries();\n return options;",
11 newStr: " validateRetries();\n validateAllowance();\n return options;",
12 status: "completed",
13 },
14 "/workspace",
15 );
16 expect(content).toEqual([]);
17 const rendered = getRenderablePatch(patch);
18 expect(rendered?.kind).toBe("files");
19 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
20 expect(resolveFileDiffPath(rendered.files[0]!)).toBe("src/file with spaces.ts");
21 expect(getDiffLineStat(rendered.files)).toEqual({ additions: 1, deletions: 0 });
22 });
23
24 it("renders recorded hunks without reading a repository, including renamed paths", () => {
25 for (const changes of [
26 undefined,
27 [{ operation: "update", oldPath: "old.ts", path: "new.ts" }],
28 ]) {
29 const { patch } = toolCallDiff({
30 fileName: "old.ts",
31 diffStr: "@@ -1 +1 @@\n-before\n+after\n",
32 ...(changes ? { changes } : {}),
33 });
34 const rendered = getRenderablePatch(patch);
35 expect(rendered?.kind).toBe("files");
36 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
37 expect(rendered.files).toHaveLength(1);
38 expect(resolveFileDiffPath(rendered.files[0]!)).toBe(changes ? "new.ts" : "old.ts");
39 expect(rendered.files[0]!.additionLines.join("")).toContain("after");
40 expect(rendered.files[0]!.deletionLines.join("")).toContain("before");
41 }
42 });
43
44 it("renders added and deleted file contents as a file diff", () => {
45 for (const operation of ["add", "delete"]) {
46 for (const [content, ending] of [
47 ["first\nsecond", ""],
48 ["@@ -0,0 +1 @@\n+content", "\n"],
49 ["@@ -1 +0,0 @@\n-content", "\n"],
50 ["first\nsecond", "\n"],
51 ["--- notes\nsecond", "\n"],
52 ["diff --git notes\nsecond", "\n"],
53 ["--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after", "\n"],
54 [
55 "diff --git a/embedded.ts b/embedded.ts\n--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after",
56 "\n",
57 ],
58 ] as const) {
59 const recordedContent = `${content}${ending}`;
60 const rendered = getRenderablePatch(
61 toolCallDiff({
62 fileName: "file with spaces.ts",
63 diffStr: recordedContent,
64 changes: [{ operation, path: "file with spaces.ts" }],
65 }).patch,
66 );
67 expect(rendered?.kind).toBe("files");
68 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
69 expect(resolveFileDiffPath(rendered.files[0]!)).toBe("file with spaces.ts");
70 const file = rendered.files[0]!;
71 const lines = content.split("\n").length;
72 expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);
73 expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);
74 expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(
75 recordedContent,
76 );
77 }
78 }
79 });
80
81 it("keeps content-only writes out of the patch and preserves empty recorded content", () => {
82 for (const operation of ["add", "delete"]) {
83 expect(
84 toolCallDiff({
85 fileName: "empty.ts",
86 diffStr: "",
87 changes: [{ operation, path: "empty.ts" }],
88 }).content,
89 ).toEqual([
90 {
91 label: operation === "add" ? "Recorded content after" : "Recorded content before",
92 text: "",
93 },
94 ]);
95 }
96 for (const status of ["running", "cancelled", "failed"] as const) {
97 expect(toolCallDiff({ fileName: "a.ts", oldStr: "before", newStr: "", status })).toEqual({
98 patch: undefined,
99 content: [
100 { label: "Recorded content before", text: "before" },
101 { label: "Recorded content after", text: "" },
102 ],
103 truncated: false,
104 });
105 }
106 expect(toolCallDiff({ fileName: "a.ts", newStr: "after" }).content).toEqual([
107 { label: "Recorded content after", text: "after" },
108 ]);
109 });
110
111 it("removes transport truncation markers before converting file contents", () => {
112 const suffix = "\n… output truncated for transport";
113 const diff = toolCallDiff({
114 fileName: "a.ts",
115 diffStr: `first\nsecond${suffix}`,
116 changes: [{ operation: "add", path: "a.ts" }],
117 });
118 expect(diff.truncated).toBe(true);
119 expect(diff.patch).not.toContain("output truncated");
120 expect(getRenderablePatch(diff.patch)?.kind).toBe("files");
121 expect(toolCallDiff({ fileName: "a.ts", newStr: `content${suffix}` })).toMatchObject({
122 content: [{ label: "Recorded content after", text: "content" }],
123 truncated: true,
124 });
125 expect(toolCallDiff({ fileName: "a.ts", oldStr: `before${suffix}`, newStr: "after" })).toEqual({
126 patch: undefined,
127 content: [
128 { label: "Recorded content before", text: "before" },
129 { label: "Recorded content after", text: "after" },
130 ],
131 truncated: true,
132 });
133 });
134
135 it("names files under the workspace relative to it in headers only", () => {
136 const root = "/Users/me/project";
137 const paths = (patch: string | undefined) => {
138 const rendered = getRenderablePatch(patch);
139 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
140 return rendered.files.map((file) => resolveFileDiffPath(file));
141 };
142 // Claude's Edit: absolute headers; hunk lines that look like headers keep the root.
143 const claude = `--- ${root}/docs/plan.md\n+++ ${root}/docs/plan.md\n@@ -2 +2 @@\n-- ${root}/x\n++ ${root}/y\n`;
144 const { patch } = toolCallDiff({ fileName: `${root}/docs/plan.md`, diffStr: claude }, root);
145 expect(paths(patch)).toEqual(["docs/plan.md"]);
146 expect(patch).toContain(`\n-- ${root}/x\n++ ${root}/y\n`);
147 // Synthesized headers, a trailing separator on the root, and a path outside it.
148 const hunk = "@@ -1 +1 @@\n-x\n+y\n";
149 expect(
150 paths(toolCallDiff({ fileName: `${root}/src/a.ts`, diffStr: hunk }, `${root}/`).patch),
151 ).toEqual(["src/a.ts"]);
152 expect(paths(toolCallDiff({ fileName: "/elsewhere/b.ts", diffStr: hunk }, root).patch)).toEqual(
153 ["/elsewhere/b.ts"],
154 );
155 // A relative path, and on POSIX a backslash that is part of a name, keep theirs.
156 for (const [fileName, workspace] of [
157 ["repo/src/a.ts", "/repo"],
158 ["/repo/back/slash/a.ts", "/repo/back\\slash"],
159 ] as const) {
160 const diffStr = `--- ${fileName}\n+++ ${fileName}\n${hunk}`;
161 expect(toolCallDiff({ fileName, diffStr }, workspace).patch).toBe(diffStr);
162 }
163 const backslash = toolCallDiff(
164 { fileName: "/repo/back\\slash/a.ts", diffStr: hunk },
165 "/repo/back\\slash",
166 ).patch;
167 expect(paths(backslash)).toEqual(["a.ts"]);
168 // Each file of a multi-file patch; a path that merely contains the root keeps it.
169 const twoFiles = [
170 `diff --git a/${root}/a.ts b/${root}/a.ts`,
171 `--- a/${root}/a.ts`,
172 `+++ b/${root}/a.ts`,
173 hunk.trimEnd(),
174 `diff --git a/backup${root}/b.ts b/backup${root}/b.ts`,
175 `--- a/backup${root}/b.ts`,
176 `+++ b/backup${root}/b.ts`,
177 hunk,
178 ].join("\n");
179 expect(
180 paths(toolCallDiff({ fileName: `${root}/a.ts`, diffStr: twoFiles }, root).patch),
181 ).toEqual(["a.ts", `backup${root}/b.ts`]);
182 // Only where a name starts: a quoted name with a space before the root.
183 expect(
184 paths(toolCallDiff({ fileName: `${root}/docs ${root}/a.md`, diffStr: hunk }, root).patch),
185 ).toEqual([`docs ${root}/a.md`]);
186 expect(
187 paths(toolCallDiff({ fileName: `/backup ${root}/a.md`, diffStr: hunk }, root).patch),
188 ).toEqual([`/backup ${root}/a.md`]);
189 // Windows paths, which a quoted header escapes, compared without case.
190 const windows = toolCallDiff(
191 { fileName: "C:\\repo\\src\\a.ts", diffStr: hunk },
192 "c:\\repo",
193 ).patch;
194 expect(paths(windows)).toEqual(["src\\a.ts"]);
195 });
196
197 it("keeps a tab inside a quoted Claude Write filename", () => {
198 const diffStr = '--- /dev/null\n+++ "/w/name\\tpart.txt"\n@@ -0,0 +1,1 @@\n+hello\n';
199 const rendered = getRenderablePatch(
200 toolCallDiff({ fileName: "/w/name\tpart.txt", diffStr }, "/w").patch,
201 );
202 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
203 expect(rendered.files.map((file) => resolveFileDiffPath(file))).toEqual(["name\tpart.txt"]);
204 expect(getDiffLineStat(rendered.files)).toEqual({ additions: 1, deletions: 0 });
205 });
206
207 it("preserves a multi-file patch whose first change is an empty file", () => {
208 const diffStr = [
209 'diff --git "a/empty.ts" "b/empty.ts"',
210 "--- /dev/null",
211 '+++ "b/empty.ts"',
212 'diff --git "a/c.ts" "b/c.ts"',
213 '--- "a/c.ts"',
214 '+++ "b/c.ts"',
215 "@@ -1 +1 @@",
216 "-before",
217 "+after",
218 'diff --git "a/gone.ts" "b/gone.ts"',
219 '--- "a/gone.ts"',
220 "+++ /dev/null",
221 "",
222 ].join("\n");
223 const { patch } = toolCallDiff({
224 fileName: "empty.ts",
225 diffStr,
226 changes: [
227 { operation: "add", path: "empty.ts" },
228 { operation: "update", path: "c.ts" },
229 { operation: "delete", path: "gone.ts" },
230 ],
231 });
232 expect(patch).toBe(diffStr);
233 });
234
235 it("keeps both files of a multi-file add whose first path has a control character", () => {
236 const diffStr = [
237 'diff --git "a//w/odd\\aname.txt" "b//w/odd\\aname.txt"',
238 "--- /dev/null",
239 '+++ "b//w/odd\\aname.txt"',
240 "@@ -0,0 +1,1 @@",
241 "+hello",
242 'diff --git "a//w/z-normal.txt" "b//w/z-normal.txt"',
243 "--- /dev/null",
244 '+++ "b//w/z-normal.txt"',
245 "@@ -0,0 +1,1 @@",
246 "+hello",
247 "",
248 ].join("\n");
249 const { patch } = toolCallDiff(
250 {
251 fileName: "/w/odd\u0007name.txt",
252 diffStr,
253 status: "completed",
254 changes: [
255 { operation: "add", path: "/w/odd\u0007name.txt" },
256 { operation: "add", path: "/w/z-normal.txt" },
257 ],
258 },
259 "/w",
260 );
261 const rendered = getRenderablePatch(patch);
262 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
263 expect(rendered.files.map(resolveFileDiffPath)).toEqual(["odd\u0007name.txt", "z-normal.txt"]);
264 expect(rendered.files.map((file) => file.additionLines)).toEqual([["hello\n"], ["hello\n"]]);
265 });
266
267 it("keeps multi-file OpenCode patches whole but wraps a lone added file's Index: contents", () => {
268 // Captured from OpenCode 1.19.4 apply_patch.
269 const file = (path: string, hunk = "") =>
270 `Index: ${path}\n${"=".repeat(67)}\n--- ${path}\n+++ ${path}\n${hunk}\n`;
271 const added = file("/w/new.txt", "@@ -0,0 +1,1 @@\n+new\n");
272 const add = { operation: "add", path: "/w/new.txt" };
273 for (const [diffStr, second] of [
274 [added + file("/w/empty.txt"), { operation: "add", path: "/w/empty.txt" }],
275 [
276 added + file("/w/a.txt", "@@ -1 +1 @@\n-old\n+changed\n"),
277 { operation: "move", path: "/w/b.txt", oldPath: "/w/a.txt" },
278 ],
279 [
280 `${added}\nIndex: /w/cut.txt\n${"=".repeat(67)}\n--- /w/cut.txt\n+++ /w/c`,
281 { operation: "add", path: "/w/cut.txt" },
282 ],
283 ] as const) {
284 expect(toolCallDiff({ fileName: add.path, diffStr, changes: [add, second] }).patch).toBe(
285 diffStr,
286 );
287 }
288 expect(toolCallDiff({ fileName: add.path, diffStr: added, changes: [add] }).patch).toContain(
289 "+Index: /w/new.txt\n",
290 );
291 });
292
293 it("wraps one added file whose content repeats its own hunk-less header", () => {
294 const diffStr = ["diff --git a/a.patch b/a.patch", "diff --git a/a.patch b/a.patch", ""].join(
295 "\n",
296 );
297 const { patch } = toolCallDiff({
298 fileName: "a.patch",
299 diffStr,
300 changes: [{ operation: "add", path: "a.patch" }],
301 });
302 expect(patch).toContain("@@ -0,0 +1,2 @@\n+diff --git a/a.patch b/a.patch\n");
303 });
304
305 it("preserves complete patches and unsupported provider output", () => {
306 for (const operation of ["add", "delete"]) {
307 const diffStr = [
308 "diff --git a/a.ts b/a.ts",
309 `--- ${operation === "add" ? "/dev/null" : "a/a.ts"}`,
310 `+++ ${operation === "add" ? "b/a.ts" : "/dev/null"}`,
311 operation === "add" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@",
312 operation === "add" ? "+content" : "-content",
313 "",
314 ].join("\n");
315 expect(
316 toolCallDiff({ fileName: "a.ts", diffStr, changes: [{ operation, path: "a.ts" }] }).patch,
317 ).toBe(diffStr);
318 }
319 for (const diffStr of [
320 undefined,
321 "",
322 "provider output",
323 "--- a/a.ts\n+++ b/a.ts\n@@ -1 +1 @@\n-a\n+b\n",
324 ]) {
325 expect(
326 toolCallDiff({ fileName: "a.ts", ...(diffStr === undefined ? {} : { diffStr }) }).patch,
327 ).toBe(diffStr);
328 }
329 });
330});