/** * The hook over the single `/api/overview` request. * * The five-endpoint build reconciled five window identities here — the retry per * mismatch episode, the terminal "different window" error, the orphaned stale * completion. One request cannot disagree with itself, so those behaviours have * no subject left and are gone rather than ported. What survived the collapse is * pinned below: the three states, and the one rule a single request still does * not settle — that a `keepPreviousData` body from the period the reader left * must never render under the new period's label. */ import { render, screen, waitFor } from "@testing-library/react"; import { QueryClientProvider } from "@tanstack/react-query"; import { createQueryClient } from "@/lib/queryClient"; import type { Period } from "@/lib/types"; import { useOverviewWindow } from "./overviewWindow"; const SINCE = Date.UTC(2026, 0, 1, 0, 0) / 1000; const UNTIL = Date.UTC(2026, 0, 2, 0, 0) / 1000; let failing: boolean; let calls: number; function body(period: Period): unknown { return { period, since: SINCE, until: UNTIL, bucket_seconds: 1800, totals: { queries: 10, blocked: 2, clients: 1, avg_response_time_us: 1000 }, buckets: [], clients: [], other: [], types: [], routes: [], coverage: { complete: true, available_since: SINCE }, }; } function json(payload: unknown, status = 200): Response { return new Response(JSON.stringify(payload), { status, headers: { "content-type": "application/json" } }); } beforeEach(() => { failing = false; calls = 0; vi.stubGlobal( "fetch", vi.fn(async (input: RequestInfo | URL) => { const url = String(input); if (!url.startsWith("/api/overview")) return json({ error: "not stubbed" }, 404); calls += 1; if (failing) return json({ error: "endpoint unavailable" }, 400); const period = (new URLSearchParams(url.split("?")[1]).get("period") ?? "24h") as Period; return json(body(period)); }), ); }); afterEach(() => vi.unstubAllGlobals()); /** The last retry the hook handed out, so a test can spend it. */ let lastRetry: () => void; function Probe({ period }: { period: Period }) { const panel = useOverviewWindow(period); if (panel.status === "error") lastRetry = panel.retry; const detail = panel.status === "ready" ? `${panel.data.period}@${panel.data.until}` : panel.status === "error" ? (panel.error as Error).message : ""; return
{`${panel.status}:${detail}`}
; } function renderProbe(period: Period = "24h") { const client = createQueryClient(); const view = render(