import type { Hunk } from "@pierre/diffs"; import { describe, expect, it } from "vitest"; import { hiddenLine, newSideLine, reservedRows } from "#lib/components/repo/fileDiff.ts"; describe("reservedRows", () => { const file = (lines: number, insertions: number, deletions: number) => ({ lines, stat: { insertions, deletions } }); it("takes knotmirror's pair count as the split row count", () => { expect(reservedRows(file(40, 10, 6), true)).toBe(40); }); it("adds the replaced lines back for unified rows", () => { expect(reservedRows(file(40, 10, 6), false)).toBe(46); }); it("counts a pure addition the same either way", () => { expect(reservedRows(file(21, 21, 0), false)).toBe(21); expect(reservedRows(file(21, 21, 0), true)).toBe(21); }); it("falls back to the stat sum when the line count is unknown", () => { expect(reservedRows({ stat: { insertions: 10, deletions: 6 } }, true)).toBe(16); expect(reservedRows({ stat: { insertions: 10, deletions: 6 } }, false)).toBe(22); }); }); const hunk = ( deletionStart: number, deletionCount: number, additionStart: number, additionCount: number ): Hunk => ({ deletionStart, deletionCount, additionStart, additionCount }) as Hunk; const hunks = [hunk(10, 5, 10, 7), hunk(40, 3, 41, 0)]; describe("hiddenLine", () => { it("shows lines inside a hunk", () => { expect(hiddenLine(hunks, 12, "additions")).toBeUndefined(); expect(hiddenLine(hunks, 14, "deletions")).toBeUndefined(); expect(hiddenLine(hunks, 41, "deletions")).toBeUndefined(); }); it("maps hidden lines to the new side", () => { expect(hiddenLine(hunks, 3, "additions")).toBe(3); expect(hiddenLine(hunks, 3, "deletions")).toBe(3); expect(hiddenLine(hunks, 20, "deletions")).toBe(22); expect(hiddenLine(hunks, 30, "additions")).toBe(30); expect(hiddenLine(hunks, 50, "deletions")).toBe(49); expect(hiddenLine(hunks, 49, "additions")).toBe(49); }); }); describe("newSideLine", () => { it("puts a removed line where its hunk starts", () => { expect(newSideLine(hunks, 41, "deletions")).toBe(42); expect(newSideLine(hunks, 20, "deletions")).toBe(22); expect(newSideLine(hunks, 7, "additions")).toBe(7); }); });