import { describe, expect, it } from "vitest"; import { parseDiffFromFile, parsePatchFiles } from "@pierre/diffs"; import { diffRows, findDiffMatches, rowLine, rowSide } from "#lib/components/lines/diff.ts"; const patch = `diff --git a/a.ts b/a.ts --- a/a.ts +++ b/a.ts @@ -2,4 +2,4 @@ two -three +THREE +three and a half four -five @@ -10,2 +10,2 @@ ten -eleven +ELEVEN `; const [file] = parsePatchFiles(patch)[0].files; const rows = diffRows(file); describe("diff rows", () => { it("walks a patch in unified order with both line numbers", () => { expect(rows).toEqual([ { deletion: 2, addition: 2, text: "two", gap: false }, { deletion: 3, text: "three", gap: false }, { addition: 3, text: "THREE", gap: false }, { addition: 4, text: "three and a half", gap: false }, { deletion: 4, addition: 5, text: "four", gap: false }, { deletion: 5, text: "five", gap: false }, { deletion: 10, addition: 10, text: "ten", gap: false }, { deletion: 11, text: "eleven", gap: false }, { addition: 11, text: "ELEVEN", gap: false } ]); }); it("fills the unchanged lines of a full-file diff", () => { const before = ["a", "b", "c", "d", "e"].join("\n") + "\n"; const after = ["a", "b", "C", "d", "e"].join("\n") + "\n"; const full = diffRows( parseDiffFromFile( { name: "x", contents: before }, { name: "x", contents: after }, { context: 0 } ) ); expect(full.map((row) => [row.deletion, row.addition, row.text, row.gap])).toEqual([ [1, 1, "a", true], [2, 2, "b", true], [3, undefined, "c", false], [undefined, 3, "C", false], [4, 4, "d", true], [5, 5, "e", true] ]); }); it("reads a row's line on either side", () => { expect(rowSide(rows[1])).toBe("deletions"); expect(rowSide(rows[0])).toBe("additions"); expect(rowLine(rows[4], "deletions")).toBe(4); expect(rowLine(rows[4], "additions")).toBe(5); expect(rowLine(rows[1], "additions")).toBeUndefined(); }); }); describe("diff search", () => { it("finds matches on the side each row belongs to", () => { expect(findDiffMatches("a", rows, "three", 100)).toEqual([ { id: "a", row: 1, line: 3, side: "deletions", start: 0, end: 5 }, { id: "a", row: 2, line: 3, side: "additions", start: 0, end: 5 }, { id: "a", row: 3, line: 4, side: "additions", start: 0, end: 5 } ]); }); });