type St0reTestEnv = Readonly>; export interface St0reRealinfraProbe { target?: string; reachable: boolean; reason: string; } /** Keep the real client's subsequent writes inside the selected test origin too. */ export function isolatedSt0reFetch(target: string, fetchImpl: typeof fetch): typeof fetch { const origin = resolveSt0reRealinfraTarget({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target }); return async (input, init) => { const url = new URL(typeof input === "string" ? input : input instanceof URL ? input.href : input.url); if (url.origin !== origin) throw new Error("ST0RE realinfra request escaped its isolated test origin"); return fetchImpl(input, { ...init, redirect: "error" }); }; } /** Only an explicit, isolated loopback origin can enable real-infrastructure tests. */ export function resolveSt0reRealinfraTarget(env: St0reTestEnv): string | undefined { if (env.ST0RE_REALINFRA_TESTS !== "1") return undefined; const target = env.ST0RE_TEST_URL; const configError = () => new Error( "ST0RE realinfra configuration error: ST0RE_REALINFRA_TESTS=1 requires ST0RE_TEST_URL to be an HTTP(S) loopback origin (localhost, 127.0.0.1 or [::1]), without credentials, path, query or fragment. Remote/production targets are forbidden." ); // Check the original spelling too: URL parsing normalizes paths and IP aliases. if (!target || !/^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(?::[0-9]+)?\/?$/i.test(target)) { throw configError(); } try { return new URL(target).origin; } catch { throw configError(); } } export async function probeSt0reRealinfra( env: St0reTestEnv, fetchImpl: typeof fetch, ): Promise { // Configuration failures must escape the reachability catch and fail closed. const target = resolveSt0reRealinfraTarget(env); if (!target) { return { reachable: false, reason: "disabled: ST0RE_REALINFRA_TESTS is not 1" }; } try { const response = await fetchImpl(`${target}/v1/st0re/lakehouse/namespaces`, { method: "GET", redirect: "error", headers: { "X-Tenant-Id": env.ST0RE_TEST_TENANT_ID ?? "t_0pt_realinfra_local", "X-Actor-Id": "0pt-realinfra-test", }, signal: AbortSignal.timeout(5000), }); return { target, reachable: response.ok, reason: response.ok ? "" : `ST0RE responded HTTP ${response.status}`, }; } catch { return { target, reachable: false, reason: "Local ST0RE reachability probe failed" }; } }