|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { Patch } from "../../src/patch" |
| 3 | +import path from "path" |
| 4 | +import fs from "fs" |
| 5 | +import { tmpdir } from "os" |
| 6 | + |
| 7 | +describe("Patch parser edge cases", () => { |
| 8 | + test("parsePatch handles CRLF line endings in patch text", () => { |
| 9 | + const patchText = |
| 10 | + "*** Begin Patch\r\n*** Add File: test.txt\r\n+hello world\r\n*** End Patch\r\n" |
| 11 | + const result = Patch.parsePatch(patchText) |
| 12 | + expect(result.hunks).toHaveLength(1) |
| 13 | + expect(result.hunks[0].type).toBe("add") |
| 14 | + if (result.hunks[0].type === "add") { |
| 15 | + expect(result.hunks[0].contents).toBe("hello world") |
| 16 | + } |
| 17 | + }) |
| 18 | + |
| 19 | + test("parsePatch handles mixed CRLF and LF", () => { |
| 20 | + const patchText = |
| 21 | + "*** Begin Patch\r\n*** Add File: mixed.txt\n+line one\r\n+line two\n*** End Patch\n" |
| 22 | + const result = Patch.parsePatch(patchText) |
| 23 | + expect(result.hunks).toHaveLength(1) |
| 24 | + if (result.hunks[0].type === "add") { |
| 25 | + expect(result.hunks[0].contents).toContain("line one") |
| 26 | + expect(result.hunks[0].contents).toContain("line two") |
| 27 | + } |
| 28 | + }) |
| 29 | + |
| 30 | + test("parsePatch handles bare CR line endings", () => { |
| 31 | + const patchText = |
| 32 | + "*** Begin Patch\r*** Add File: cr.txt\r+content\r*** End Patch\r" |
| 33 | + const result = Patch.parsePatch(patchText) |
| 34 | + expect(result.hunks).toHaveLength(1) |
| 35 | + }) |
| 36 | + |
| 37 | + test("parsePatch returns empty hunks for empty patch", () => { |
| 38 | + const result = Patch.parsePatch("*** Begin Patch\n*** End Patch") |
| 39 | + expect(result.hunks).toHaveLength(0) |
| 40 | + }) |
| 41 | + |
| 42 | + test("parsePatch handles Unicode in file paths", () => { |
| 43 | + const patchText = [ |
| 44 | + "*** Begin Patch", |
| 45 | + "*** Add File: src/données.txt", |
| 46 | + "+contenu français", |
| 47 | + "*** End Patch", |
| 48 | + ].join("\n") |
| 49 | + const result = Patch.parsePatch(patchText) |
| 50 | + expect(result.hunks).toHaveLength(1) |
| 51 | + if (result.hunks[0].type === "add") { |
| 52 | + expect(result.hunks[0].path).toBe("src/données.txt") |
| 53 | + expect(result.hunks[0].contents).toBe("contenu français") |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + test("parsePatch handles Unicode smart quotes in content", () => { |
| 58 | + const patchText = [ |
| 59 | + "*** Begin Patch", |
| 60 | + '*** Add File: quotes.txt', |
| 61 | + '+He said \u201CHello\u201D', |
| 62 | + "*** End Patch", |
| 63 | + ].join("\n") |
| 64 | + const result = Patch.parsePatch(patchText) |
| 65 | + expect(result.hunks).toHaveLength(1) |
| 66 | + if (result.hunks[0].type === "add") { |
| 67 | + expect(result.hunks[0].contents).toContain("\u201C") |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + test("parsePatch handles delete with CRLF", () => { |
| 72 | + const patchText = "*** Begin Patch\r\n*** Delete File: old.txt\r\n*** End Patch\r\n" |
| 73 | + const result = Patch.parsePatch(patchText) |
| 74 | + expect(result.hunks).toHaveLength(1) |
| 75 | + expect(result.hunks[0].type).toBe("delete") |
| 76 | + expect(result.hunks[0].path).toBe("old.txt") |
| 77 | + }) |
| 78 | + |
| 79 | + test("parsePatch handles update with move and CRLF", () => { |
| 80 | + const patchText = [ |
| 81 | + "*** Begin Patch", |
| 82 | + "*** Update File: src/old.ts", |
| 83 | + "*** Move to: src/new.ts", |
| 84 | + "@@ context line", |
| 85 | + " unchanged", |
| 86 | + "-old line", |
| 87 | + "+new line", |
| 88 | + "*** End Patch", |
| 89 | + ].join("\r\n") |
| 90 | + const result = Patch.parsePatch(patchText) |
| 91 | + expect(result.hunks).toHaveLength(1) |
| 92 | + const hunk = result.hunks[0] |
| 93 | + expect(hunk.type).toBe("update") |
| 94 | + if (hunk.type === "update") { |
| 95 | + expect(hunk.path).toBe("src/old.ts") |
| 96 | + expect(hunk.move_path).toBe("src/new.ts") |
| 97 | + expect(hunk.chunks).toHaveLength(1) |
| 98 | + expect(hunk.chunks[0].old_lines).toContain("old line") |
| 99 | + expect(hunk.chunks[0].new_lines).toContain("new line") |
| 100 | + } |
| 101 | + }) |
| 102 | +}) |
| 103 | + |
| 104 | +describe("deriveNewContentsFromChunks edge cases", () => { |
| 105 | + const tmpDir = path.join(tmpdir(), `patch-test-${process.pid}`) |
| 106 | + |
| 107 | + function writeTemp(name: string, content: string): string { |
| 108 | + const filePath = path.join(tmpDir, name) |
| 109 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }) |
| 110 | + fs.writeFileSync(filePath, content) |
| 111 | + return filePath |
| 112 | + } |
| 113 | + |
| 114 | + test("handles file with CRLF line endings", () => { |
| 115 | + const filePath = writeTemp("crlf.txt", "line one\r\nline two\r\nline three\r\n") |
| 116 | + const result = Patch.deriveNewContentsFromChunks(filePath, [ |
| 117 | + { |
| 118 | + old_lines: ["line two"], |
| 119 | + new_lines: ["line TWO"], |
| 120 | + }, |
| 121 | + ]) |
| 122 | + expect(result.content).toContain("line TWO") |
| 123 | + expect(result.content).not.toContain("\r") |
| 124 | + }) |
| 125 | + |
| 126 | + test("handles file with UTF-8 BOM", () => { |
| 127 | + const filePath = writeTemp("bom.txt", "\uFEFFline one\nline two\n") |
| 128 | + const result = Patch.deriveNewContentsFromChunks(filePath, [ |
| 129 | + { |
| 130 | + old_lines: ["line one"], |
| 131 | + new_lines: ["LINE ONE"], |
| 132 | + }, |
| 133 | + ]) |
| 134 | + expect(result.content).toContain("LINE ONE") |
| 135 | + // BOM should be stripped, not affect matching |
| 136 | + expect(result.content.charCodeAt(0)).not.toBe(0xfeff) |
| 137 | + }) |
| 138 | + |
| 139 | + test("handles whitespace-only differences via fuzzy matching", () => { |
| 140 | + const filePath = writeTemp("whitespace.txt", " indented\n also indented\n") |
| 141 | + const result = Patch.deriveNewContentsFromChunks(filePath, [ |
| 142 | + { |
| 143 | + old_lines: ["indented"], // no leading spaces in patch |
| 144 | + new_lines: ["REPLACED"], |
| 145 | + }, |
| 146 | + ]) |
| 147 | + expect(result.content).toContain("REPLACED") |
| 148 | + }) |
| 149 | + |
| 150 | + test("handles Unicode smart quotes via normalization", () => { |
| 151 | + // File has ASCII quotes, patch has smart quotes |
| 152 | + const filePath = writeTemp("unicode-quotes.txt", 'He said "hello"\nGoodbye\n') |
| 153 | + const result = Patch.deriveNewContentsFromChunks(filePath, [ |
| 154 | + { |
| 155 | + old_lines: ['He said \u201Chello\u201D'], // smart quotes |
| 156 | + new_lines: ['He said "hi"'], |
| 157 | + }, |
| 158 | + ]) |
| 159 | + expect(result.content).toContain('He said "hi"') |
| 160 | + }) |
| 161 | + |
| 162 | + test("throws on unmatched lines", () => { |
| 163 | + const filePath = writeTemp("nomatch.txt", "alpha\nbeta\ngamma\n") |
| 164 | + expect(() => |
| 165 | + Patch.deriveNewContentsFromChunks(filePath, [ |
| 166 | + { |
| 167 | + old_lines: ["nonexistent line"], |
| 168 | + new_lines: ["replacement"], |
| 169 | + }, |
| 170 | + ]), |
| 171 | + ).toThrow(/Failed to find expected lines/) |
| 172 | + }) |
| 173 | +}) |
0 commit comments