|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import type { Server } from "node:http"; |
| 3 | +import { |
| 4 | + buildHealthBody, |
| 5 | + createAmsHealthHandler, |
| 6 | + readiness, |
| 7 | + startAmsHealthServer, |
| 8 | +} from "../../packages/loopover-miner/lib/ams-health-server.js"; |
| 9 | + |
| 10 | +const servers: Server[] = []; |
| 11 | +afterEach(() => { |
| 12 | + for (const server of servers.splice(0)) server.close(); |
| 13 | +}); |
| 14 | + |
| 15 | +type CapturedRes = { |
| 16 | + out: { status: number; headers: Record<string, string>; body: string }; |
| 17 | + writeHead(status: number, headers: Record<string, string>): void; |
| 18 | + end(body: string): void; |
| 19 | +}; |
| 20 | + |
| 21 | +function mockRes(): CapturedRes { |
| 22 | + const out = { status: 0, headers: {} as Record<string, string>, body: "" }; |
| 23 | + return { |
| 24 | + out, |
| 25 | + writeHead(status, headers) { |
| 26 | + out.status = status; |
| 27 | + out.headers = headers; |
| 28 | + }, |
| 29 | + end(body) { |
| 30 | + out.body = body; |
| 31 | + }, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +const passProbe = (name: string) => ({ name, check: async () => true }); |
| 36 | +const failProbe = (name: string) => ({ name, check: async () => false }); |
| 37 | +const throwProbe = (name: string) => ({ |
| 38 | + name, |
| 39 | + check: async () => { |
| 40 | + throw new Error("backend unreachable"); |
| 41 | + }, |
| 42 | +}); |
| 43 | + |
| 44 | +describe("ams-health-server buildHealthBody / readiness (#7177)", () => { |
| 45 | + it("liveness body is a bare { status: 'ok' }", () => { |
| 46 | + expect(buildHealthBody()).toEqual({ status: "ok" }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("readiness with no probes is ok with empty check maps", async () => { |
| 50 | + expect(await readiness()).toEqual({ ok: true, checks: {}, durationsMs: {} }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("readiness is ok only when every probe passes, and times each one", async () => { |
| 54 | + const result = await readiness([passProbe("store"), passProbe("loop")]); |
| 55 | + expect(result.ok).toBe(true); |
| 56 | + expect(result.checks).toEqual({ store: true, loop: true }); |
| 57 | + expect(typeof result.durationsMs.store).toBe("number"); |
| 58 | + expect(result.durationsMs.loop).toBeGreaterThanOrEqual(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it("readiness fails when any probe returns false or throws (never crashing)", async () => { |
| 62 | + const result = await readiness([passProbe("store"), failProbe("loop"), throwProbe("queue")]); |
| 63 | + expect(result.ok).toBe(false); |
| 64 | + expect(result.checks).toEqual({ store: true, loop: false, queue: false }); |
| 65 | + expect(typeof result.durationsMs.queue).toBe("number"); // duration recorded even for the throwing probe |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe("ams-health-server request routing (#7177)", () => { |
| 70 | + it("GET /health returns 200 liveness", async () => { |
| 71 | + const res = mockRes(); |
| 72 | + await createAmsHealthHandler()({ method: "GET", url: "/health" }, res); |
| 73 | + expect(res.out.status).toBe(200); |
| 74 | + expect(res.out.headers["content-type"]).toBe("application/json"); |
| 75 | + expect(JSON.parse(res.out.body)).toEqual({ status: "ok" }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("GET /ready returns 200 when probes pass, 503 when they don't", async () => { |
| 79 | + const okRes = mockRes(); |
| 80 | + await createAmsHealthHandler([passProbe("store")])({ method: "GET", url: "/ready" }, okRes); |
| 81 | + expect(okRes.out.status).toBe(200); |
| 82 | + expect(JSON.parse(okRes.out.body).ok).toBe(true); |
| 83 | + |
| 84 | + const degradedRes = mockRes(); |
| 85 | + await createAmsHealthHandler([failProbe("store")])({ method: "GET", url: "/ready?verbose=1" }, degradedRes); |
| 86 | + expect(degradedRes.out.status).toBe(503); // query string stripped, still matched /ready |
| 87 | + expect(JSON.parse(degradedRes.out.body).ok).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it("unknown paths, non-GET methods, and missing urls all 404", async () => { |
| 91 | + const handler = createAmsHealthHandler(); |
| 92 | + const unknown = mockRes(); |
| 93 | + await handler({ method: "GET", url: "/metrics" }, unknown); |
| 94 | + expect(unknown.out.status).toBe(404); |
| 95 | + |
| 96 | + const wrongMethod = mockRes(); |
| 97 | + await handler({ method: "POST", url: "/health" }, wrongMethod); |
| 98 | + expect(wrongMethod.out.status).toBe(404); |
| 99 | + |
| 100 | + const noUrl = mockRes(); |
| 101 | + await handler({ method: "GET" }, noUrl); // url undefined -> "" -> 404 |
| 102 | + expect(noUrl.out.status).toBe(404); |
| 103 | + expect(JSON.parse(noUrl.out.body)).toEqual({ error: "not_found" }); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe("ams-health-server listener (#7177)", () => { |
| 108 | + async function base(server: Server) { |
| 109 | + const address = server.address(); |
| 110 | + if (address === null || typeof address === "string") throw new Error("expected a bound TCP address"); |
| 111 | + return `http://127.0.0.1:${address.port}`; |
| 112 | + } |
| 113 | + |
| 114 | + it("serves /health, /ready, and 404s over a real socket on an ephemeral port", async () => { |
| 115 | + const server = await startAmsHealthServer(); // all defaults: port 0, host 0.0.0.0, no probes |
| 116 | + servers.push(server); |
| 117 | + const url = await base(server); |
| 118 | + |
| 119 | + const health = await fetch(`${url}/health`); |
| 120 | + expect(health.status).toBe(200); |
| 121 | + expect(await health.json()).toEqual({ status: "ok" }); |
| 122 | + |
| 123 | + const ready = await fetch(`${url}/ready`); |
| 124 | + expect(ready.status).toBe(200); |
| 125 | + |
| 126 | + const missing = await fetch(`${url}/nope`); |
| 127 | + expect(missing.status).toBe(404); |
| 128 | + }); |
| 129 | + |
| 130 | + it("reports 503 over the socket when a wired probe fails", async () => { |
| 131 | + const server = await startAmsHealthServer({ port: 0, host: "127.0.0.1", probes: [failProbe("store")] }); |
| 132 | + servers.push(server); |
| 133 | + const ready = await fetch(`${await base(server)}/ready`); |
| 134 | + expect(ready.status).toBe(503); |
| 135 | + const body = (await ready.json()) as { checks: Record<string, boolean> }; |
| 136 | + expect(body.checks).toEqual({ store: false }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments