Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 foldViewedFilesOnce,
6 isFileDiffCollapsed,
7 isLineInFileDiff,
8 toggleFileDiffFoldForViewed,
9} from "./pullRequestDiff.logic";
10
11/** Only the hunk ranges matter here; the viewer fills the rest in when it renders. */
12function fileWithHunks(
13 hunks: ReadonlyArray<{
14 deletionStart: number;
15 deletionCount: number;
16 additionStart: number;
17 additionCount: number;
18 }>,
19): FileDiffMetadata {
20 return { name: "src/app.ts", hunks } as unknown as FileDiffMetadata;
21}
22
23describe("isLineInFileDiff", () => {
24 const file = fileWithHunks([
25 { deletionStart: 10, deletionCount: 3, additionStart: 10, additionCount: 5 },
26 { deletionStart: 40, deletionCount: 0, additionStart: 42, additionCount: 2 },
27 ]);
28
29 it("places a line inside a hunk, on the side that hunk counts", () => {
30 expect(isLineInFileDiff(file, "right", 12)).toBe(true);
31 expect(isLineInFileDiff(file, "left", 11)).toBe(true);
32 });
33
34 it("includes the first line of a hunk and excludes the one past its last", () => {
35 // The boundaries are where an off-by-one would quietly move a conversation between lists.
36 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
37 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
38 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
39 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
40 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
41 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
42 });
43
44 it("keeps the two sides apart, since one line number means two lines", () => {
45 // The second hunk is a pure insertion: it deletes nothing, so nothing is on its left.
46 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
47 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
48 });
49
50 it("places nothing in a file whose hunks the host withheld", () => {
51 expect(isLineInFileDiff(fileWithHunks([]), "right", 1)).toBe(false);
52 });
53});
54
55describe("isFileDiffCollapsed", () => {
56 const NO_TOGGLES: ReadonlySet<string> = new Set();
57
58 it("opens every file before the reader has touched anything", () => {
59 expect(isFileDiffCollapsed("a.ts", null, NO_TOGGLES)).toBe(false);
60 expect(isFileDiffCollapsed("b.ts", null, NO_TOGGLES)).toBe(false);
61 });
62
63 it("opens every file once the toolbar has asked for it", () => {
64 // Pressing the toolbar clears the reader's own toggles, which is why the set is empty here.
65 expect(isFileDiffCollapsed("a.ts", "expanded", NO_TOGGLES)).toBe(false);
66 expect(isFileDiffCollapsed("b.ts", "expanded", NO_TOGGLES)).toBe(false);
67 });
68
69 it("folds every file again on the second press", () => {
70 expect(isFileDiffCollapsed("a.ts", "folded", NO_TOGGLES)).toBe(true);
71 expect(isFileDiffCollapsed("b.ts", "folded", NO_TOGGLES)).toBe(true);
72 });
73
74 it("keeps a file the reader folded closed as the next slice arrives", () => {
75 // The file keys grow with every slice, so the answer for one already folded must not depend
76 // on how many of them there are by then.
77 const toggled = new Set(["b.ts"]);
78 expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
79 expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
80 });
81
82 it("still answers to a toggle after either toolbar press", () => {
83 expect(isFileDiffCollapsed("a.ts", "expanded", new Set(["a.ts"]))).toBe(true);
84 expect(isFileDiffCollapsed("a.ts", "folded", new Set(["a.ts"]))).toBe(false);
85 });
86});
87
88describe("foldViewedFilesOnce", () => {
89 const none: ReadonlySet<string> = new Set();
90 const file = (name: string, viewed: boolean, keySuffix = "") => ({
91 fileKey: `${name}${keySuffix}`,
92 path: name,
93 viewed,
94 });
95
96 it("folds the files already ticked off, and only them, when the tab comes up expanded", () => {
97 const result = foldViewedFilesOnce(
98 [file("a", true), file("b", false), file("c", true)],
99 none,
100 "expanded",
101 none,
102 );
103 expect([...result.toggledFileKeys].sort()).toEqual(["a", "c"]);
104 expect([...result.seenPaths].sort()).toEqual(["a", "b", "c"]);
105 });
106
107 it("changes no fold when the tab comes up folded", () => {
108 const result = foldViewedFilesOnce([file("a", true)], none, "folded", none);
109 expect(result.toggledFileKeys).toBe(none);
110 expect([...result.seenPaths]).toEqual(["a"]);
111 });
112
113 it("leaves a file it has already seen to the reader, even when it is ticked now", () => {
114 const seen: ReadonlySet<string> = new Set(["a"]);
115 const result = foldViewedFilesOnce([file("a", true)], seen, "expanded", none);
116 expect(result.toggledFileKeys).toBe(none);
117 expect(result.seenPaths).toBe(seen);
118 });
119
120 it("recognises a file by its path when a whitespace toggle re-keys the diff", () => {
121 const first = foldViewedFilesOnce([file("a", true)], none, "expanded", none);
122 // The reader expanded it by hand (toggle cleared), then flipped ignore-whitespace.
123 const second = foldViewedFilesOnce([file("a", true, "#ws")], first.seenPaths, "expanded", none);
124 expect(second.toggledFileKeys).toBe(none);
125 });
126
127 it("folds only the files that arrived since the last pass", () => {
128 const first = foldViewedFilesOnce([file("a", true)], none, "expanded", none);
129 const second = foldViewedFilesOnce(
130 [file("a", false), file("b", true)],
131 first.seenPaths,
132 "expanded",
133 first.toggledFileKeys,
134 );
135 expect([...second.toggledFileKeys].sort()).toEqual(["a", "b"]);
136 });
137});
138
139describe("toggleFileDiffFoldForViewed", () => {
140 it("puts a file away when it is ticked off", () => {
141 // Files start expanded, so ticking one off is the case that has somewhere to go.
142 expect([...toggleFileDiffFoldForViewed("a.ts", true, null, new Set())]).toEqual(["a.ts"]);
143 });
144
145 it("brings a file back when the tick is taken off", () => {
146 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, new Set(["a.ts"]))]).toEqual([]);
147 });
148
149 it("leaves the fold alone when it already says what the tick does", () => {
150 const folded = new Set(["a.ts"]);
151 expect(toggleFileDiffFoldForViewed("a.ts", true, null, folded)).toBe(folded);
152 });
153
154 it("moves against whatever the toolbar last asked for", () => {
155 // Everything is open, so ticking a file off has to fold that one against the default.
156 expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
157 expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
158 });
159
160 it("touches only the file that was ticked", () => {
161 const toggled = new Set(["a.ts", "b.ts"]);
162 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, toggled)]).toEqual(["b.ts"]);
163 });
164});