Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"]] });
});
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`,
Expand Down
29 changes: 29 additions & 0 deletions packages/client-runtime/src/state/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,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",
() =>
Expand Down
Loading