80 lines
2.8 KiB
JavaScript
80 lines
2.8 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import http from "node:http";
|
|
import { spawn } from "node:child_process";
|
|
|
|
test("Healthcheck: test/healthcheck.js script exists and parses cleanly", () => {
|
|
const scriptPath = path.resolve(process.cwd(), "test/healthcheck.js");
|
|
assert.ok(fs.existsSync(scriptPath), "test/healthcheck.js must exist");
|
|
|
|
const content = fs.readFileSync(scriptPath, "utf-8");
|
|
assert.match(content, /127\.0\.0\.1/, "Must target IPv4 loopback 127.0.0.1 to avoid Alpine IPv6 lookup failures");
|
|
assert.match(content, /\/healthz/, "Must target the /healthz endpoint");
|
|
});
|
|
|
|
test("Healthcheck: test/healthcheck.js succeeds when server returns 200 OK", async () => {
|
|
const scriptPath = path.resolve(process.cwd(), "test/healthcheck.js");
|
|
|
|
// Start dummy HTTP server responding 200 on /healthz
|
|
const server = http.createServer((req, res) => {
|
|
if (req.url === "/healthz") {
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
res.end(JSON.stringify({ status: "ok" }));
|
|
} else {
|
|
res.writeHead(404);
|
|
res.end();
|
|
}
|
|
});
|
|
|
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
const port = server.address().port;
|
|
|
|
try {
|
|
const exitCode = await new Promise((resolve) => {
|
|
const child = spawn(process.execPath, [scriptPath], {
|
|
env: { ...process.env, PORT: String(port) },
|
|
});
|
|
child.on("close", resolve);
|
|
});
|
|
|
|
assert.strictEqual(exitCode, 0, "healthcheck.js must exit with code 0 on healthy endpoint");
|
|
} finally {
|
|
await new Promise((resolve) => server.close(resolve));
|
|
}
|
|
});
|
|
|
|
test("Healthcheck: test/healthcheck.js fails when connection is refused or server is 500", async () => {
|
|
const scriptPath = path.resolve(process.cwd(), "test/healthcheck.js");
|
|
|
|
// Pick an unused high port
|
|
const exitCodeDown = await new Promise((resolve) => {
|
|
const child = spawn(process.execPath, [scriptPath], {
|
|
env: { ...process.env, PORT: "59999" },
|
|
});
|
|
child.on("close", resolve);
|
|
});
|
|
assert.strictEqual(exitCodeDown, 1, "healthcheck.js must exit with code 1 when server is unreachable");
|
|
|
|
// Test 500 status code
|
|
const server = http.createServer((req, res) => {
|
|
res.writeHead(500);
|
|
res.end();
|
|
});
|
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
const port = server.address().port;
|
|
|
|
try {
|
|
const exitCode500 = await new Promise((resolve) => {
|
|
const child = spawn(process.execPath, [scriptPath], {
|
|
env: { ...process.env, PORT: String(port) },
|
|
});
|
|
child.on("close", resolve);
|
|
});
|
|
assert.strictEqual(exitCode500, 1, "healthcheck.js must exit with code 1 on HTTP 500");
|
|
} finally {
|
|
await new Promise((resolve) => server.close(resolve));
|
|
}
|
|
});
|