Skip to content

Commit 9d38d68

Browse files
only21milcodexmaria-rcks
authored
fix(web): wait for fresh diff pages in review guides (pingdotgg#23225)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent b7c7a78 commit 9d38d68

3 files changed

Lines changed: 100 additions & 1 deletion

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
});

‎apps/web/src/components/pullRequest/guide/useGuideDiffFiles.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function useGuideDiffFiles({
5151
);
5252

5353
const data = query.data;
54-
if (data !== null && !current.done && current.folded !== data) {
54+
if (data !== null && !query.isPending && !current.done && current.folded !== data) {
5555
const parsed = getRenderablePatch(
5656
data.patch,
5757
`pull-request-guide:${key}:${current.cursor ?? "first"}`,

‎packages/client-runtime/src/state/runtime.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,35 @@ describe("runtime command runner", () => {
12121212
});
12131213

12141214
describe("persisted environment queries", () => {
1215+
it.effect("keeps an in-memory cached page pending until revalidation settles", () =>
1216+
Effect.scoped(
1217+
Effect.gen(function* () {
1218+
const refreshStarted = Latch.makeUnsafe();
1219+
const finishRefresh = Latch.makeUnsafe();
1220+
let executions = 0;
1221+
const harness = yield* makeEnvironmentQueryHarness(
1222+
Effect.suspend(() => {
1223+
if (++executions === 1) return Effect.succeed("cached");
1224+
refreshStarted.openUnsafe();
1225+
return finishRefresh.await.pipe(Effect.as("fresh"));
1226+
}),
1227+
{ persistence: { load: () => Effect.succeedNone, save: () => Effect.void } },
1228+
);
1229+
const registry = yield* mountEnvironmentQuery(harness.atom);
1230+
expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("cached");
1231+
registry.refresh(harness.atom);
1232+
yield* refreshStarted.await;
1233+
expect(registry.get(harness.atom)).toMatchObject({
1234+
_tag: "Success",
1235+
value: "cached",
1236+
waiting: true,
1237+
});
1238+
finishRefresh.openUnsafe();
1239+
expect(yield* readEnvironmentQuery(registry, harness.atom)).toBe("fresh");
1240+
}),
1241+
),
1242+
);
1243+
12151244
it.effect(
12161245
"restores cached data in a new registry while offline, then refreshes on reconnect",
12171246
() =>

0 commit comments

Comments
 (0)