import { describe, expect, test } from "bun:test" import { Patch } from "@opencode-ai/core/patch" import { Result } from "effect" const parse = (input: string) => Result.getOrThrow(Patch.parse(input)) describe("Patch", () => { test("parses add, update, and delete hunks", () => { expect( parse( "*** Begin Patch\n*** Add File: add.txt\n+added\n*** Update File: update.txt\n@@ section\n-old\n+new\n*** Delete File: delete.txt\n*** End Patch", ), ).toEqual([ { type: "add", path: "add.txt", contents: "added" }, { type: "update", path: "update.txt", chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: "section", endOfFile: undefined }], movePath: undefined, }, { type: "delete", path: "delete.txt" }, ]) }) test("parses a file move", () => { expect( parse( "*** Begin Patch\n*** Update File: old.txt\n*** Move to: new.txt\n@@\n-old\n+new\n*** End Patch", ), ).toEqual([ { type: "update", path: "old.txt", movePath: "new.txt", chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }], }, ]) }) test("identifies the missing patch boundary", () => { expect(() => parse("This is not a valid patch")).toThrow( "The first line of the patch must be '*** Begin Patch'", ) expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added")).toThrow( "The last line of the patch must be '*** End Patch'", ) }) test("strips a heredoc wrapper", () => { expect(parse("cat <<'EOF'\n*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nEOF")).toEqual([ { type: "add", path: "add.txt", contents: "added" }, ]) }) test("strips a heredoc wrapper without cat", () => { expect(parse("< { expect(parse("*** Begin Patch\n *** Update File: foo.txt\n@@\n-old\n+new\n*** End Patch")).toEqual([ { type: "update", path: "foo.txt", movePath: undefined, chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }], }, ]) }) test("parses leading and trailing whitespace around patch markers", () => { expect(parse(" *** Begin Patch\n*** Update File: file.txt\n@@\n-one\n+two\n*** End Patch ")).toEqual([ { type: "update", path: "file.txt", movePath: undefined, chunks: [{ oldLines: ["one"], newLines: ["two"], changeContext: undefined, endOfFile: undefined }], }, ]) }) test("parses whitespace on the inner sides of patch marker lines", () => { expect(parse("*** Begin Patch \n*** Update File: file.txt\n@@\n-one\n+two\n *** End Patch")).toEqual([ { type: "update", path: "file.txt", movePath: undefined, chunks: [{ oldLines: ["one"], newLines: ["two"], changeContext: undefined, endOfFile: undefined }], }, ]) }) test("strips one carriage return from CRLF patch lines", () => { expect(parse("*** Begin Patch\r\n*** Update File: file.txt\r\n@@\r\n-old\r\n+new\r\n*** End Patch\r\n")).toEqual([ { type: "update", path: "file.txt", movePath: undefined, chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }], }, ]) }) test("preserves an extra carriage return in CRLF patch lines", () => { expect(parse("*** Begin Patch\r\n*** Update File: file.txt\r\n@@\r\n-old\r\r\n+new\r\n*** End Patch\r\n")).toEqual([ { type: "update", path: "file.txt", movePath: undefined, chunks: [{ oldLines: ["old\r"], newLines: ["new"], changeContext: undefined, endOfFile: undefined }], }, ]) }) test("derives fuzzy line updates while preserving BOM", () => { const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n") expect(update).toEqual({ content: "new\n", bom: true }) expect(Patch.joinBom(update.content, update.bom)).toBe("\uFEFFnew\n") }) test("derives multiple update chunks", () => { expect( Patch.derive( "update.txt", [ { oldLines: ["line 2"], newLines: ["LINE 2"] }, { oldLines: ["line 4"], newLines: ["LINE 4"] }, ], "line 1\nline 2\nline 3\nline 4\n", ).content, ).toBe("line 1\nLINE 2\nline 3\nLINE 4\n") }) test("updates empty files and adds a trailing newline", () => { expect(Patch.derive("empty.txt", [{ oldLines: [], newLines: ["First line"] }], "").content).toBe( "First line\n", ) expect(Patch.derive("no-newline.txt", [{ oldLines: ["old"], newLines: ["new"] }], "old").content).toBe( "new\n", ) }) test("disambiguates updates with change context", () => { expect( Patch.derive( "update.txt", [{ oldLines: ["x=10"], newLines: ["x=11"], changeContext: "fn b" }], "fn a\nx=10\nfn b\nx=10\n", ).content, ).toBe("fn a\nx=10\nfn b\nx=11\n") }) test("matches leading, trailing, and Unicode punctuation differences", () => { expect(Patch.derive("leading.txt", [{ oldLines: ["line"], newLines: ["next"] }], " line\n").content).toBe( "next\n", ) expect(Patch.derive("trailing.txt", [{ oldLines: ["line"], newLines: ["next"] }], "line \n").content).toBe( "next\n", ) expect( Patch.derive('unicode.txt', [{ oldLines: ['He said "hello"'], newLines: ['He said "hi"'] }], 'He said “hello”\n') .content, ).toBe('He said "hi"\n') }) test("matches EOF-anchored chunks from the end", () => { expect( Patch.derive( "update.txt", [{ oldLines: ["marker", "end"], newLines: ["marker changed", "end"], endOfFile: true }], "marker\nmiddle\nmarker\nend\n", ).content, ).toBe("marker\nmiddle\nmarker changed\nend\n") }) test("matches V1 lenient parsing of malformed hunk bodies", () => { expect(parse("*** Begin Patch\n*** Add File: add.txt\nmissing plus\n*** End Patch")).toEqual([ { type: "add", path: "add.txt", contents: "" }, ]) expect(parse("*** Begin Patch\n*** Update File: update.txt\n*** End Patch")).toEqual([ { type: "update", path: "update.txt", movePath: undefined, chunks: [] }, ]) expect(parse("*** Begin Patch\n*** Delete File: delete.txt\nunexpected body\n*** End Patch")).toEqual([ { type: "delete", path: "delete.txt" }, ]) }) })