70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { createFetchHandler, type StaticAssets } from "../../src/server/app";
|
|
|
|
const staticAssets: StaticAssets = {
|
|
indexHtml: new Blob(["<!doctype html><title>Gateway Checker Demo</title><div id=\"root\"></div>"], {
|
|
type: "text/html",
|
|
}),
|
|
files: {
|
|
"/assets/app.js": new Blob(["console.log('demo');"], { type: "text/javascript" }),
|
|
},
|
|
};
|
|
|
|
describe("Bun fullstack runtime", () => {
|
|
const fetchHandler = createFetchHandler({ mode: "test", staticAssets });
|
|
|
|
test("/api/demo 返回 JSON demo 响应", async () => {
|
|
const response = await fetchHandler(new Request("http://localhost/api/demo"));
|
|
const body = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("content-type")).toContain("application/json");
|
|
expect(body.message).toContain("/api/demo");
|
|
expect(body.runtime.mode).toBe("test");
|
|
});
|
|
|
|
test("/health 返回机器可读健康检查", async () => {
|
|
const response = await fetchHandler(new Request("http://localhost/health"));
|
|
const body = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(body.ok).toBe(true);
|
|
expect(body.service).toBe("gateway-checker");
|
|
});
|
|
|
|
test("未知 /api/* 路由返回 JSON 404", async () => {
|
|
const response = await fetchHandler(new Request("http://localhost/api/missing"));
|
|
const body = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(response.headers.get("content-type")).toContain("application/json");
|
|
expect(body.status).toBe(404);
|
|
});
|
|
|
|
test("生产根路径返回前端入口", async () => {
|
|
const response = await fetchHandler(new Request("http://localhost/"));
|
|
const body = await response.text();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("content-type")).toContain("text/html");
|
|
expect(body).toContain("Gateway Checker Demo");
|
|
});
|
|
|
|
test("生产静态资源返回正确内容类型", async () => {
|
|
const response = await fetchHandler(new Request("http://localhost/assets/app.js"));
|
|
const body = await response.text();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("content-type")).toContain("text/javascript");
|
|
expect(body).toContain("demo");
|
|
});
|
|
|
|
test("前端路由 fallback 到入口 HTML", async () => {
|
|
const response = await fetchHandler(new Request("http://localhost/dashboard"));
|
|
const body = await response.text();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(body).toContain("Gateway Checker Demo");
|
|
});
|
|
});
|