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

pullRequestDiff.logic.test.ts

147 lines5.5 KB

1import type { FileDiffMetadata } from "@pierre/diffs";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 isFileDiffCollapsed,
6 isLineInFileDiff,
7 reconcileDiffSlices,
8 type DiffSlice,
9 toggleFileDiffFoldForViewed,
10} from "./pullRequestDiff.logic";
11​
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;
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 expect(isLineInFileDiff(file, "right", 10)).toBe(true);
36 expect(isLineInFileDiff(file, "right", 14)).toBe(true);
37 expect(isLineInFileDiff(file, "right", 15)).toBe(false);
38 expect(isLineInFileDiff(file, "left", 9)).toBe(false);
39 expect(isLineInFileDiff(file, "left", 12)).toBe(true);
40 expect(isLineInFileDiff(file, "left", 13)).toBe(false);
41 });
42​
43 it("keeps the two sides apart, since one line number means two lines", () => {
44 expect(isLineInFileDiff(file, "right", 43)).toBe(true);
45 expect(isLineInFileDiff(file, "left", 40)).toBe(false);
46 });
47​
48 it("places nothing in a file whose hunks the host withheld", () => {
49 expect(isLineInFileDiff(fileWithHunks([]), "right", 1)).toBe(false);
50 });
51});
52​
53describe("isFileDiffCollapsed", () => {
54 const NO_TOGGLES: ReadonlySet<string> = new Set();
55​
56 it("opens every file before the reader has touched anything", () => {
57 expect(isFileDiffCollapsed("a.ts", null, NO_TOGGLES)).toBe(false);
58 expect(isFileDiffCollapsed("b.ts", null, NO_TOGGLES)).toBe(false);
59 });
60​
61 it("opens every file once the toolbar has asked for it", () => {
62 expect(isFileDiffCollapsed("a.ts", "expanded", NO_TOGGLES)).toBe(false);
63 expect(isFileDiffCollapsed("b.ts", "expanded", NO_TOGGLES)).toBe(false);
64 });
65​
66 it("folds every file again on the second press", () => {
67 expect(isFileDiffCollapsed("a.ts", "folded", NO_TOGGLES)).toBe(true);
68 expect(isFileDiffCollapsed("b.ts", "folded", NO_TOGGLES)).toBe(true);
69 });
70​
71 it("keeps a file the reader folded closed as the next slice arrives", () => {
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});
82​
83describe("toggleFileDiffFoldForViewed", () => {
84 it("puts a file away when it is ticked off", () => {
85 expect([...toggleFileDiffFoldForViewed("a.ts", true, null, new Set())]).toEqual(["a.ts"]);
86 });
87​
88 it("brings a file back when the tick is taken off", () => {
89 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, new Set(["a.ts"]))]).toEqual([]);
90 });
91​
92 it("leaves the fold alone when it already says what the tick does", () => {
93 const folded = new Set(["a.ts"]);
94 expect(toggleFileDiffFoldForViewed("a.ts", true, null, folded)).toBe(folded);
95 });
96​
97 it("moves against whatever the toolbar last asked for", () => {
98 expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
99 expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
100 });
101​
102 it("touches only the file that was ticked", () => {
103 const toggled = new Set(["a.ts", "b.ts"]);
104 expect([...toggleFileDiffFoldForViewed("a.ts", false, null, toggled)]).toEqual(["b.ts"]);
105 });
106});
107​
108describe("reconcileDiffSlices", () => {
109 const slice = (cursor: string | null, patch: string, nextCursor: string | null): DiffSlice => ({
110 cursor,
111 patch,
112 truncated: false,
113 nextCursor,
114 omittedFileStats: [{ path: "large.bin", additions: 1, deletions: 0 }],
115 });
116 const loaded = [slice(null, "a", "2"), slice("2", "b", "3"), slice("3", "c", "4")];
117​
118 it("revalidates unchanged loaded pages in order without replacing them or loading more", () => {
119 let state = { cursor: null as string | null, slices: loaded as ReadonlyArray<DiffSlice> };
120 for (const [cursor, patch, nextCursor, expectedCursor] of [
121 [null, "a", "2", "2"],
122 ["2", "b", "3", "3"],
123 ["3", "c", "4", "3"],
124 ] as const) {
125 expect(state.cursor).toBe(cursor);
126 state = reconcileDiffSlices(state.slices, slice(cursor, patch, nextCursor));
127 expect(state.cursor).toBe(expectedCursor);
128 expect(state.slices).toBe(loaded);
129 }
130 });
131​
132 it("keeps earlier pages but replaces a changed page and discards its descendants", () => {
133 const replacement = slice("2", "updated b", "new-3");
134 const refreshed = reconcileDiffSlices(loaded, replacement);
135​
136 expect(refreshed.cursor).toBe("2");
137 expect(refreshed.slices).toEqual([loaded[0], replacement]);
138 expect(refreshed.slices[0]).toBe(loaded[0]);
139 expect(loaded).toHaveLength(3);
140​
141 const nextPage = slice("new-3", "updated c", null);
142 expect(reconcileDiffSlices(refreshed.slices, nextPage)).toEqual({
143 cursor: "new-3",
144 slices: [loaded[0], replacement, nextPage],
145 });
146 });
147});