The best way to control Claude Code, Codex and more from any computer or phone

main
ts 194 lines 7.88 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2 import { getRenderablePatch, resolveFileDiffPath } from "./diffRendering";
3 import { toolCallDiff } from "./toolCallDiff";
4
5 describe("toolCallDiff", () => {
6 it("renders recorded hunks without reading a repository, including renamed paths", () => {
7 for (const changes of [
8 undefined,
9 [{ operation: "update", oldPath: "old.ts", path: "new.ts" }],
10 ]) {
11 const { patch } = toolCallDiff({
12 fileName: "old.ts",
13 diffStr: "@@ -1 +1 @@\n-before\n+after\n",
14 ...(changes ? { changes } : {}),
15 });
16 const rendered = getRenderablePatch(patch);
17 expect(rendered?.kind).toBe("files");
18 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
19 expect(rendered.files).toHaveLength(1);
20 expect(resolveFileDiffPath(rendered.files[0]!)).toBe(changes ? "new.ts" : "old.ts");
21 expect(rendered.files[0]!.additionLines.join("")).toContain("after");
22 expect(rendered.files[0]!.deletionLines.join("")).toContain("before");
23 }
24 });
25
26 it("renders added and deleted file contents as a file diff", () => {
27 for (const operation of ["add", "delete"]) {
28 for (const [content, ending] of [
29 ["first\nsecond", ""],
30 ["@@ -0,0 +1 @@\n+content", "\n"],
31 ["@@ -1 +0,0 @@\n-content", "\n"],
32 ["first\nsecond", "\n"],
33 ["--- notes\nsecond", "\n"],
34 ["diff --git notes\nsecond", "\n"],
35 ["--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after", "\n"],
36 [
37 "diff --git a/embedded.ts b/embedded.ts\n--- a/embedded.ts\n+++ b/embedded.ts\n@@ -1 +1 @@\n-before\n+after",
38 "\n",
39 ],
40 ] as const) {
41 const recordedContent = `${content}${ending}`;
42 const rendered = getRenderablePatch(
43 toolCallDiff({
44 fileName: "file with spaces.ts",
45 diffStr: recordedContent,
46 changes: [{ operation, path: "file with spaces.ts" }],
47 }).patch,
48 );
49 expect(rendered?.kind).toBe("files");
50 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
51 expect(resolveFileDiffPath(rendered.files[0]!)).toBe("file with spaces.ts");
52 const file = rendered.files[0]!;
53 const lines = content.split("\n").length;
54 expect(file.additionLines).toHaveLength(operation === "add" ? lines : 0);
55 expect(file.deletionLines).toHaveLength(operation === "delete" ? lines : 0);
56 expect((operation === "add" ? file.additionLines : file.deletionLines).join("")).toBe(
57 recordedContent,
58 );
59 }
60 }
61 });
62
63 it("keeps content-only writes out of the patch and preserves empty recorded content", () => {
64 for (const operation of ["add", "delete"]) {
65 expect(
66 toolCallDiff({
67 fileName: "empty.ts",
68 diffStr: "",
69 changes: [{ operation, path: "empty.ts" }],
70 }).content,
71 ).toEqual([
72 {
73 label: operation === "add" ? "Recorded content after" : "Recorded content before",
74 text: "",
75 },
76 ]);
77 }
78 expect(toolCallDiff({ fileName: "a.ts", oldStr: "before", newStr: "" })).toEqual({
79 patch: undefined,
80 content: [
81 { label: "Recorded content before", text: "before" },
82 { label: "Recorded content after", text: "" },
83 ],
84 truncated: false,
85 });
86 expect(toolCallDiff({ fileName: "a.ts", newStr: "after" }).content).toEqual([
87 { label: "Recorded content after", text: "after" },
88 ]);
89 });
90
91 it("removes transport truncation markers before converting file contents", () => {
92 const suffix = "\n… output truncated for transport";
93 const diff = toolCallDiff({
94 fileName: "a.ts",
95 diffStr: `first\nsecond${suffix}`,
96 changes: [{ operation: "add", path: "a.ts" }],
97 });
98 expect(diff.truncated).toBe(true);
99 expect(diff.patch).not.toContain("output truncated");
100 expect(getRenderablePatch(diff.patch)?.kind).toBe("files");
101 expect(toolCallDiff({ fileName: "a.ts", newStr: `content${suffix}` })).toMatchObject({
102 content: [{ label: "Recorded content after", text: "content" }],
103 truncated: true,
104 });
105 });
106
107 it("names files under the workspace relative to it in headers only", () => {
108 const root = "/Users/me/project";
109 const paths = (patch: string | undefined) => {
110 const rendered = getRenderablePatch(patch);
111 if (rendered?.kind !== "files") throw new Error("Expected a file diff");
112 return rendered.files.map((file) => resolveFileDiffPath(file));
113 };
114 // Claude's Edit: absolute headers; hunk lines that look like headers keep the root.
115 const claude = `--- ${root}/docs/plan.md\n+++ ${root}/docs/plan.md\n@@ -2 +2 @@\n-- ${root}/x\n++ ${root}/y\n`;
116 const { patch } = toolCallDiff({ fileName: `${root}/docs/plan.md`, diffStr: claude }, root);
117 expect(paths(patch)).toEqual(["docs/plan.md"]);
118 expect(patch).toContain(`\n-- ${root}/x\n++ ${root}/y\n`);
119 // Synthesized headers, a trailing separator on the root, and a path outside it.
120 const hunk = "@@ -1 +1 @@\n-x\n+y\n";
121 expect(
122 paths(toolCallDiff({ fileName: `${root}/src/a.ts`, diffStr: hunk }, `${root}/`).patch),
123 ).toEqual(["src/a.ts"]);
124 expect(paths(toolCallDiff({ fileName: "/elsewhere/b.ts", diffStr: hunk }, root).patch)).toEqual(
125 ["/elsewhere/b.ts"],
126 );
127 // A relative path, and on POSIX a backslash that is part of a name, keep theirs.
128 for (const [fileName, workspace] of [
129 ["repo/src/a.ts", "/repo"],
130 ["/repo/back/slash/a.ts", "/repo/back\\slash"],
131 ] as const) {
132 const diffStr = `--- ${fileName}\n+++ ${fileName}\n${hunk}`;
133 expect(toolCallDiff({ fileName, diffStr }, workspace).patch).toBe(diffStr);
134 }
135 const backslash = toolCallDiff(
136 { fileName: "/repo/back\\slash/a.ts", diffStr: hunk },
137 "/repo/back\\slash",
138 ).patch;
139 expect(paths(backslash)).toEqual(["a.ts"]);
140 // Each file of a multi-file patch; a path that merely contains the root keeps it.
141 const twoFiles = [
142 `diff --git a/${root}/a.ts b/${root}/a.ts`,
143 `--- a/${root}/a.ts`,
144 `+++ b/${root}/a.ts`,
145 hunk.trimEnd(),
146 `diff --git a/backup${root}/b.ts b/backup${root}/b.ts`,
147 `--- a/backup${root}/b.ts`,
148 `+++ b/backup${root}/b.ts`,
149 hunk,
150 ].join("\n");
151 expect(
152 paths(toolCallDiff({ fileName: `${root}/a.ts`, diffStr: twoFiles }, root).patch),
153 ).toEqual(["a.ts", `backup${root}/b.ts`]);
154 // Only where a name starts: a quoted name with a space before the root.
155 expect(
156 paths(toolCallDiff({ fileName: `${root}/docs ${root}/a.md`, diffStr: hunk }, root).patch),
157 ).toEqual([`docs ${root}/a.md`]);
158 expect(
159 paths(toolCallDiff({ fileName: `/backup ${root}/a.md`, diffStr: hunk }, root).patch),
160 ).toEqual([`/backup ${root}/a.md`]);
161 // Windows paths, which a quoted header escapes, compared without case.
162 const windows = toolCallDiff(
163 { fileName: "C:\\repo\\src\\a.ts", diffStr: hunk },
164 "c:\\repo",
165 ).patch;
166 expect(paths(windows)).toEqual(["src\\a.ts"]);
167 });
168
169 it("preserves complete patches and unsupported provider output", () => {
170 for (const operation of ["add", "delete"]) {
171 const diffStr = [
172 "diff --git a/a.ts b/a.ts",
173 `--- ${operation === "add" ? "/dev/null" : "a/a.ts"}`,
174 `+++ ${operation === "add" ? "b/a.ts" : "/dev/null"}`,
175 operation === "add" ? "@@ -0,0 +1 @@" : "@@ -1 +0,0 @@",
176 operation === "add" ? "+content" : "-content",
177 "",
178 ].join("\n");
179 expect(
180 toolCallDiff({ fileName: "a.ts", diffStr, changes: [{ operation, path: "a.ts" }] }).patch,
181 ).toBe(diffStr);
182 }
183 for (const diffStr of [
184 undefined,
185 "",
186 "provider output",
187 "--- a/a.ts\n+++ b/a.ts\n@@ -1 +1 @@\n-a\n+b\n",
188 ]) {
189 expect(
190 toolCallDiff({ fileName: "a.ts", ...(diffStr === undefined ? {} : { diffStr }) }).patch,
191 ).toBe(diffStr);
192 }
193 });
194 });