90 lines
2.9 KiB
TypeScript
90 lines
2.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
createQueryClient,
|
|
QUERY_CACHE_DEFAULTS,
|
|
} from "../../../src/adapters/query-cache/tanstack-query-cache.ts";
|
|
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
|
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
|
import {
|
|
jsonRows,
|
|
networkFailure,
|
|
scopeSnapshot,
|
|
testExecutor,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
|
|
const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
|
|
unknown,
|
|
unknown,
|
|
unknown
|
|
>;
|
|
|
|
/**
|
|
* Spec §16. Multiple consumers of one query identity must not imply multiple
|
|
* physical requests. TanStack Query owns in-flight coalescing; this suite pins
|
|
* that the installed query client plus the real V3 executor keep it that way.
|
|
*/
|
|
describe("request deduplication and coalescing", () => {
|
|
function loadSameQuery(
|
|
client: ReturnType<typeof createQueryClient>,
|
|
executor: ReturnType<typeof testExecutor>,
|
|
key: readonly unknown[] = ["entities", "list", { limit: 20 }],
|
|
) {
|
|
return client.fetchQuery({
|
|
queryKey: key,
|
|
queryFn: ({ signal }) =>
|
|
executor.execute(TEST_QUERY, { limit: 20 }, {
|
|
routeId: "TEST_ROUTE",
|
|
scope: scopeSnapshot(),
|
|
signal,
|
|
}),
|
|
});
|
|
}
|
|
|
|
it("coalesces concurrent consumers of the same query identity", async () => {
|
|
const transport = vi.fn<Transport>(async () => jsonRows(3));
|
|
const executor = testExecutor(transport);
|
|
const client = createQueryClient();
|
|
|
|
const results = await Promise.all([
|
|
loadSameQuery(client, executor),
|
|
loadSameQuery(client, executor),
|
|
loadSameQuery(client, executor),
|
|
]);
|
|
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
expect(results.every((result) => result.kind === "SUCCESS")).toBe(true);
|
|
client.clear();
|
|
});
|
|
|
|
it("keeps distinct query identities as distinct physical requests", async () => {
|
|
const transport = vi.fn<Transport>(async () => jsonRows(1));
|
|
const executor = testExecutor(transport);
|
|
const client = createQueryClient();
|
|
|
|
await Promise.all([
|
|
loadSameQuery(client, executor, ["entities", "list", 1]),
|
|
loadSameQuery(client, executor, ["entities", "list", 2]),
|
|
]);
|
|
|
|
expect(transport).toHaveBeenCalledTimes(2);
|
|
client.clear();
|
|
});
|
|
|
|
it("does not stack a query-layer retry on top of the executor retry budget", async () => {
|
|
expect(QUERY_CACHE_DEFAULTS.retry).toBe(false);
|
|
expect(QUERY_CACHE_DEFAULTS.mutationRetry).toBe(false);
|
|
|
|
const transport = vi.fn<Transport>(networkFailure);
|
|
const executor = testExecutor(transport, { maxRetryAttempts: 2 });
|
|
const client = createQueryClient();
|
|
|
|
await Promise.all([loadSameQuery(client, executor), loadSameQuery(client, executor)]);
|
|
|
|
// One coalesced logical execution: 1 attempt + at most 2 executor retries.
|
|
expect(transport.mock.calls.length).toBeLessThanOrEqual(3);
|
|
client.clear();
|
|
});
|
|
});
|