import { describe, expect, test, afterAll, beforeAll } from "bun:test"; import type { Server } from "node:http"; import { startHttpServer } from "./server.js"; describe("HTTP Server", () => { let server: Server; const testPort = 18787; const baseUrl = `http://127.0.0.1:${testPort}`; beforeAll(async () => { server = await startHttpServer({ port: testPort, host: "127.0.0.1", silent: true }); }); afterAll(async () => { await new Promise((resolve) => server.close(() => resolve())); }); test("GET / returns service overview and CORS headers", async () => { const res = await fetch(`${baseUrl}/`); expect(res.status).toBe(200); expect(res.headers.get("access-control-allow-origin")).toBe("*"); const json = (await res.json()) as any; expect(json.name).toBe("seal-use"); expect(json.docs).toBe("/docs"); expect(json.openapi).toBe("/openapi.json"); expect(json.health).toBe("/health"); }); test("GET /health returns status ok", async () => { const res = await fetch(`${baseUrl}/health`); expect(res.status).toBe(200); const json = (await res.json()) as any; expect(json.status).toBe("ok"); expect(json.name).toBe("seal-use"); }); test("GET /openapi.json returns valid OpenAPI 3.1 schema", async () => { const res = await fetch(`${baseUrl}/openapi.json`); expect(res.status).toBe(200); const json = (await res.json()) as any; expect(json.openapi).toBe("3.1.0"); expect(json.info.title).toBe("seal-use Wrapped API"); }); test("GET /docs returns HTML API reference page", async () => { const res = await fetch(`${baseUrl}/docs`); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toContain("text/html"); const html = await res.text(); expect(html).toContain(" { const res = await fetch(`${baseUrl}/api/cli`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ command: "version" }) }); expect(res.status).toBe(200); const json = (await res.json()) as any; expect(json.success).toBe(true); expect(json.exitCode).toBe(0); expect(json.data.name).toBe("seal-use"); }); test("GET /v1/corps returns enterprise list", async () => { const res = await fetch(`${baseUrl}/v1/corps`); expect(res.status).toBe(200); const json = (await res.json()) as any; expect(json).toHaveProperty("count"); expect(Array.isArray(json.corps)).toBe(true); }); test("OPTIONS / handles CORS preflight", async () => { const res = await fetch(`${baseUrl}/v1/corps`, { method: "OPTIONS" }); expect(res.status).toBe(204); expect(res.headers.get("access-control-allow-methods")).toContain("GET"); }); });