Open History to see the last commit
1import { renderToStaticMarkup } from "react-dom/server";
2import { expect, it, vi } from "vite-plus/test";
3
4import { buildReviewParsedDiff, type ReviewRenderableFile } from "./reviewModel";
5import { useReviewFileVisibility } from "./reviewFileVisibility";
6
7const stored = vi.hoisted(() => ({
8 expanded: undefined as ReadonlyArray<string> | undefined,
9 viewed: undefined as ReadonlyArray<string> | undefined,
10}));
11vi.mock("./reviewState", () => ({
12 updateReviewExpandedFileIds: (
13 _threadKey: string,
14 _sectionId: string,
15 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
16 ) => {
17 stored.expanded = update(stored.expanded);
18 },
19 updateReviewViewedFileIds: (
20 _threadKey: string,
21 _sectionId: string,
22 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
23 ) => {
24 stored.viewed = update(stored.viewed);
25 },
26}));
27
28function patch(path: string, line: string) {
29 return `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+${line}\n`;
30}
31
32function files(diff: string): ReadonlyArray<ReviewRenderableFile> {
33 const parsed = buildReviewParsedDiff(diff, "turn:3");
34 if (parsed.kind !== "files") throw new Error("Expected structured diff");
35 return parsed.files;
36}
37
38function visibility(
39 current: ReadonlyArray<ReviewRenderableFile>,
40 section: ReadonlyArray<ReviewRenderableFile> = current,
41) {
42 let result: ReturnType<typeof useReviewFileVisibility> | undefined;
43 function Probe({ report }: { report: (value: typeof result) => void }) {
44 report(
45 useReviewFileVisibility({
46 threadKey: "thread",
47 sectionId: "turn:3",
48 files: current,
49 sectionFiles: section,
50 cachedExpandedFileIds: stored.expanded,
51 cachedViewedFileIds: stored.viewed,
52 }),
53 );
54 return null;
55 }
56 renderToStaticMarkup(<Probe report={(value) => (result = value)} />);
57 return result!;
58}
59
60it("keeps files the reader left open expanded when the section's diff changes", () => {
61 stored.expanded = undefined;
62 const before = files(patch("a.ts", "one") + patch("b.ts", "two"));
63 visibility(before).toggleExpandedFile(before[0]!.id);
64 expect(visibility(before).collapsedFileIds).toEqual([before[0]!.id]);
65
66 const after = files(patch("a.ts", "changed") + patch("b.ts", "two"));
67 const collapsedPaths = visibility(after).collapsedFileIds.map(
68 (id) => after.find((file) => file.id === id)!.path,
69 );
70 expect(collapsedPaths).not.toContain("b.ts");
71});
72
73it("folds only the selected entry when a file becomes a symlink", () => {
74 stored.expanded = undefined;
75 const current = files(
76 "diff --git a/a.ts b/a.ts\ndeleted file mode 100644\n--- a/a.ts\n+++ /dev/null\n@@ -1 +0,0 @@\n-old\n" +
77 "diff --git a/a.ts b/a.ts\nnew file mode 120000\n--- /dev/null\n+++ b/a.ts\n@@ -0,0 +1 @@\n+b.ts\n\\ No newline at end of file\n",
78 );
79 expect(current).toHaveLength(2);
80 visibility(current).toggleExpandedFile(current[0]!.id);
81 expect(visibility(current).collapsedFileIds).toEqual([current[0]!.id]);
82});
83
84it("keeps the marks of files a repository filter hides", () => {
85 stored.expanded = undefined;
86 stored.viewed = undefined;
87 const all = files(
88 patch("web/a.ts", "one") + patch("api/b.ts", "two") + patch("api/c.ts", "three"),
89 );
90 const [webFile, apiFile, otherApiFile] = all;
91 visibility(all).toggleViewedFile(webFile!.id);
92
93 const apiOnly = all.slice(1);
94 visibility(apiOnly).toggleViewedFile(apiFile!.id);
95 visibility(apiOnly).toggleExpandedFile(otherApiFile!.id);
96
97 expect(visibility(all).viewedFileIds).toEqual([webFile!.id, apiFile!.id]);
98 expect(visibility(all).collapsedFileIds).toEqual(all.map((file) => file.id));
99 visibility(apiOnly).toggleViewedFile(apiFile!.id);
100 expect(visibility(all).viewedFileIds).toEqual([webFile!.id]);
101
102 stored.expanded = undefined;
103 visibility(apiOnly, all).toggleExpandedFile(apiFile!.id);
104 expect(visibility(all).collapsedFileIds).toEqual([apiFile!.id]);
105 stored.expanded = undefined;
106 stored.viewed = undefined;
107 visibility(apiOnly, all).toggleViewedFile(otherApiFile!.id);
108 expect(visibility(all).collapsedFileIds).toEqual([otherApiFile!.id]);
109});