|
| 1 | +// QA Lab owns the real child/provider fixture used by the managed service proof. |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { startQaGatewayChild } from "./gateway-child.js"; |
| 5 | +import { startQaProviderServer } from "./providers/server-runtime.js"; |
| 6 | + |
| 7 | +const repoRoot = fileURLToPath(new URL("../../../", import.meta.url)); |
| 8 | + |
| 9 | +async function fetchJson(url: string, label: string): Promise<Record<string, unknown>> { |
| 10 | + const response = await fetch(url); |
| 11 | + if (!response.ok) { |
| 12 | + throw new Error(`${label} returned HTTP ${response.status}`); |
| 13 | + } |
| 14 | + const body: unknown = await response.json(); |
| 15 | + if (!body || typeof body !== "object" || Array.isArray(body)) { |
| 16 | + throw new Error(`${label} did not return a JSON object`); |
| 17 | + } |
| 18 | + return body as Record<string, unknown>; |
| 19 | +} |
| 20 | + |
| 21 | +describe("managed gateway service lifecycle product proof", () => { |
| 22 | + it( |
| 23 | + "runs a foreground gateway with live, ready, and RPC health", |
| 24 | + { timeout: 180_000 }, |
| 25 | + async () => { |
| 26 | + const provider = await startQaProviderServer("mock-openai"); |
| 27 | + if (!provider) { |
| 28 | + throw new Error("mock OpenAI provider did not start"); |
| 29 | + } |
| 30 | + let gateway: Awaited<ReturnType<typeof startQaGatewayChild>> | undefined; |
| 31 | + let proofFailure: Error | undefined; |
| 32 | + try { |
| 33 | + gateway = await startQaGatewayChild({ |
| 34 | + repoRoot, |
| 35 | + useRepoCli: true, |
| 36 | + providerBaseUrl: `${provider.baseUrl}/v1`, |
| 37 | + providerMode: "mock-openai", |
| 38 | + transportBaseUrl: "http://127.0.0.1", |
| 39 | + controlUiEnabled: false, |
| 40 | + }); |
| 41 | + expect(gateway.pid).toBeGreaterThan(0); |
| 42 | + await expect( |
| 43 | + fetchJson(`${gateway.baseUrl}/healthz`, "gateway /healthz"), |
| 44 | + ).resolves.toMatchObject({ status: "live" }); |
| 45 | + await expect( |
| 46 | + fetchJson(`${gateway.baseUrl}/readyz`, "gateway /readyz"), |
| 47 | + ).resolves.toMatchObject({ ready: true }); |
| 48 | + const rpcHealth = await gateway.call("health", {}); |
| 49 | + expect(rpcHealth).not.toBeNull(); |
| 50 | + expect(Array.isArray(rpcHealth)).toBe(false); |
| 51 | + expect(typeof rpcHealth).toBe("object"); |
| 52 | + } catch (error) { |
| 53 | + proofFailure = error instanceof Error ? error : new Error(String(error)); |
| 54 | + } |
| 55 | + const cleanup = await Promise.allSettled([gateway?.stop(), provider.stop()]); |
| 56 | + const cleanupFailures = cleanup.flatMap((result) => |
| 57 | + result.status === "rejected" ? [result.reason] : [], |
| 58 | + ); |
| 59 | + if (proofFailure && cleanupFailures.length > 0) { |
| 60 | + throw new AggregateError( |
| 61 | + [proofFailure, ...cleanupFailures], |
| 62 | + "foreground gateway proof and cleanup failed", |
| 63 | + ); |
| 64 | + } |
| 65 | + if (proofFailure) { |
| 66 | + throw proofFailure; |
| 67 | + } |
| 68 | + if (cleanupFailures.length > 0) { |
| 69 | + throw new AggregateError(cleanupFailures, "foreground gateway cleanup failed"); |
| 70 | + } |
| 71 | + }, |
| 72 | + ); |
| 73 | +}); |
0 commit comments