|
| 1 | +/** |
| 2 | + * Regression tests for the fuzzy-edit normalization bug (#89994). |
| 3 | + * |
| 4 | + * When any edit falls back to fuzzy matching, normalizeForFuzzyMatch must be |
| 5 | + * used only to locate the match position — the base content must remain the |
| 6 | + * original so that unrelated lines are never silently mutated. |
| 7 | + */ |
| 8 | +import { describe, expect, it } from "vitest"; |
| 9 | +import { applyEditsToNormalizedContent, generateDiffString } from "./edit-diff.js"; |
| 10 | + |
| 11 | +describe("applyEditsToNormalizedContent fuzzy matching", () => { |
| 12 | + // ── Core regression: issue #89994 repro ────────────────────────── |
| 13 | + it("preserves em dash and trailing whitespace on unrelated lines during fuzzy edit", () => { |
| 14 | + const original = |
| 15 | + 'const label = "a — b";\n' + // em dash in unrelated line |
| 16 | + "let x = 1; \n" + // trailing spaces in unrelated line |
| 17 | + "\n" + |
| 18 | + "function bar() {\n" + |
| 19 | + " return 2;\n" + // only line targeted by the edit |
| 20 | + "}\n"; |
| 21 | + |
| 22 | + // oldText contains non-breaking spaces (U+00A0) where the file has |
| 23 | + // regular spaces, so exact match fails and fuzzy match takes over. |
| 24 | + const edits = [{ oldText: " return 2;", newText: " return 3;" }]; |
| 25 | + |
| 26 | + const { baseContent, newContent } = applyEditsToNormalizedContent(original, edits, "/x.ts"); |
| 27 | + |
| 28 | + // baseContent must be the original, NOT fuzzy-normalized. |
| 29 | + expect(baseContent).toBe(original); |
| 30 | + |
| 31 | + // Exact output: only the targeted line changes. |
| 32 | + const expected = |
| 33 | + 'const label = "a — b";\n' + |
| 34 | + "let x = 1; \n" + |
| 35 | + "\n" + |
| 36 | + "function bar() {\n" + |
| 37 | + " return 3;\n" + |
| 38 | + "}\n"; |
| 39 | + expect(newContent).toBe(expected); |
| 40 | + |
| 41 | + // Additional spot checks. |
| 42 | + expect(newContent).toContain("—"); // em dash preserved |
| 43 | + expect(newContent).toContain("let x = 1; "); // trailing whitespace preserved |
| 44 | + expect(newContent).toContain(" return 3;"); // edit applied |
| 45 | + expect(newContent).not.toContain("return 2;"); // old value gone |
| 46 | + }); |
| 47 | + |
| 48 | + it("preserves smart quotes on unrelated lines during fuzzy edit", () => { |
| 49 | + const original = |
| 50 | + 'const msg = "“help”";\n' + // smart double quotes |
| 51 | + "\n" + |
| 52 | + "function foo() {\n" + |
| 53 | + " return 1;\n" + |
| 54 | + "}\n"; |
| 55 | + |
| 56 | + const edits = [{ oldText: " return 1;", newText: " return 2;" }]; |
| 57 | + |
| 58 | + const { newContent } = applyEditsToNormalizedContent(original, edits, "/x.ts"); |
| 59 | + |
| 60 | + // Exact output: smart quotes and unrelated lines preserved. |
| 61 | + const expected = |
| 62 | + 'const msg = "“help”";\n' + "\n" + "function foo() {\n" + " return 2;\n" + "}\n"; |
| 63 | + expect(newContent).toBe(expected); |
| 64 | + expect(newContent).toContain("“help”"); |
| 65 | + expect(newContent).toContain(" return 2;"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("preserves NFKC-relevant Unicode on unrelated lines during fuzzy edit", () => { |
| 69 | + // The string "ffi" (U+FB03, LATIN SMALL LIGATURE FFI) is NFKC-normalized |
| 70 | + // to "ffi", so it would be silently corrupted by the old code. |
| 71 | + const original = |
| 72 | + 'const ligature = "ffi";\n' + // ffi |
| 73 | + "\n" + |
| 74 | + "function foo() {\n" + |
| 75 | + " return 1;\n" + |
| 76 | + "}\n"; |
| 77 | + |
| 78 | + const edits = [{ oldText: " return 1;", newText: " return 2;" }]; |
| 79 | + |
| 80 | + const { newContent } = applyEditsToNormalizedContent(original, edits, "/x.ts"); |
| 81 | + |
| 82 | + // Exact output: ligature preserved, only the targeted line changed. |
| 83 | + const expected = |
| 84 | + 'const ligature = "ffi";\n' + "\n" + "function foo() {\n" + " return 2;\n" + "}\n"; |
| 85 | + expect(newContent).toBe(expected); |
| 86 | + expect(newContent).toContain("ffi"); |
| 87 | + expect(newContent).toContain(" return 2;"); |
| 88 | + }); |
| 89 | + |
| 90 | + // ── Multiple edits with mixed match types ──────────────────────── |
| 91 | + it("preserves exact-match lines when another edit in the same batch uses fuzzy", () => { |
| 92 | + const original = |
| 93 | + "const a = 1;\n" + |
| 94 | + 'const label = "a — b";\n' + // em dash — should survive |
| 95 | + " const b = 2;\n" + // has leading spaces (matches NBSP in oldText) |
| 96 | + "const c = 3;\n"; |
| 97 | + |
| 98 | + // First edit matches exactly; second needs fuzzy (NBSP in oldText |
| 99 | + // normalizes to space, which matches the spaces in the original). |
| 100 | + const edits = [ |
| 101 | + { oldText: "const a = 1;", newText: "const a = 10;" }, |
| 102 | + { |
| 103 | + oldText: " const b = 2;", |
| 104 | + newText: " const b = 20;", |
| 105 | + }, |
| 106 | + ]; |
| 107 | + |
| 108 | + const { newContent } = applyEditsToNormalizedContent(original, edits, "/x.ts"); |
| 109 | + |
| 110 | + expect(newContent).toContain("const a = 10;"); |
| 111 | + expect(newContent).toContain(" const b = 20;"); |
| 112 | + expect(newContent).toContain("—"); // em dash preserved |
| 113 | + expect(newContent).toContain("const c = 3;"); |
| 114 | + }); |
| 115 | + |
| 116 | + // ── Diff honesty ───────────────────────────────────────────────── |
| 117 | + it("produces a diff that reflects only the intended change, not normalization", () => { |
| 118 | + const original = |
| 119 | + 'const label = "a — b";\n' + |
| 120 | + "let x = 1; \n" + |
| 121 | + "\n" + |
| 122 | + "function bar() {\n" + |
| 123 | + " return 2;\n" + |
| 124 | + "}\n"; |
| 125 | + |
| 126 | + const edits = [{ oldText: " return 2;", newText: " return 3;" }]; |
| 127 | + |
| 128 | + const { baseContent, newContent } = applyEditsToNormalizedContent(original, edits, "/x.ts"); |
| 129 | + const { diff } = generateDiffString(baseContent, newContent); |
| 130 | + |
| 131 | + // The diff shows the intended change — return 2 → return 3. |
| 132 | + expect(diff).toContain("return 2;"); |
| 133 | + expect(diff).toContain("return 3;"); |
| 134 | + |
| 135 | + // Unrelated lines must NOT appear as added (+) or removed (-) lines. |
| 136 | + // They may appear as context lines (prefix " N ") but NOT as changes. |
| 137 | + const changedLines = diff.split("\n").filter((l) => l.startsWith("+") || l.startsWith("-")); |
| 138 | + const changedStr = changedLines.join("\n"); |
| 139 | + expect(changedStr).not.toContain("const label"); |
| 140 | + expect(changedStr).not.toContain("let x = 1"); |
| 141 | + }); |
| 142 | + |
| 143 | + // ── Exact match path: regression guard ─────────────────────────── |
| 144 | + it("exact match preserves all content unchanged except the edited region", () => { |
| 145 | + const original = |
| 146 | + 'const label = "a — b";\n' + |
| 147 | + "let x = 1; \n" + |
| 148 | + "function bar() {\n" + |
| 149 | + " return 2;\n" + |
| 150 | + "}\n"; |
| 151 | + |
| 152 | + const edits = [{ oldText: " return 2;", newText: " return 3;" }]; |
| 153 | + |
| 154 | + const { baseContent, newContent } = applyEditsToNormalizedContent(original, edits, "/x.ts"); |
| 155 | + |
| 156 | + // Exact match: baseContent should equal original. |
| 157 | + expect(baseContent).toBe(original); |
| 158 | + expect(newContent).toContain("—"); |
| 159 | + expect(newContent).toContain("let x = 1; "); |
| 160 | + expect(newContent).toContain(" return 3;"); |
| 161 | + }); |
| 162 | + |
| 163 | + // ── Error behaviour ────────────────────────────────────────────── |
| 164 | + it("still throws when oldText is not found even with fuzzy matching", () => { |
| 165 | + const original = "line one\nline two\nline three\n"; |
| 166 | + |
| 167 | + const edits = [{ oldText: "this text does not exist anywhere", newText: "nope" }]; |
| 168 | + |
| 169 | + expect(() => applyEditsToNormalizedContent(original, edits, "/x.ts")).toThrow(/Could not find/); |
| 170 | + }); |
| 171 | +}); |
0 commit comments