The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 69 lines 2.66 KB
Raw
1 import { renderToStaticMarkup } from "react-dom/server";
2 import { expect, it, vi } from "vite-plus/test";
3
4 import { buildReviewParsedDiff, type ReviewRenderableFile } from "./reviewModel";
5 import { useReviewFileVisibility } from "./reviewFileVisibility";
6
7 const stored = vi.hoisted(() => ({ expanded: undefined as ReadonlyArray<string> | undefined }));
8 vi.mock("./reviewState", () => ({
9 updateReviewExpandedFileIds: (
10 _threadKey: string,
11 _sectionId: string,
12 update: (current: ReadonlyArray<string> | undefined) => ReadonlyArray<string> | undefined,
13 ) => {
14 stored.expanded = update(stored.expanded);
15 },
16 updateReviewViewedFileIds: () => {},
17 }));
18
19 function patch(path: string, line: string) {
20 return `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+${line}\n`;
21 }
22
23 function files(diff: string): ReadonlyArray<ReviewRenderableFile> {
24 const parsed = buildReviewParsedDiff(diff, "turn:3");
25 if (parsed.kind !== "files") throw new Error("Expected structured diff");
26 return parsed.files;
27 }
28
29 function visibility(current: ReadonlyArray<ReviewRenderableFile>) {
30 let result: ReturnType<typeof useReviewFileVisibility> | undefined;
31 function Probe({ report }: { report: (value: typeof result) => void }) {
32 report(
33 useReviewFileVisibility({
34 threadKey: "thread",
35 sectionId: "turn:3",
36 files: current,
37 cachedExpandedFileIds: stored.expanded,
38 cachedViewedFileIds: undefined,
39 }),
40 );
41 return null;
42 }
43 renderToStaticMarkup(<Probe report={(value) => (result = value)} />);
44 return result!;
45 }
46
47 it("keeps files the reader left open expanded when the section's diff changes", () => {
48 stored.expanded = undefined;
49 const before = files(patch("a.ts", "one") + patch("b.ts", "two"));
50 visibility(before).toggleExpandedFile(before[0]!.id);
51 expect(visibility(before).collapsedFileIds).toEqual([before[0]!.id]);
52
53 const after = files(patch("a.ts", "changed") + patch("b.ts", "two"));
54 const collapsedPaths = visibility(after).collapsedFileIds.map(
55 (id) => after.find((file) => file.id === id)!.path,
56 );
57 expect(collapsedPaths).not.toContain("b.ts");
58 });
59
60 it("folds only the selected entry when a file becomes a symlink", () => {
61 stored.expanded = undefined;
62 const current = files(
63 "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" +
64 "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",
65 );
66 expect(current).toHaveLength(2);
67 visibility(current).toggleExpandedFile(current[0]!.id);
68 expect(visibility(current).collapsedFileIds).toEqual([current[0]!.id]);
69 });