import { spawn } from "node:child_process"; const BASE = "http://127.0.0.1:18787"; function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); } async function waitForHealth(retries = 30) { for (let i = 0; i < retries; i++) { try { const r = await fetch(`${BASE}/health`); if (r.ok) return true; } catch {} await sleep(200); } return false; } function assert(cond, msg) { if (!cond) throw new Error(msg); } async function run() { const token = "test-token"; const child = spawn("node", ["no-reply-api/server.mjs"], { cwd: process.cwd(), env: { ...process.env, PORT: "18787", AUTH_TOKEN: token, NO_REPLY_MODEL: "wg-test-model" }, stdio: ["ignore", "pipe", "pipe"], }); child.stdout.on("data", () => {}); child.stderr.on("data", () => {}); try { const ok = await waitForHealth(); assert(ok, "health check failed"); const unauth = await fetch(`${BASE}/v1/models`); assert(unauth.status === 401, `expected 401, got ${unauth.status}`); const models = await fetch(`${BASE}/v1/models`, { headers: { Authorization: `Bearer ${token}` }, }); assert(models.ok, "authorized /v1/models failed"); const modelsJson = await models.json(); assert(modelsJson?.data?.[0]?.id === "wg-test-model", "model id mismatch"); const cc = await fetch(`${BASE}/v1/chat/completions`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ model: "wg-test-model", messages: [{ role: "user", content: "hi" }] }), }); assert(cc.ok, "chat completions failed"); const ccJson = await cc.json(); assert(ccJson?.choices?.[0]?.message?.content === "NO_REPLY", "chat completion not NO_REPLY"); const rsp = await fetch(`${BASE}/v1/responses`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ model: "wg-test-model", input: "hi" }), }); assert(rsp.ok, "responses failed"); const rspJson = await rsp.json(); assert(rspJson?.output?.[0]?.content?.[0]?.text === "NO_REPLY", "responses not NO_REPLY"); console.log("test-no-reply-api: ok"); } finally { child.kill("SIGTERM"); } } run().catch((err) => { console.error(`test-no-reply-api: fail: ${err.message}`); process.exit(1); });