|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { applyEditsToNormalizedContent, fuzzyFindText } from "./edit-diff.js"; |
| 3 | + |
| 4 | +const path = "/workspace/notes.md"; |
| 5 | + |
| 6 | +describe("fuzzy edit source mapping", () => { |
| 7 | + it("preserves every byte outside the fuzzy-matched span", () => { |
| 8 | + const original = |
| 9 | + "The spec says “must” — not optional. \n" + |
| 10 | + "ligature: file; fraction: ½; space: a b\n" + |
| 11 | + "let target = 1;\n"; |
| 12 | + |
| 13 | + expect( |
| 14 | + applyEditsToNormalizedContent( |
| 15 | + original, |
| 16 | + [{ oldText: "let target = 1; ", newText: "let target = 2;" }], |
| 17 | + path, |
| 18 | + ), |
| 19 | + ).toEqual({ |
| 20 | + baseContent: original, |
| 21 | + newContent: original.replace("let target = 1;", "let target = 2;"), |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("mixes exact and fuzzy edits in original-content coordinates", () => { |
| 26 | + const original = "keep “this” — unchanged \nconst exact = 1;\nconst fuzzy = 2;\n"; |
| 27 | + |
| 28 | + expect( |
| 29 | + applyEditsToNormalizedContent( |
| 30 | + original, |
| 31 | + [ |
| 32 | + { oldText: "const exact = 1;", newText: "const exact = 3;" }, |
| 33 | + { oldText: "const fuzzy = 2;", newText: "const fuzzy = 4;" }, |
| 34 | + ], |
| 35 | + path, |
| 36 | + ).newContent, |
| 37 | + ).toBe("keep “this” — unchanged \nconst exact = 3;\nconst fuzzy = 4;\n"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("maps a complete NFKC expansion back to its source character", () => { |
| 41 | + expect( |
| 42 | + applyEditsToNormalizedContent( |
| 43 | + "const value = fi;\n", |
| 44 | + [{ oldText: "fi", newText: "pair" }], |
| 45 | + path, |
| 46 | + ).newContent, |
| 47 | + ).toBe("const value = pair;\n"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("maps a complete combining sequence and preserves supplementary characters", () => { |
| 51 | + const original = "const café = 1; // 🙂\n"; |
| 52 | + |
| 53 | + expect( |
| 54 | + applyEditsToNormalizedContent( |
| 55 | + original, |
| 56 | + [{ oldText: "const café = 1;", newText: "const cafe = 2;" }], |
| 57 | + path, |
| 58 | + ).newContent, |
| 59 | + ).toBe("const cafe = 2; // 🙂\n"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("rejects a match that ends inside an NFKC expansion", () => { |
| 63 | + expect(() => |
| 64 | + applyEditsToNormalizedContent("const value = fi;\n", [{ oldText: "f", newText: "x" }], path), |
| 65 | + ).toThrow(/ambiguous Unicode-normalization or trimmed-whitespace boundary/); |
| 66 | + }); |
| 67 | + |
| 68 | + it("rejects a fuzzy edit when normalization makes multiple matches ambiguous", () => { |
| 69 | + expect(() => |
| 70 | + applyEditsToNormalizedContent( |
| 71 | + "const value = 1;\nconst value = 1;\n", |
| 72 | + [{ oldText: "const value = 1; ", newText: "updated" }], |
| 73 | + path, |
| 74 | + ), |
| 75 | + ).toThrow(/Found 2 occurrences/); |
| 76 | + }); |
| 77 | + |
| 78 | + it("uses a later safe match when an earlier normalized occurrence has an unsafe boundary", () => { |
| 79 | + expect( |
| 80 | + applyEditsToNormalizedContent("fi\nf\n", [{ oldText: "f", newText: "x" }], path).newContent, |
| 81 | + ).toBe("fi\nx\n"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("rejects a match that starts inside an NFKC expansion", () => { |
| 85 | + expect(() => |
| 86 | + applyEditsToNormalizedContent("const value = fi;\n", [{ oldText: "i", newText: "x" }], path), |
| 87 | + ).toThrow(/ambiguous Unicode-normalization or trimmed-whitespace boundary/); |
| 88 | + }); |
| 89 | + |
| 90 | + it("preserves trailing whitespace adjacent to a fuzzy span", () => { |
| 91 | + expect( |
| 92 | + applyEditsToNormalizedContent( |
| 93 | + "const value = 1; ", |
| 94 | + [{ oldText: "const value = 1;", newText: "const value = 2;" }], |
| 95 | + path, |
| 96 | + ).newContent, |
| 97 | + ).toBe("const value = 2; "); |
| 98 | + }); |
| 99 | + |
| 100 | + it("preserves trailing whitespace before a fuzzy span that starts with a newline", () => { |
| 101 | + expect( |
| 102 | + applyEditsToNormalizedContent( |
| 103 | + "header \nfoo “x”;\n", |
| 104 | + [{ oldText: '\nfoo "x";', newText: '\nfoo "y";' }], |
| 105 | + path, |
| 106 | + ).newContent, |
| 107 | + ).toBe('header \nfoo "y";\n'); |
| 108 | + }); |
| 109 | + |
| 110 | + it("does not treat an exact match as a fuzzy duplicate", () => { |
| 111 | + expect( |
| 112 | + applyEditsToNormalizedContent( |
| 113 | + "foo(); \nfoo();\n", |
| 114 | + [{ oldText: "foo();\n", newText: "bar();\n" }], |
| 115 | + path, |
| 116 | + ).newContent, |
| 117 | + ).toBe("foo(); \nbar();\n"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("preserves trailing whitespace after a multiline fuzzy span", () => { |
| 121 | + expect( |
| 122 | + applyEditsToNormalizedContent( |
| 123 | + "first line\nsecond line \nafter \n", |
| 124 | + [{ oldText: "first line\nsecond line", newText: "combined" }], |
| 125 | + path, |
| 126 | + ).newContent, |
| 127 | + ).toBe("combined \nafter \n"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("replaces a fuzzy match that ends at EOF after an earlier trimmed line", () => { |
| 131 | + expect( |
| 132 | + applyEditsToNormalizedContent( |
| 133 | + "keep trailing spaces \ntarget value", |
| 134 | + [{ oldText: "target value", newText: "updated" }], |
| 135 | + path, |
| 136 | + ).newContent, |
| 137 | + ).toBe("keep trailing spaces \nupdated"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("rejects a fuzzy edit when grapheme-local NFKC differs from whole-string NFKC", () => { |
| 141 | + expect(() => |
| 142 | + applyEditsToNormalizedContent( |
| 143 | + "ㄱᅡ value\n", |
| 144 | + [{ oldText: "가 value", newText: "updated" }], |
| 145 | + path, |
| 146 | + ), |
| 147 | + ).toThrow(/ambiguous Unicode-normalization or trimmed-whitespace boundary/); |
| 148 | + }); |
| 149 | + |
| 150 | + it("replaces an entire grapheme even when whole-string NFKC composes it", () => { |
| 151 | + expect( |
| 152 | + applyEditsToNormalizedContent("Ångstrom\n", [{ oldText: "Å", newText: "A" }], path) |
| 153 | + .newContent, |
| 154 | + ).toBe("Angstrom\n"); |
| 155 | + }); |
| 156 | + |
| 157 | + it("maps fuzzy matches after supplementary characters", () => { |
| 158 | + const content = "🙂 before; const value = 1; after\n"; |
| 159 | + const match = fuzzyFindText(content, "const value = 1;"); |
| 160 | + |
| 161 | + expect(match).toMatchObject({ |
| 162 | + found: true, |
| 163 | + index: content.indexOf("const value"), |
| 164 | + matchLength: "const value = 1;".length, |
| 165 | + usedFuzzyMatch: true, |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments