| 1 | import fs from "node:fs"; |
| 2 | import path from "node:path"; |
| 3 | |
| 4 | // @ts-ignore no typings available |
| 5 | import { diff as myersDiff } from "myers-diff"; |
| 6 | import { DiffLine } from "../.."; |
| 7 | import { myersDiff as continueMyersDiff } from "../../diff/myers"; |
| 8 | import { dedent } from "../../util"; |
| 9 | import { deterministicApplyLazyEdit } from "./deterministic"; |
| 10 | |
| 11 | const UNIFIED_DIFF_SYMBOLS = { |
| 12 | same: "", |
| 13 | new: "+", |
| 14 | old: "-", |
| 15 | }; |
| 16 | |
| 17 | async function collectDiffs( |
| 18 | oldFile: string, |
| 19 | newFile: string, |
| 20 | filename: string, |
| 21 | ): Promise<{ ourDiffs: DiffLine[]; myersDiffs: any }> { |
| 22 | const ourDiffs: DiffLine[] = []; |
| 23 | |
| 24 | for (const diffLine of (await deterministicApplyLazyEdit({ |
| 25 | oldFile, |
| 26 | newLazyFile: newFile, |
| 27 | filename, |
| 28 | })) ?? []) { |
| 29 | ourDiffs.push(diffLine); |
| 30 | } |
| 31 | |
| 32 | const myersDiffs = myersDiff(oldFile, newFile); |
| 33 | |
| 34 | return { ourDiffs, myersDiffs }; |
| 35 | } |
| 36 | |
| 37 | function displayDiff(diff: DiffLine[]) { |
| 38 | return diff |
| 39 | .map(({ type, line }) => |
| 40 | type === "same" ? line : `${UNIFIED_DIFF_SYMBOLS[type]} ${line}`, |
| 41 | ) |
| 42 | .join("\n"); |
| 43 | } |
| 44 | |
| 45 | function normalizeDisplayedDiff(d: string): string { |
| 46 | return d |
| 47 | .split("\n") |
| 48 | .map((line) => (line.trim() === "" ? "" : line)) |
| 49 | .join("\n"); |
| 50 | } |
| 51 | |
| 52 | async function expectDiff(file: string) { |
| 53 | const testFilePath = path.join( |
| 54 | __dirname, |
| 55 | "edit", |
| 56 | "lazy", |
| 57 | "test-examples", |
| 58 | file + ".diff", |
| 59 | ); |
| 60 | const testFileContents = fs.readFileSync(testFilePath, "utf-8"); |
| 61 | const [oldFile, newFile, expectedDiff] = testFileContents |
| 62 | .split("\n---\n") |
| 63 | .map((s) => s.replace(/^\n+/, "").trimEnd()); |
| 64 | const { ourDiffs: streamDiffs } = await collectDiffs(oldFile, newFile, file); |
| 65 | const displayedDiff = displayDiff(streamDiffs); |
| 66 | |
| 67 | if (!expectedDiff || expectedDiff.trim() === "") { |
| 68 | console.log( |
| 69 | "Expected diff was empty. Writing computed diff to the test file", |
| 70 | ); |
| 71 | fs.writeFileSync( |
| 72 | testFilePath, |
| 73 | `${oldFile}\n\n---\n\n${newFile}\n\n---\n\n${displayDiff( |
| 74 | continueMyersDiff(oldFile, newFile), |
| 75 | )}`, |
| 76 | ); |
| 77 | |
| 78 | throw new Error("Expected diff is empty"); |
| 79 | } |
| 80 | |
| 81 | expect(normalizeDisplayedDiff(displayedDiff)).toEqual( |
| 82 | normalizeDisplayedDiff(expectedDiff), |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | describe("deterministicApplyLazyEdit(", () => { |
| 87 | test("no changes", async () => { |
| 88 | const file = dedent` |
| 89 | function test() { |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | function test2(a) { |
| 94 | return a * 2; |
| 95 | } |
| 96 | `; |
| 97 | |
| 98 | const { ourDiffs: streamDiffs, myersDiffs } = await collectDiffs( |
| 99 | file, |
| 100 | file, |
| 101 | "test.js", |
| 102 | ); |
| 103 | |
| 104 | expect(streamDiffs).toEqual( |
| 105 | file.split("\n").map((line) => ({ |
| 106 | line, |
| 107 | type: "same", |
| 108 | })), |
| 109 | ); |
| 110 | |
| 111 | expect(myersDiffs).toEqual([]); |
| 112 | }); |
| 113 | |
| 114 | test("fastapi", async () => { |
| 115 | await expectDiff("fastapi.py"); |
| 116 | }); |
| 117 | |
| 118 | test("calculator exp", async () => { |
| 119 | await expectDiff("calculator-exp.js"); |
| 120 | }); |
| 121 | |
| 122 | test("calculator exp2", async () => { |
| 123 | await expectDiff("calculator-exp2.js"); |
| 124 | }); |
| 125 | |
| 126 | test("calculator comments", async () => { |
| 127 | await expectDiff("calculator-comments.js"); |
| 128 | }); |
| 129 | |
| 130 | test.skip("calculator docstrings", async () => { |
| 131 | await expectDiff("calculator-docstrings.js"); |
| 132 | }); |
| 133 | |
| 134 | test.skip("calculator stateless", async () => { |
| 135 | await expectDiff("calculator-stateless.js"); |
| 136 | }); |
| 137 | |
| 138 | test("top level same blocks", async () => { |
| 139 | await expectDiff("top-level-same.js"); |
| 140 | }); |
| 141 | |
| 142 | test.skip("gui add toggle", async () => { |
| 143 | await expectDiff("gui.js"); |
| 144 | }); |
| 145 | |
| 146 | test("rust calculator", async () => { |
| 147 | await expectDiff("rust-calc-exp.rs"); |
| 148 | }); |
| 149 | |
| 150 | test("no lazy blocks", async () => { |
| 151 | await expectDiff("no-lazy.js"); |
| 152 | }); |
| 153 | |
| 154 | test.skip("no lazy blocks in single top level class", async () => { |
| 155 | await expectDiff("no-lazy-single-class.js"); |
| 156 | }); |
| 157 | |
| 158 | test("should acknowledge jsx_expression lazy comments", async () => { |
| 159 | await expectDiff("migration-page.tsx"); |
| 160 | }); |
| 161 | |
| 162 | test.skip("should handle case where surrounding class is neglected, with lazy block surrounding", async () => { |
| 163 | await expectDiff("calculator-class-neglected.js"); |
| 164 | }); |
| 165 | |
| 166 | test("should handle case where surrounding class is neglected, without lazy block surrounding", async () => { |
| 167 | await expectDiff("calculator-only-method.js"); |
| 168 | }); |
| 169 | }); |