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