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

useGuideDiffFiles.browser.test.tsx

70 lines2.7 KB

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.Writable<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 );
35​
36function page(path: string, value: string, nextCursor: string | null): PullRequestDiffResult {
37 return {
38 patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+${value}\n`,
39 truncated: false,
40 nextCursor,
41 };
43​
44it.each([
45 { name: "contains the requested file", path: "wanted.ts", cursor: null },
46 { name: "ends before a newly added file", path: "other.ts", cursor: null },
47 { name: "has an obsolete next cursor", path: "other.ts", cursor: "old-next" },
48])("waits for a cached page that $name to settle", async ({ path, cursor }) => {
49 state.pages.clear();
50 await using stack = new AsyncDisposableStack();
51 const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());
52 const first = Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(
53 AsyncResult.success(page(path, "STALE", cursor), { waiting: true }),
54 );
55 state.pages.set(null, first);
56 state.pages.set(
57 "old-next",
58 Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(AsyncResult.initial(true)),
59 );
60 await using screen = await render(
61 <RegistryContext.Provider value={registry}>
62 <Probe wantedPaths={["wanted.ts"]} />
63 </RegistryContext.Provider>,
64 );
65 const result = () => JSON.parse(screen.container.querySelector("output")!.textContent!);
66 expect(result()).toEqual({ loading: true, files: [] });
67​
68 await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));
69 expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] });
70});