import { cookies } from "next/headers"; // A TEST SERVER'S WAY TO HOLD A STREAMED SECTION BACK. // // The pages that stream (the dashboard, /jobs, /settings, /storage, /channels) // send their shell first and each slow section when its read is done. Against // the e2e fixture every read is over in milliseconds, so a spec could never // see a skeleton, measure its box against the box of what replaces it, or // prove that a refresh does not bring it back. A spec sets this cookie to a // number of milliseconds and every streamed section waits that long before it // reads. // // INERT OUTSIDE A TEST SERVER: without E2E_TEST_ROUTES=1 (the test server's // identity, api/test/_guard.ts) the cookie is not even read. // // A SECOND COOKIE MAKES ONE SECTION FAIL: `e2e-stream-fail=` throws in // the section of that name, so a spec can see that a section's failure stays // in its own place (StreamErrorBoundary) and the rest of the page stands. export const STREAM_DELAY_COOKIE = "e2e-stream-delay"; export const STREAM_FAIL_COOKIE = "e2e-stream-fail"; const MAX_DELAY_MS = 5_000; // `name` is the section's (its skeleton's `data-stream-skeleton`). export async function e2eStreamDelay(name?: string): Promise { if (process.env.E2E_TEST_ROUTES !== "1") return; const jar = await cookies(); const ms = Math.min(Number(jar.get(STREAM_DELAY_COOKIE)?.value) || 0, MAX_DELAY_MS); if (ms > 0) await new Promise((resolve) => setTimeout(resolve, ms)); if (name && jar.get(STREAM_FAIL_COOKIE)?.value === name) { throw new Error(`e2e: the ${name} section was told to fail`); } }