TypeScript
fix(web): wait for fresh diff pages in review guides (#23225)
Victor Vogel9d38d68d0437
diff --git a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsxnew file mode 100644index 00000000000..cdb7d4099e5--- /dev/null+++ b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.browser.test.tsx@@ -0,0 +1,70 @@+import { RegistryContext } from "@effect/atom-react";+import { EnvironmentId, ProjectId, type PullRequestDiffResult } from "@t3tools/contracts";+import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";+import { expect, it, vi } from "vite-plus/test";++import { act, render } from "../../../../test/render";++const state = vi.hoisted(() => ({+ pages: new Map<string | null, Atom.Writable<AsyncResult.AsyncResult<PullRequestDiffResult>>>(),+}));+vi.mock("~/state/pullRequests", () => ({+ pullRequestEnvironment: {+ diff: ({ input }: { input: { cursor?: string } }) => state.pages.get(input.cursor ?? null)!,+ },+}));++import { useGuideDiffFiles } from "./useGuideDiffFiles";++function Probe({ wantedPaths }: { wantedPaths: string[] }) {+ const diff = useGuideDiffFiles({+ environmentId: EnvironmentId.make("test"),+ reference: { projectId: ProjectId.make("project"), repository: "owner/repo", number: 1 },+ headSha: "new-head",+ wantedPaths,+ });+ return (+ <output>+ {JSON.stringify({+ loading: diff.loading,+ files: [...diff.files].map(([path, file]) => [path, file.additionLines.join("")]),+ })}+ </output>+ );+}++function page(path: string, value: string, nextCursor: string | null): PullRequestDiffResult {+ return {+ patch: `diff --git a/${path} b/${path}\n--- a/${path}\n+++ b/${path}\n@@ -1 +1 @@\n-old\n+${value}\n`,+ truncated: false,+ nextCursor,+ };+}++it.each([+ { name: "contains the requested file", path: "wanted.ts", cursor: null },+ { name: "ends before a newly added file", path: "other.ts", cursor: null },+ { name: "has an obsolete next cursor", path: "other.ts", cursor: "old-next" },+])("waits for a cached page that $name to settle", async ({ path, cursor }) => {+ state.pages.clear();+ await using stack = new AsyncDisposableStack();+ const registry = stack.adopt(AtomRegistry.make(), (value) => value.dispose());+ const first = Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(+ AsyncResult.success(page(path, "STALE", cursor), { waiting: true }),+ );+ state.pages.set(null, first);+ state.pages.set(+ "old-next",+ Atom.make<AsyncResult.AsyncResult<PullRequestDiffResult>>(AsyncResult.initial(true)),+ );+ await using screen = await render(+ <RegistryContext.Provider value={registry}>+ <Probe wantedPaths={["wanted.ts"]} />+ </RegistryContext.Provider>,+ );+ const result = () => JSON.parse(screen.container.querySelector("output")!.textContent!);+ expect(result()).toEqual({ loading: true, files: [] });++ await act(() => registry.set(first, AsyncResult.success(page("wanted.ts", "FRESH", null))));+ expect(result()).toEqual({ loading: false, files: [["wanted.ts", "FRESH"]] });+});diff --git a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.ts b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.tsindex c905488949c..21765c68d18 100644--- a/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.ts+++ b/apps/web/src/components/pullRequest/guide/useGuideDiffFiles.ts@@ -51,7 +51,7 @@ export function useGuideDiffFiles({);const data = query.data;- if (data !== null && !current.done && current.folded !== data) {+ if (data !== null && !query.isPending && !current.done && current.folded !== data) {const parsed = getRenderablePatch(data.patch,`pull-request-guide:${key}:${current.cursor ?? "first"}`,diff --git a/packages/client-runtime/src/state/runtime.test.ts b/packages/client-runtime/src/state/runtime.test.tsindex 963b3a7bfde..519ccd025b0 100644--- a/packages/client-runtime/src/state/runtime.test.ts+++ b/packages/client-runtime/src/state/runtime.test.ts@@ -1212,6 +1212,35 @@ describe("runtime command runner", () => {});describe("persisted environment queries", () => {+ it.effect("keeps an in-memory cached page pending until revalidation settles", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const refreshStarted = Latch.makeUnsafe();+ const finishRefresh = Latch.makeUnsafe();+ let executions = 0;+ const harness = yield* makeEnvironmentQueryHarness(+ Effect.suspend(() => {+ if (++executions === 1) return Effect.succeed("cached");+ refreshStarted.openUnsafe();+ return finishRefresh.await.pipe(Effect.as("fresh"));+ }),+ { persistence: { load: () => Effect.succeedNone, save: () => Effect.void } },+ );+ const registry = yield* mountEnvironmentQuery(harness.atom);+ expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("cached");+ registry.refresh(harness.atom);+ yield* refreshStarted.await;+ expect(registry.get(harness.atom)).toMatchObject({+ _tag: "Success",+ value: "cached",+ waiting: true,+ });+ finishRefresh.openUnsafe();+ expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("fresh");+ }),+ ),+ );+it.effect("restores cached data in a new registry while offline, then refreshes on reconnect",() =>