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

main
ts 245 lines 8.31 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import { ANTIGRAVITY_EDIT_DIFF_OMITTED, antigravityEditPatch } from "./AntigravityEditLocation.ts";
4
5 const path = "/workspace/app.ts";
6 const file = Array.from({ length: 20 }, (_, i) =>
7 i === 9
8 ? "const target = 10;"
9 : i === 14
10 ? "const other = 15;"
11 : `const line${i + 1} = ${i + 1};`,
12 ).join("\n");
13 const replace = { TargetContent: "const target = 10;", ReplacementContent: "const target = 100;" };
14 const header = `--- ${path}\n+++ ${path}\n`;
15
16 describe("antigravityEditPatch", () => {
17 it("finds the target inside the announced search range, in either argument casing", () => {
18 const expected = `${header}@@ -10,1 +10,1 @@\n-const target = 10;\n+const target = 100;\n`;
19 const readPreEditFile = () => file;
20 for (const rawInput of [
21 { ...replace, StartLine: 5, EndLine: 20 },
22 {
23 target_content: replace.TargetContent,
24 replacement_content: replace.ReplacementContent,
25 start_line: 5,
26 end_line: 20,
27 },
28 {
29 replacement_chunks: [
30 {
31 target_content: replace.TargetContent,
32 replacement_content: replace.ReplacementContent,
33 start_line: 5,
34 end_line: 20,
35 },
36 ],
37 },
38 // Like the runtime, a present PascalCase value wins, and a 0 line means unspecified.
39 { ...replace, target_content: "const other = 15;", StartLine: 5, EndLine: 0 },
40 ]) {
41 expect(antigravityEditPatch({ path, rawInput, readPreEditFile })).toBe(expected);
42 }
43 for (const rawInput of [replace, { ReplacementChunks: [replace] }]) {
44 expect(
45 antigravityEditPatch({ path, rawInput, locations: [{ path, line: 5 }], readPreEditFile }),
46 ).toBe(expected);
47 }
48 // A hunk replaces whole lines, so a match inside a line is not a placement.
49 expect(
50 antigravityEditPatch({
51 path,
52 rawInput: { TargetContent: "10", ReplacementContent: "100" },
53 readPreEditFile,
54 }),
55 ).toMatch(/ snippet, line unknown$/m);
56 // The range start is not the target's line: without the file, or outside the range, it stays unplaced.
57 const snippet = `${header}@@ -1,1 +1,1 @@ snippet, line unknown\n-const target = 10;\n+const target = 100;\n`;
58 expect(
59 antigravityEditPatch({ path, rawInput: { ...replace, StartLine: 5, EndLine: 20 } }),
60 ).toBe(snippet);
61 expect(
62 antigravityEditPatch({
63 path,
64 rawInput: { ...replace, StartLine: 11, EndLine: 20 },
65 readPreEditFile,
66 }),
67 ).toBe(snippet);
68 });
69
70 it("finds an unannounced snippet in the pre-edit file", () => {
71 const expected = `${header}@@ -10,1 +10,1 @@\n-const target = 10;\n+const target = 100;\n`;
72 expect(antigravityEditPatch({ path, rawInput: replace, readPreEditFile: () => file })).toBe(
73 expected,
74 );
75 const crlf = file.replaceAll("\n", "\r\n");
76 expect(antigravityEditPatch({ path, rawInput: replace, readPreEditFile: () => crlf })).toBe(
77 expected,
78 );
79 // A target written with CRLF previews the same lines, without carriage returns.
80 expect(
81 antigravityEditPatch({
82 path,
83 rawInput: {
84 TargetContent: "const target = 10;\r\n",
85 ReplacementContent: "const target = 100;\r\n",
86 },
87 readPreEditFile: () => crlf,
88 }),
89 ).toBe(expected);
90 // A change to the line ending alone has no LF hunk to place.
91 const endingOnly = {
92 TargetContent: "const target = 10;\r\n",
93 ReplacementContent: "const target = 10;\n",
94 };
95 for (const readPreEditFile of [() => crlf, undefined]) {
96 expect(antigravityEditPatch({ path, rawInput: endingOnly, readPreEditFile })).toBe(
97 ANTIGRAVITY_EDIT_DIFF_OMITTED,
98 );
99 }
100 // A multi-line target written with LF still finds its lines in a CRLF file.
101 expect(
102 antigravityEditPatch({
103 path,
104 rawInput: {
105 TargetContent: "const line9 = 9;\nconst target = 10;",
106 ReplacementContent: "const line9 = 9;\nconst target = 100;",
107 },
108 readPreEditFile: () => crlf,
109 }),
110 ).toMatch(/^@@ -9,2 \+9,2 @@$/m);
111 });
112
113 it("omits the preview when a chunk adds or removes a line break at its end", () => {
114 // Whether that splits or joins lines depends on what follows the match.
115 for (const [TargetContent, ReplacementContent] of [
116 ["const line3 = 3;", "x\n"],
117 ["const line3 = 3;\n", "x"],
118 ["const line20 = 20;", "x\n"],
119 // Clearing the last line of a file with no final newline removes the line.
120 ["const line20 = 20;", ""],
121 ]) {
122 expect(
123 antigravityEditPatch({
124 path,
125 rawInput: { TargetContent, ReplacementContent },
126 readPreEditFile: () => file,
127 }),
128 ).toBe(ANTIGRAVITY_EDIT_DIFF_OMITTED);
129 }
130 });
131
132 it("labels repeated or unmatched snippets instead of placing them at line 1", () => {
133 const snippet = `${header}@@ -1,1 +1,1 @@ snippet, line unknown\n-const target = 10;\n+const target = 100;\n`;
134 for (const readPreEditFile of [
135 () => `${file}\n${file}`,
136 () => "unrelated\n",
137 () => undefined,
138 undefined,
139 ]) {
140 expect(antigravityEditPatch({ path, rawInput: replace, readPreEditFile })).toBe(snippet);
141 }
142 });
143
144 it("places each replacement chunk and shifts later chunks by earlier line changes", () => {
145 const patch = antigravityEditPatch({
146 path,
147 rawInput: {
148 ReplacementChunks: [
149 {
150 TargetContent: "const other = 15;\n",
151 ReplacementContent: "const other = 150;\n",
152 StartLine: 15,
153 EndLine: 15,
154 },
155 {
156 TargetContent: "const target = 10;\n",
157 ReplacementContent: "const target = 100;\nconst added = 1;\n",
158 StartLine: 10,
159 EndLine: 10,
160 },
161 ],
162 },
163 readPreEditFile: () => file,
164 });
165 expect(patch).toBe(
166 [
167 header.trimEnd(),
168 "@@ -10,1 +10,2 @@",
169 "-const target = 10;",
170 "+const target = 100;",
171 "+const added = 1;",
172 "@@ -15,1 +16,1 @@",
173 "-const other = 15;",
174 "+const other = 150;",
175 "",
176 ].join("\n"),
177 );
178 });
179
180 it("omits the preview when only some chunks can be placed", () => {
181 const patch = antigravityEditPatch({
182 path,
183 rawInput: {
184 ReplacementChunks: [
185 {
186 TargetContent: "const target = 10;\n",
187 ReplacementContent: "const target = 100;\n",
188 StartLine: 10,
189 EndLine: 10,
190 },
191 { TargetContent: "missing", ReplacementContent: "still missing" },
192 ],
193 },
194 readPreEditFile: () => file,
195 });
196 expect(patch).toBe(ANTIGRAVITY_EDIT_DIFF_OMITTED);
197 });
198
199 it("leaves whole-file writes and unknown inputs to the generic diff", () => {
200 expect(antigravityEditPatch({ path, rawInput: { CodeContent: "x" } })).toBeUndefined();
201 expect(antigravityEditPatch({ path, rawInput: undefined })).toBeUndefined();
202 });
203
204 it("omits the preview once an edit exceeds the shared diff budget", () => {
205 const lines = (prefix: string, count: number) =>
206 Array.from({ length: count }, (_, index) => `${prefix} ${index} ${"x".repeat(8)}`).join("\n");
207 // A 7,000-line wholesale replacement, as one chunk.
208 expect(
209 antigravityEditPatch({
210 path,
211 rawInput: {
212 TargetContent: lines("old", 7_000),
213 ReplacementContent: lines("new", 7_000),
214 StartLine: 1,
215 },
216 }),
217 ).toBe(ANTIGRAVITY_EDIT_DIFF_OMITTED);
218 // Chunks that each fit, but not together.
219 const chunk = (start: number) => ({
220 TargetContent: lines(`old-${start}`, 300),
221 ReplacementContent: lines(`new-${start}`, 300),
222 StartLine: start,
223 });
224 expect(
225 antigravityEditPatch({
226 path,
227 rawInput: { ReplacementChunks: [chunk(1), chunk(1_000)] },
228 }),
229 ).toBe(ANTIGRAVITY_EDIT_DIFF_OMITTED);
230 // Text over the size limit is not diffed at all.
231 expect(
232 antigravityEditPatch({
233 path,
234 rawInput: {
235 TargetContent: "a".repeat(200_000),
236 ReplacementContent: "b".repeat(100_000),
237 StartLine: 1,
238 },
239 }),
240 ).toBe(ANTIGRAVITY_EDIT_DIFF_OMITTED);
241 expect(antigravityEditPatch({ path, rawInput: { ReplacementChunks: [chunk(1)] } })).toMatch(
242 /^@@ -1,300 \+1,300 @@/m,
243 );
244 });
245 });