138 lines
4.1 KiB
TypeScript
138 lines
4.1 KiB
TypeScript
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
import {
|
|
type ReactNode,
|
|
useEffect,
|
|
useState,
|
|
} from "react";
|
|
|
|
import { MutationIntentProvider } from "../../src/presentation/adapters/query/mutation-intent-provider.tsx";
|
|
import { QueryInvalidationProvider } from "../../src/presentation/adapters/query/query-invalidation-provider.tsx";
|
|
import { ServerStateScopeProvider } from "../../src/presentation/adapters/query/server-state-scope-provider.tsx";
|
|
import type { MutationIntentFactory } from "../../src/application/ports/mutation-intent-factory.ts";
|
|
import type { QueryInvalidationCoordinator } from "../../src/contracts/query-invalidation.ts";
|
|
import { createRuntimeIdentityRegistry } from "../../src/contracts/query-keys.ts";
|
|
import type {
|
|
CacheScopeSnapshot,
|
|
ServerStateScopeRuntime,
|
|
} from "../../src/contracts/server-state-scope.ts";
|
|
|
|
type TestQueryResources = Readonly<{
|
|
client: QueryClient;
|
|
coordinator: QueryInvalidationCoordinator;
|
|
mutationIntentFactory: MutationIntentFactory;
|
|
scopeRuntime: ServerStateScopeRuntime;
|
|
dispose(): void;
|
|
}>;
|
|
|
|
function createTestQueryResources(): TestQueryResources {
|
|
const client = new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false, staleTime: 0, gcTime: Infinity },
|
|
mutations: { retry: false },
|
|
},
|
|
});
|
|
|
|
const coordinator: QueryInvalidationCoordinator = {
|
|
async invalidate(topics) {
|
|
for (const topic of topics) {
|
|
await client.invalidateQueries({
|
|
queryKey: [topic],
|
|
exact: false,
|
|
refetchType: "active",
|
|
});
|
|
}
|
|
},
|
|
beginMutation() {
|
|
return { release: async () => {} };
|
|
},
|
|
async resetLocal() {
|
|
await client.cancelQueries();
|
|
client.clear();
|
|
},
|
|
dispose() {},
|
|
};
|
|
|
|
let identitySequence = 0;
|
|
const lifetime = new AbortController();
|
|
const identities = createRuntimeIdentityRegistry({
|
|
tokenFactory: () =>
|
|
"tech-log-test-identity-" + String(++identitySequence).padStart(4, "0"),
|
|
});
|
|
const scopeSnapshot: CacheScopeSnapshot = Object.freeze({
|
|
generation: 1,
|
|
fingerprint: "tech-log-test-scope-0001",
|
|
identities,
|
|
signal: lifetime.signal,
|
|
isCurrent: () => !lifetime.signal.aborted,
|
|
});
|
|
const scopeRuntime: ServerStateScopeRuntime = Object.freeze({
|
|
getSnapshot: () => scopeSnapshot,
|
|
getPhase: () => "READY",
|
|
subscribe: () => () => {},
|
|
subscribeLifecycle: () => () => {},
|
|
dispose() {
|
|
lifetime.abort();
|
|
identities.close();
|
|
},
|
|
});
|
|
|
|
let sequence = 0;
|
|
const mutationIntentFactory: MutationIntentFactory = Object.freeze({
|
|
create(input) {
|
|
sequence += 1;
|
|
return Object.freeze({
|
|
intentId: `intent-${sequence}`,
|
|
operationId: input.operationId,
|
|
canonicalInputIdentity: input.canonicalInputIdentity,
|
|
...(input.requiresIdempotencyKey
|
|
? { idempotencyKey: `key-${sequence}` }
|
|
: {}),
|
|
createdAtMonotonicMs: sequence,
|
|
});
|
|
},
|
|
});
|
|
|
|
return Object.freeze({
|
|
client,
|
|
coordinator,
|
|
mutationIntentFactory,
|
|
scopeRuntime,
|
|
dispose() {
|
|
scopeRuntime.dispose();
|
|
coordinator.dispose();
|
|
client.clear();
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Test equivalent of the runtime's ServerStateGenerationProvider stack.
|
|
* Components that consume application queries/mutations must be rendered
|
|
* under this wrapper so tests exercise the same provider contract as the app.
|
|
*/
|
|
export function TestQueryProviders({ children }: { children: ReactNode }) {
|
|
const [resources] = useState(createTestQueryResources);
|
|
useEffect(
|
|
() => () => {
|
|
resources.dispose();
|
|
},
|
|
[resources],
|
|
);
|
|
|
|
return (
|
|
<MutationIntentProvider factory={resources.mutationIntentFactory}>
|
|
<QueryClientProvider client={resources.client}>
|
|
<ServerStateScopeProvider runtime={resources.scopeRuntime}>
|
|
<QueryInvalidationProvider coordinator={resources.coordinator}>
|
|
{children}
|
|
</QueryInvalidationProvider>
|
|
</ServerStateScopeProvider>
|
|
</QueryClientProvider>
|
|
</MutationIntentProvider>
|
|
);
|
|
}
|
|
|
|
export function renderWithQueryProviders(children: ReactNode): ReactNode {
|
|
return <TestQueryProviders>{children}</TestQueryProviders>;
|
|
}
|