import { describe, expect, test, afterAll, beforeAll } from "bun:test"; import type { Server } from "node:http"; import { startHttpServer } from "./server.js"; import { VERSION } from "../version.js"; describe("HTTP Server - 嵌入式服务与 REST 网关", () => { let server: Server; const testPort = 18888; 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 / 返回服务概览与本地 CORS 标头", async () => { const res = await fetch(`${baseUrl}/`, { headers: { Origin: "http://localhost:3000" }, }); expect(res.status).toBe(200); expect(res.headers.get("access-control-allow-origin")).toBe("http://localhost:3000"); const json = (await res.json()) as any; expect(json.name).toBe("hose-use"); expect(json.version).toBe(VERSION); expect(json.docs).toBe("/docs"); expect(json.openapi).toBe("/openapi.json"); expect(json.health).toBe("/health"); expect(json.endpoints.cli).toBe("POST /api/cli"); }); test("GET /health 返回存活探针状态", 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("hose-use"); expect(json.version).toBe(VERSION); expect(json.timestamp).toBeDefined(); }); test("GET /openapi.json 返回标准包装层 OpenAPI 规范", 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).toContain("hose-use"); }); test("GET /raw-openapi.json 返回原生下游 OpenAPI 规范", async () => { const res = await fetch(`${baseUrl}/raw-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).toContain("Downstream"); }); test("GET /docs 返回 Scalar 交互式文档 HTML 页面", 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.stdout).toContain(`hose-use v${VERSION}`); }); test("POST /api/cli 拒绝来自非授信外部 Origin 的跨站命令执行请求 (防 CSRF RCE)", async () => { const res = await fetch(`${baseUrl}/api/cli`, { method: "POST", headers: { "Content-Type": "application/json", Origin: "https://evil.example.com", }, body: JSON.stringify({ command: "version" }), }); expect(res.status).toBe(403); expect(res.headers.get("access-control-allow-origin")).toBeNull(); }); test("GET /v1/corps 返回企业列表与运行模式", 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(json).toHaveProperty("mode"); expect(Array.isArray(json.corps)).toBe(true); }); test("OPTIONS /v1/corps 允许本地 Origin 预检并拦截未知外部 Origin", async () => { const res = await fetch(`${baseUrl}/v1/corps`, { method: "OPTIONS", headers: { Origin: "http://127.0.0.1:8888" }, }); expect(res.status).toBe(204); expect(res.headers.get("access-control-allow-methods")).toContain("GET"); expect(res.headers.get("access-control-allow-origin")).toBe("http://127.0.0.1:8888"); const blocked = await fetch(`${baseUrl}/v1/corps`, { method: "OPTIONS", headers: { Origin: "https://attacker.example.com" }, }); expect(blocked.status).toBe(403); }); });