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