|
| 1 | +// Gateway health route tests cover the machine-readable fast path and its error contract. |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { runGatewayHealthJsonRoute } from "./health-route.js"; |
| 4 | + |
| 5 | +function createRuntime() { |
| 6 | + return { |
| 7 | + writeJson: vi.fn(), |
| 8 | + exit: vi.fn(), |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +describe("runGatewayHealthJsonRoute", () => { |
| 13 | + it("writes successful JSON without loading error-only dependencies", async () => { |
| 14 | + const runtime = createRuntime(); |
| 15 | + const callGateway = vi.fn(async () => ({ ok: true, durationMs: 6 })); |
| 16 | + const readBestEffortConfig = vi.fn(async () => ({})); |
| 17 | + const emitReachableGatewayAuthDiagnostic = vi.fn(async () => false); |
| 18 | + const formatGatewayClientRequestErrorJson = vi.fn(); |
| 19 | + const formatGatewayTransportErrorJson = vi.fn(); |
| 20 | + |
| 21 | + await runGatewayHealthJsonRoute( |
| 22 | + { |
| 23 | + rpc: { json: true, timeout: "10000" }, |
| 24 | + }, |
| 25 | + runtime as never, |
| 26 | + { |
| 27 | + callGateway, |
| 28 | + readBestEffortConfig, |
| 29 | + emitReachableGatewayAuthDiagnostic: emitReachableGatewayAuthDiagnostic as never, |
| 30 | + formatGatewayClientRequestErrorJson: formatGatewayClientRequestErrorJson as never, |
| 31 | + formatGatewayTransportErrorJson: formatGatewayTransportErrorJson as never, |
| 32 | + }, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(callGateway).toHaveBeenCalledWith("health", { json: true, timeout: "10000" }); |
| 36 | + expect(runtime.writeJson).toHaveBeenCalledWith({ ok: true, durationMs: 6 }); |
| 37 | + expect(readBestEffortConfig).not.toHaveBeenCalled(); |
| 38 | + expect(emitReachableGatewayAuthDiagnostic).not.toHaveBeenCalled(); |
| 39 | + expect(formatGatewayClientRequestErrorJson).not.toHaveBeenCalled(); |
| 40 | + expect(formatGatewayTransportErrorJson).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("projects a local port into the routed config", async () => { |
| 44 | + const runtime = createRuntime(); |
| 45 | + const callGateway = vi.fn(async () => ({ ok: true })); |
| 46 | + const readBestEffortConfig = vi.fn(async () => ({ |
| 47 | + gateway: { auth: { mode: "token" as const } }, |
| 48 | + })); |
| 49 | + |
| 50 | + await runGatewayHealthJsonRoute( |
| 51 | + { |
| 52 | + rpc: { json: true, timeout: "10000" }, |
| 53 | + localPortOverride: 19083, |
| 54 | + }, |
| 55 | + runtime as never, |
| 56 | + { callGateway, readBestEffortConfig }, |
| 57 | + ); |
| 58 | + |
| 59 | + expect(callGateway).toHaveBeenCalledWith( |
| 60 | + "health", |
| 61 | + expect.objectContaining({ |
| 62 | + localPortOverride: 19083, |
| 63 | + config: { |
| 64 | + gateway: { auth: { mode: "token" }, mode: "local", port: 19083 }, |
| 65 | + }, |
| 66 | + }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it("preserves structured transport errors", async () => { |
| 71 | + const runtime = createRuntime(); |
| 72 | + const error = new Error("gateway unavailable"); |
| 73 | + const callGateway = vi.fn(async () => { |
| 74 | + throw error; |
| 75 | + }); |
| 76 | + const payload = { ok: false, error: { type: "gateway_transport_error" } }; |
| 77 | + |
| 78 | + await runGatewayHealthJsonRoute({ rpc: { json: true, timeout: "10000" } }, runtime as never, { |
| 79 | + callGateway, |
| 80 | + readBestEffortConfig: async () => ({}), |
| 81 | + emitReachableGatewayAuthDiagnostic: vi.fn(async () => false) as never, |
| 82 | + formatGatewayClientRequestErrorJson: vi.fn(() => null) as never, |
| 83 | + formatGatewayTransportErrorJson: vi.fn(() => payload) as never, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(runtime.writeJson).toHaveBeenCalledWith(payload); |
| 87 | + expect(runtime.exit).toHaveBeenCalledWith(1); |
| 88 | + }); |
| 89 | +}); |
0 commit comments