~james/core

core/web/src/lib/components/repo/fileDiff.test.ts -rw-r--r-- 2.1 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
	});
});