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

main
tsx 170 lines 6.15 KB
Raw
1 import { RegistryContext } from "@effect/atom-react";
2 import { EnvironmentId, ProjectId, type PullRequestDiffResult } from "@t3tools/contracts";
3 import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
4 import { expect, it, vi } from "vite-plus/test";
5
6 import { act, render } from "../../../../test/render";
7
8 const state = vi.hoisted(() => ({
9 heads: new Map<
10 string,
11 Map<string | null, Atom.Atom<AsyncResult.AsyncResult<PullRequestDiffResult>>>
12 >(),
13 pages: new Map<string | null, Atom.Atom<AsyncResult.AsyncResult<PullRequestDiffResult>>>(),
14 }));
15 vi.mock("~/state/pullRequests", () => ({
16 pullRequestEnvironment: {
17 diff: ({ input }: { input: { cursor?: string; cacheHeadSha?: string } }) =>
18 (state.heads.get(input.cacheHeadSha ?? "") ?? state.pages).get(input.cursor ?? null)!,
19 },
20 }));
21
22 import { useGuideDiffFiles } from "./useGuideDiffFiles";
23
24 function Probe({ wantedPaths }: { wantedPaths: string[] }) {
25 const diff = useGuideDiffFiles({
26 environmentId: EnvironmentId.make("test"),
27 reference: { projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 },
28 headSha: "new-head",
29 wantedPaths,
30 });
31 return (
32 <output>
33 {JSON.stringify({
34 loading: diff.loading,
35 files: [...diff.files].map(([path, file]) => [path, file.additionLines.join("")]),
36 })}
37 </output>
38 );
39 }
40
41 function setup() {
42 state.pages.clear();
43 state.heads.clear();
44 }
45
46 function page(path: string, value: string, nextCursor: string | null): PullRequestDiffResult {
47 return {
48 patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+${value}\n`,
49 truncated: false,
50 nextCursor,
51 };
52 }
53
54 it.each([
55 { name: "contains the requested file", path: "wanted.ts", cursor: null },
56 { name: "ends before a newly added file", path: "other.ts", cursor: null },
57 { name: "has an obsolete next cursor", path: "other.ts", cursor: "old-next" },
58 ])("waits for a cached page that $name to settle", async ({ path, cursor }) => {
59 setup();
60 await using stack = new AsyncDisposableStack();
61 const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
62 const first = Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
63 AsyncResult.success(page(path, "STALE", cursor), { waiting: true }),
64 );
65 state.pages.set(null, first);
66 state.pages.set(
67 "old-next",
68 Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(AsyncResult.initial(true)),
69 );
70 await using screen = await render(
71 <RegistryContext.Provider value={registry}>
72 <Probe wantedPaths={["wanted.ts"]} />
73 </RegistryContext.Provider>,
74 );
75 const result = () => JSON.parse(screen.container.querySelector("output")!.textContent!);
76 expect(result()).toEqual({ loading: true, files: [] });
77
78 await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));
79 expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH\n"]] });
80 });
81
82 it("walks more cached pages than React's re-render limit and stops at the requested file", async () => {
83 setup();
84 await using stack = new AsyncDisposableStack();
85 const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
86 const pageCount = 30;
87 for (let index = 0; index < pageCount; index++) {
88 const last = index === pageCount - 1;
89 state.pages.set(
90 index === 0 ? null : `cursor-${index}`,
91 Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
92 AsyncResult.success(
93 page(last ? "wanted.ts" : `file-${index}.ts`, "FOUND", `cursor-${index + 1}`),
94 ),
95 ),
96 );
97 }
98 let pastPageReads = 0;
99 state.pages.set(
100 `cursor-${pageCount}`,
101 Atom.make((): AsyncResult.AsyncResult<PullRequestDiffResult> => {
102 pastPageReads++;
103 return AsyncResult.initial(true);
104 }),
105 );
106 await using screen = await render(
107 <RegistryContext.Provider value={registry}>
108 <Probe wantedPaths={["wanted.ts"]} />
109 </RegistryContext.Provider>,
110 );
111 const result = JSON.parse(screen.container.querySelector("output")!.textContent!);
112 expect(result.loading).toBe(false);
113 expect(result.files).toContainEqual(["wanted.ts", "FOUND\n"]);
114 expect(pastPageReads).toBe(0);
115 });
116
117 it("waits for a later cached page that is still refreshing", async () => {
118 setup();
119 await using stack = new AsyncDisposableStack();
120 const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
121 state.pages.set(
122 null,
123 Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
124 AsyncResult.success(page("first.ts", "FIRST", "cursor-1")),
125 ),
126 );
127 const second = Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
128 AsyncResult.success(page("other.ts", "STALE", "old-next"), { waiting: true }),
129 );
130 state.pages.set("cursor-1", second);
131 state.pages.set(
132 "old-next",
133 Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(AsyncResult.initial(true)),
134 );
135 await using screen = await render(
136 <RegistryContext.Provider value={registry}>
137 <Probe wantedPaths={["wanted.ts"]} />
138 </RegistryContext.Provider>,
139 );
140 const result = () => JSON.parse(screen.container.querySelector("output")!.textContent!);
141 expect(result()).toEqual({ loading: true, files: [["first.ts", "FIRST\n"]] });
142
143 await act(() => registry.set(second, AsyncResult.success(page("wanted.ts", "FRESH", null))));
144 expect(result()).toEqual({
145 loading: false,
146 files: [
147 ["first.ts", "FIRST\n"],
148 ["wanted.ts", "FRESH\n"],
149 ],
150 });
151 });
152
153 it("reads the guide head's pages instead of the previous push's cached ones", async () => {
154 setup();
155 await using stack = new AsyncDisposableStack();
156 const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
157 state.pages.set(null, Atom.make(AsyncResult.success(page("wanted.ts", "OLD_HEAD", null))));
158 state.heads.set(
159 "new-head",
160 new Map([[null, Atom.make(AsyncResult.success(page("wanted.ts", "NEW_HEAD", null)))]]),
161 );
162 await using screen = await render(
163 <RegistryContext.Provider value={registry}>
164 <Probe wantedPaths={["wanted.ts"]} />
165 </RegistryContext.Provider>,
166 );
167 expect(JSON.parse(screen.container.querySelector("output")!.textContent!).files).toEqual([
168 ["wanted.ts", "NEW_HEAD\n"],
169 ]);
170 });