|
| 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 | + pages: new Map<string | null, Atom.Writable<AsyncResult.AsyncResult<PullRequestDiffResult>>>(), |
| 10 | +})); |
| 11 | +vi.mock("~/state/pullRequests", () => ({ |
| 12 | + pullRequestEnvironment: { |
| 13 | + diff: ({ input }: { input: { cursor?: string } }) => state.pages.get(input.cursor ?? null)!, |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +import { useGuideDiffFiles } from "./useGuideDiffFiles"; |
| 18 | + |
| 19 | +function 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 | + |
| 36 | +function 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 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +it.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 | +}); |
0 commit comments