97 lines
3.6 KiB
TypeScript
97 lines
3.6 KiB
TypeScript
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<void>((resolve) => server.close(() => resolve()));
|
|
});
|
|
|
|
test("GET / 返回服务概览与 CORS 标头", 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("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("<html");
|
|
expect(html).toContain("api-reference");
|
|
expect(html).toContain("data-url=\"/openapi.json\"");
|
|
});
|
|
|
|
test("POST /api/cli 允许同进程执行 CLI 指令", async () => {
|
|
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("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 处理 CORS 跨域预检", 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");
|
|
expect(res.headers.get("access-control-allow-origin")).toBe("*");
|
|
});
|
|
});
|