Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestDiff.logic.test.ts

81 lines3.4 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { isFileDiffCollapsed, isLineInFileDiff } from "./pullRequestDiff.logic";
5​
6/** Only the hunk ranges matter here; the viewer fills the rest in when it renders. */
7function fileWithHunks(
8 hunks: ReadonlyArray<{
9 deletionStart: number;
10 deletionCount: number;
11 additionStart: number;
12 additionCount: number;
13 }>,
14): FileDiffMetadata {
15 return { name: "src/app.ts", hunks } as unknown as FileDiffMetadata;
17​
18describe("isLineInFileDiff", () => {
19 const file = fileWithHunks([
20 { deletionStart: 10, deletionCount: 3, additionStart: 10, additionCount: 5 },
21 { deletionStart: 40, deletionCount: 0, additionStart: 42, additionCount: 2 },
22 ]);
23​
24 it("places a line inside a hunk, on the side that hunk counts", () => {
25 expect(isLineInFileDiff(file, "right", 12)).toBe(true);
26 expect(isLineInFileDiff(file, "left", 11)).toBe(true);
27 });
28​
29 it("includes the first line of a hunk and excludes the one past its last", () => {
30 // The boundaries are where an off-by-one would quietly move a conversation between lists.
31 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
32 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
33 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
34 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
35 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
36 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
37 });
38​
39 it("keeps the two sides apart, since one line number means two lines", () => {
40 // The second hunk is a pure insertion: it deletes nothing, so nothing is on its left.
41 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
42 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
43 });
44​
45 it("places nothing in a file whose hunks the host withheld", () => {
46 expect(isLineInFileDiff(fileWithHunks([]), "right", 1)).toBe(false);
47 });
48});
49​
50describe("isFileDiffCollapsed", () => {
51 const NO_TOGGLES: ReadonlySet<string> = new Set();
52​
53 it("opens every file before the reader has touched anything", () => {
54 expect(isFileDiffCollapsed("a.ts", null, NO_TOGGLES)).toBe(false);
55 expect(isFileDiffCollapsed("b.ts", null, NO_TOGGLES)).toBe(false);
56 });
57​
58 it("opens every file once the toolbar has asked for it", () => {
59 // Pressing the toolbar clears the reader's own toggles, which is why the set is empty here.
60 expect(isFileDiffCollapsed("a.ts", "expanded", NO_TOGGLES)).toBe(false);
61 expect(isFileDiffCollapsed("b.ts", "expanded", NO_TOGGLES)).toBe(false);
62 });
63​
64 it("folds every file again on the second press", () => {
65 expect(isFileDiffCollapsed("a.ts", "folded", NO_TOGGLES)).toBe(true);
66 expect(isFileDiffCollapsed("b.ts", "folded", NO_TOGGLES)).toBe(true);
67 });
68​
69 it("keeps a file the reader folded closed as the next slice arrives", () => {
70 // The file keys grow with every slice, so the answer for one already folded must not depend
71 // on how many of them there are by then.
72 const toggled = new Set(["b.ts"]);
73 expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
74 expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
75 });
76​
77 it("still answers to a toggle after either toolbar press", () => {
78 expect(isFileDiffCollapsed("a.ts", "expanded", new Set(["a.ts"]))).toBe(true);
79 expect(isFileDiffCollapsed("a.ts", "folded", new Set(["a.ts"]))).toBe(false);
80 });
81});