Elffuss BraidHub

kikocis/continue public

32d59361d5be · 10.2 KiBRawPermalink
1import fs from "node:fs";
2import path from "node:path";
3
4// @ts-ignore no typings available
5import { changed, diff as myersDiff } from "myers-diff";
6import { streamDiff } from "../diff/streamDiff.js";
7import { DiffLine, DiffLineType } from "../index.js";
8import { generateLines } from "./util.js";
9
10// "modification" is an extra type used to represent an "old" + "new" diff line
11type MyersDiffTypes = Extract<DiffLineType, "new" | "old"> | "modification";
12
13const UNIFIED_DIFF_SYMBOLS = {
14 same: "",
15 new: "+",
16 old: "-",
17};
18
19async function collectDiffs(
20 oldLines: string[],
21 newLines: string[],
22): Promise<{ streamDiffs: DiffLine[]; myersDiffs: any }> {
23 const streamDiffs: DiffLine[] = [];
24
25 for await (const diffLine of streamDiff(oldLines, generateLines(newLines))) {
26 streamDiffs.push(diffLine);
27 }
28
29 const myersDiffs = myersDiff(oldLines.join("\n"), newLines.join("\n"));
30
31 return { streamDiffs, myersDiffs };
32}
33
34function getMyersDiffType(diff: any): MyersDiffTypes | undefined {
35 if (changed(diff.rhs) && !changed(diff.lhs)) {
36 return "new";
37 }
38
39 if (!changed(diff.rhs) && changed(diff.lhs)) {
40 return "old";
41 }
42
43 if (changed(diff.rhs) && changed(diff.lhs)) {
44 return "modification";
45 }
46
47 return undefined;
48}
49
50function displayDiff(diff: DiffLine[]) {
51 return diff
52 .map(({ type, line }) => `${UNIFIED_DIFF_SYMBOLS[type]} ${line}`)
53 .join("\n");
54}
55
56async function expectDiff(file: string) {
57 const testFilePath = path.join(
58 __dirname,
59 "diff",
60 "test-examples",
61 file + ".diff",
62 );
63 const testFileContents = fs.readFileSync(testFilePath, "utf-8");
64 const [oldText, newText, expectedDiff] = testFileContents
65 .split("\n---\n")
66 .map((s) => s.replace(/^\n+/, "").trimEnd());
67 const oldLines = oldText.split("\n");
68 const newLines = newText.split("\n");
69 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
70 const displayedDiff = displayDiff(streamDiffs);
71
72 if (!expectedDiff || expectedDiff.trim() === "") {
73 console.log(
74 "Expected diff was empty. Writing computed diff to the test file",
75 );
76 fs.writeFileSync(
77 testFilePath,
78 `${oldText}\n\n---\n\n${newText}\n\n---\n\n${displayedDiff}`,
79 );
80
81 throw new Error("Expected diff is empty");
82 }
83
84 expect(displayedDiff).toEqual(expectedDiff);
85}
86
87// We use a longer `)` string here to not get
88// caught by the fuzzy matcher
89describe("streamDiff(", () => {
90 test("no changes", async () => {
91 const oldLines = ["first item", "second arg", "third param"];
92 const newLines = ["first item", "second arg", "third param"];
93
94 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
95
96 expect(streamDiffs).toEqual([
97 { type: "same", line: "first item" },
98 { type: "same", line: "second arg" },
99 { type: "same", line: "third param" },
100 ]);
101
102 expect(myersDiffs).toEqual([]);
103 });
104
105 test("add new line", async () => {
106 const oldLines = ["first item", "second arg"];
107 const newLines = ["first item", "second arg", "third param"];
108
109 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
110
111 expect(streamDiffs).toEqual([
112 { type: "same", line: "first item" },
113 { type: "same", line: "second arg" },
114 { type: "new", line: "third param" },
115 ]);
116
117 expect(myersDiffs.length).toEqual(1);
118 expect(getMyersDiffType(myersDiffs[0])).toBe("new");
119 });
120
121 test("remove line", async () => {
122 const oldLines = ["first item", "second arg", "third param"];
123 const newLines = ["first item", "third param"];
124
125 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
126
127 expect(streamDiffs).toEqual([
128 { type: "same", line: "first item" },
129 { type: "old", line: "second arg" },
130 { type: "same", line: "third param" },
131 ]);
132
133 expect(myersDiffs.length).toEqual(1);
134 expect(getMyersDiffType(myersDiffs[0])).toBe("old");
135 });
136
137 test("modify line", async () => {
138 const oldLines = ["first item", "second arg", "third param"];
139 const newLines = ["first item", "modified second arg", "third param"];
140
141 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
142
143 expect(streamDiffs).toEqual([
144 { type: "same", line: "first item" },
145 { type: "old", line: "second arg" },
146 { type: "new", line: "modified second arg" },
147 { type: "same", line: "third param" },
148 ]);
149
150 expect(myersDiffs.length).toEqual(1);
151 expect(getMyersDiffType(myersDiffs[0])).toBe("modification");
152 });
153
154 test("add multiple lines", async () => {
155 const oldLines = ["first item", "fourth val"];
156 const newLines = ["first item", "second arg", "third param", "fourth val"];
157
158 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
159
160 expect(streamDiffs).toEqual([
161 { type: "same", line: "first item" },
162 { type: "new", line: "second arg" },
163 { type: "new", line: "third param" },
164 { type: "same", line: "fourth val" },
165 ]);
166
167 // Multi-line addition
168 expect(myersDiffs[0].rhs.add).toEqual(2);
169 expect(getMyersDiffType(myersDiffs[0])).toBe("new");
170 });
171
172 test("remove multiple lines", async () => {
173 const oldLines = ["first item", "second arg", "third param", "fourth val"];
174 const newLines = ["first item", "fourth val"];
175
176 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
177
178 expect(streamDiffs).toEqual([
179 { type: "same", line: "first item" },
180 { type: "old", line: "second arg" },
181 { type: "old", line: "third param" },
182 { type: "same", line: "fourth val" },
183 ]);
184
185 // Multi-line deletion
186 expect(myersDiffs[0].lhs.del).toEqual(2);
187 expect(getMyersDiffType(myersDiffs[0])).toBe("old");
188 });
189
190 test("empty old lines", async () => {
191 const oldLines: string[] = [];
192 const newLines = ["first item", "second arg"];
193
194 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
195
196 expect(streamDiffs).toEqual([
197 { type: "new", line: "first item" },
198 { type: "new", line: "second arg" },
199 ]);
200
201 // Multi-line addition
202 expect(myersDiffs[0].rhs.add).toEqual(2);
203 expect(getMyersDiffType(myersDiffs[0])).toBe("new");
204 });
205
206 test("empty new lines", async () => {
207 const oldLines = ["first item", "second arg"];
208 const newLines: string[] = [];
209
210 const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines);
211
212 expect(streamDiffs).toEqual([
213 { type: "old", line: "first item" },
214 { type: "old", line: "second arg" },
215 ]);
216
217 // Multi-line deletion
218 expect(myersDiffs[0].lhs.del).toEqual(2);
219 expect(getMyersDiffType(myersDiffs[0])).toBe("old");
220 });
221
222 test("tabs vs. spaces differences are ignored", async () => {
223 await expectDiff("fastapi-tabs-vs-spaces.py");
224 });
225
226 test("trailing whitespaces should match ", async () => {
227 const oldLines = ["first item ", "second arg ", "third param "];
228
229 const newLines = ["first item", "second arg", "third param "];
230
231 const { streamDiffs } = await collectDiffs(oldLines, newLines);
232
233 expect(streamDiffs).toEqual([
234 { type: "same", line: "first item " },
235 { type: "same", line: "second arg " },
236 { type: "same", line: "third param " },
237 ]);
238 });
239
240 //indentation and whitespace handling
241 test.each([false, true])(
242 "ignores indentation changes for sufficiently long lines (trailingWhitespace: %s)",
243 async (trailingWhitespace) => {
244 let oldLines = [
245 " short",
246 " middle",
247 " a long enough line",
248 " short2",
249 "indented line",
250 "final line",
251 ];
252
253 let newLines = [
254 "short",
255 "middle",
256 "a long enough line",
257 "short2",
258 " indented line",
259 "final line",
260 ];
261
262 if (trailingWhitespace) {
263 oldLines = oldLines.map((line) => line + " ");
264 }
265
266 const { streamDiffs } = await collectDiffs(oldLines, newLines);
267 const expected = trailingWhitespace
268 ? [
269 { type: "old", line: " short " },
270 { type: "new", line: "short" },
271 { type: "old", line: " middle " },
272 { type: "new", line: "middle" },
273 { type: "same", line: " a long enough line " },
274 { type: "same", line: " short2 " },
275 { type: "same", line: "indented line " },
276 { type: "same", line: "final line " },
277 ]
278 : [
279 { type: "old", line: " short" },
280 { type: "new", line: "short" },
281 { type: "old", line: " middle" },
282 { type: "new", line: "middle" },
283 { type: "same", line: " a long enough line" },
284 { type: "same", line: " short2" },
285 { type: "same", line: "indented line" },
286 { type: "same", line: "final line" },
287 ];
288
289 expect(streamDiffs).toEqual(expected);
290 },
291 );
292
293 test("preserves original lines for minor reindentation in simple block", async () => {
294 const oldLines = ["if (checkValueOf(x)) {", " doSomethingWith(x);", "}"];
295 const newLines = ["if (checkValueOf(x)) {", " doSomethingWith(x);", "}"];
296
297 const { streamDiffs } = await collectDiffs(oldLines, newLines);
298
299 expect(streamDiffs).toEqual([
300 { type: "same", line: "if (checkValueOf(x)) {" },
301 { type: "same", line: " doSomethingWith(x);" },
302 { type: "same", line: "}" },
303 ]);
304 });
305
306 test("uses new lines for nested reindentation changes", async () => {
307 const oldLines = ["if (checkValueOf(x)) {", " doSomethingWith(x);", "}"];
308 const newLines = [
309 "if (checkValueOf(x)) {",
310 " if (reallyCheckValueOf(x)) {",
311 " doSomethingElseWith(x);",
312 " }",
313 "}",
314 ];
315
316 const { streamDiffs } = await collectDiffs(oldLines, newLines);
317
318 expect(streamDiffs).toEqual([
319 { type: "same", line: "if (checkValueOf(x)) {" },
320 { type: "new", line: " if (reallyCheckValueOf(x)) {" },
321 { type: "old", line: " doSomethingWith(x);" },
322 { type: "new", line: " doSomethingElseWith(x);" },
323 { type: "old", line: "}" },
324 { type: "new", line: " }" },
325 { type: "new", line: "}" },
326 ]);
327 });
328
329 test("FastAPI example", async () => {
330 await expectDiff("fastapi.py");
331 });
332
333 test("FastAPI comments", async () => {
334 await expectDiff("add-comments.py");
335 });
336
337 test("Mock LLM example", async () => {
338 await expectDiff("mock-llm.ts");
339 });
340});