Files
hermes-control-panel/test/server-error-handling.test.cjs
T

67 lines
2.0 KiB
JavaScript

"use strict"
const { test } = require("node:test")
const assert = require("node:assert/strict")
const fs = require("fs")
const http = require("http")
const os = require("os")
const path = require("path")
const { spawn } = require("child_process")
const root = path.join(__dirname, "..")
const serverPath = path.join(root, "server.cjs")
function request(port, requestPath) {
return new Promise((resolve, reject) => {
http.get({ hostname: "127.0.0.1", port, path: requestPath }, (res) => {
let body = ""
res.on("data", (chunk) => { body += chunk })
res.on("end", () => resolve({ status: res.statusCode, body }))
}).on("error", reject)
})
}
async function waitForHealth(port, proc) {
const deadline = Date.now() + 5000
while (Date.now() < deadline) {
if (proc.exitCode !== null) throw new Error(`server exited with ${proc.exitCode}`)
try {
const response = await request(port, "/health")
if (response.status === 200) return
} catch {}
await new Promise((resolve) => setTimeout(resolve, 100))
}
throw new Error("server did not become ready")
}
test("async route errors return 500 without crashing the server", { timeout: 10000 }, async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "hermes-route-error-test-"))
const port = 19745
const proc = spawn(process.execPath, [serverPath], {
cwd: root,
env: {
...process.env,
HOME: tmp,
HERMES_HOME: path.join(tmp, ".hermes"),
HERMES_EXE: path.join(tmp, "missing-hermes"),
HERMES_SETUP_UI_HOST: "127.0.0.1",
HERMES_SETUP_UI_PORT: String(port),
DATABASE_URL: "",
},
stdio: ["ignore", "pipe", "pipe"],
})
try {
await waitForHealth(port, proc)
const status = await request(port, "/api/status")
assert.equal(status.status, 500)
assert.equal(proc.exitCode, null)
const health = await request(port, "/health")
assert.equal(health.status, 200)
} finally {
proc.kill()
fs.rmSync(tmp, { recursive: true, force: true })
}
})