Initial commit
This commit is contained in:
71
tests/server/bootstrap.test.ts
Normal file
71
tests/server/bootstrap.test.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/* eslint-disable @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars, @typescript-eslint/require-await, @typescript-eslint/unbound-method */
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import type { StartServerOptions } from "../../src/server/server";
|
||||
|
||||
import { bootstrap, type BootstrapDependencies } from "../../src/server/bootstrap";
|
||||
|
||||
const origExit = process.exit;
|
||||
|
||||
describe("bootstrap", () => {
|
||||
test("使用默认依赖启动", async () => {
|
||||
let started = false;
|
||||
let signalRegistered = false;
|
||||
|
||||
const mockLoadConfig = (async () => ({
|
||||
host: "127.0.0.1",
|
||||
port: 0,
|
||||
})) as unknown as BootstrapDependencies["loadConfig"];
|
||||
const mockLogError = () => {};
|
||||
const mockOnSignal = (_signal: string, _handler: () => void) => {
|
||||
signalRegistered = true;
|
||||
};
|
||||
const mockStartServer = (_options: StartServerOptions) => {
|
||||
started = true;
|
||||
return {};
|
||||
};
|
||||
|
||||
const deps: BootstrapDependencies = {
|
||||
loadConfig: mockLoadConfig,
|
||||
logError: mockLogError,
|
||||
onSignal: mockOnSignal,
|
||||
startServer: mockStartServer,
|
||||
};
|
||||
|
||||
await bootstrap({ mode: "production" }, deps);
|
||||
|
||||
expect(started).toBe(true);
|
||||
expect(signalRegistered).toBe(true);
|
||||
});
|
||||
|
||||
test("启动失败时调用 logError", async () => {
|
||||
let errorLogged = false;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
||||
process.exit = ((code?: number) => {
|
||||
throw new Error("process.exit called");
|
||||
}) as unknown as typeof process.exit;
|
||||
|
||||
const deps: BootstrapDependencies = {
|
||||
loadConfig: async () => {
|
||||
throw new Error("test config error");
|
||||
},
|
||||
logError: () => {
|
||||
errorLogged = true;
|
||||
},
|
||||
startServer: () => {
|
||||
throw new Error("should not reach");
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
await bootstrap({ mode: "production" }, deps);
|
||||
} catch {
|
||||
// process.exit throws to interrupt flow
|
||||
}
|
||||
|
||||
process.exit = origExit;
|
||||
|
||||
expect(errorLogged).toBe(true);
|
||||
});
|
||||
});
|
||||
95
tests/server/config.test.ts
Normal file
95
tests/server/config.test.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { loadServerConfig, parseRuntimeArgs } from "../../src/server/config";
|
||||
|
||||
describe("parseRuntimeArgs", () => {
|
||||
test("无参数返回空对象", () => {
|
||||
const result = parseRuntimeArgs([]);
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
test("有参数返回 configPath", () => {
|
||||
const result = parseRuntimeArgs(["config.yaml"]);
|
||||
expect(result).toEqual({ configPath: "config.yaml" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadServerConfig", () => {
|
||||
test("无 configPath 使用默认值", async () => {
|
||||
const config = await loadServerConfig();
|
||||
expect(config.host).toBe("127.0.0.1");
|
||||
expect(config.port).toBe(3000);
|
||||
});
|
||||
|
||||
test("环境变量 HOST 覆盖默认值", async () => {
|
||||
const prev = process.env["HOST"];
|
||||
process.env["HOST"] = "0.0.0.0";
|
||||
try {
|
||||
const config = await loadServerConfig();
|
||||
expect(config.host).toBe("0.0.0.0");
|
||||
} finally {
|
||||
if (prev === undefined) {
|
||||
delete process.env["HOST"];
|
||||
} else {
|
||||
process.env["HOST"] = prev;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("环境变量 PORT 覆盖默认值", async () => {
|
||||
const prev = process.env["PORT"];
|
||||
process.env["PORT"] = "8080";
|
||||
try {
|
||||
const config = await loadServerConfig();
|
||||
expect(config.port).toBe(8080);
|
||||
} finally {
|
||||
if (prev === undefined) {
|
||||
delete process.env["PORT"];
|
||||
} else {
|
||||
process.env["PORT"] = prev;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("YAML 配置文件不存在时报错", async () => {
|
||||
try {
|
||||
await loadServerConfig("/nonexistent/path/config.yaml");
|
||||
expect.unreachable();
|
||||
} catch (error) {
|
||||
expect((error as Error).message).toContain("配置文件不存在");
|
||||
}
|
||||
});
|
||||
|
||||
test("YAML 配置文件加载 server 配置", async () => {
|
||||
const temp = tmpdir();
|
||||
const yamlPath = join(temp, "test-config.yaml");
|
||||
const yamlContent = 'server:\n host: "0.0.0.0"\n port: 9999\n';
|
||||
await writeFile(yamlPath, yamlContent);
|
||||
|
||||
try {
|
||||
const config = await loadServerConfig(yamlPath);
|
||||
expect(config.host).toBe("0.0.0.0");
|
||||
expect(config.port).toBe(9999);
|
||||
} finally {
|
||||
await rm(yamlPath, { force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("YAML 缺少 server 字段时使用默认值", async () => {
|
||||
const temp = tmpdir();
|
||||
const yamlPath = join(temp, "test-empty.yaml");
|
||||
const yamlContent = "runtime:\n debug: true\n";
|
||||
await writeFile(yamlPath, yamlContent);
|
||||
|
||||
try {
|
||||
const config = await loadServerConfig(yamlPath);
|
||||
expect(config.host).toBe("127.0.0.1");
|
||||
expect(config.port).toBe(3000);
|
||||
} finally {
|
||||
await rm(yamlPath, { force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
97
tests/server/middleware.test.ts
Normal file
97
tests/server/middleware.test.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { validateIdParam, validatePagination, validateTimeRange } from "../../src/server/middleware";
|
||||
|
||||
describe("validateIdParam", () => {
|
||||
test("有效的 ID 返回字符串", () => {
|
||||
const result = validateIdParam("api-health_01", "production");
|
||||
expect(result).not.toHaveProperty("status");
|
||||
expect((result as { id: string }).id).toBe("api-health_01");
|
||||
});
|
||||
|
||||
test("无效的 ID 返回 400", () => {
|
||||
const invalid = ["-1", "_abc", "has space", "1.5", ""];
|
||||
|
||||
for (const id of invalid) {
|
||||
const result = validateIdParam(id, "production");
|
||||
expect(result).toHaveProperty("status", 400);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateTimeRange", () => {
|
||||
test("有效的 from/to 返回 ISO 字符串", () => {
|
||||
const result = validateTimeRange("2024-01-01T00:00:00.000Z", "2024-01-02T00:00:00.000Z", "production");
|
||||
expect(result).not.toHaveProperty("status");
|
||||
expect((result as { from: string; to: string }).from).toBe("2024-01-01T00:00:00.000Z");
|
||||
expect((result as { from: string; to: string }).to).toBe("2024-01-02T00:00:00.000Z");
|
||||
});
|
||||
|
||||
test("缺失 from 或 to 返回 400", () => {
|
||||
const missingFrom = validateTimeRange(null, "2024-01-02T00:00:00.000Z", "production");
|
||||
const missingTo = validateTimeRange("2024-01-01T00:00:00.000Z", null, "production");
|
||||
const missingBoth = validateTimeRange(null, null, "production");
|
||||
|
||||
expect(missingFrom).toHaveProperty("status", 400);
|
||||
expect(missingTo).toHaveProperty("status", 400);
|
||||
expect(missingBoth).toHaveProperty("status", 400);
|
||||
});
|
||||
|
||||
test("空字符串 from 或 to 返回 400", () => {
|
||||
const emptyFrom = validateTimeRange("", "2024-01-02T00:00:00.000Z", "production");
|
||||
const emptyTo = validateTimeRange("2024-01-01T00:00:00.000Z", "", "production");
|
||||
|
||||
expect(emptyFrom).toHaveProperty("status", 400);
|
||||
expect(emptyTo).toHaveProperty("status", 400);
|
||||
});
|
||||
|
||||
test("无效的日期格式返回 400", () => {
|
||||
const result = validateTimeRange("invalid-date", "2024-01-02T00:00:00.000Z", "production");
|
||||
expect(result).toHaveProperty("status", 400);
|
||||
});
|
||||
|
||||
test("from 晚于 to 返回 400", () => {
|
||||
const result = validateTimeRange("2024-01-02T00:00:00.000Z", "2024-01-01T00:00:00.000Z", "production");
|
||||
expect(result).toHaveProperty("status", 400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validatePagination", () => {
|
||||
test("默认值:page=1, pageSize=20", () => {
|
||||
const result = validatePagination(null, null, "production");
|
||||
expect(result).toEqual({ page: 1, pageSize: 20 });
|
||||
});
|
||||
|
||||
test("有效的 page 和 pageSize 参数", () => {
|
||||
const result = validatePagination("2", "50", "production");
|
||||
expect(result).toEqual({ page: 2, pageSize: 50 });
|
||||
});
|
||||
|
||||
test("无效的 page 参数返回 400", () => {
|
||||
const invalidPage = ["0", "-1", "abc", "1.5"];
|
||||
|
||||
for (const page of invalidPage) {
|
||||
const result = validatePagination(page, "20", "production");
|
||||
expect(result).toHaveProperty("status", 400);
|
||||
}
|
||||
});
|
||||
|
||||
test("无效的 pageSize 参数返回 400", () => {
|
||||
const invalidPageSize = ["0", "-1", "abc", "1.5"];
|
||||
|
||||
for (const pageSize of invalidPageSize) {
|
||||
const result = validatePagination("1", pageSize, "production");
|
||||
expect(result).toHaveProperty("status", 400);
|
||||
}
|
||||
});
|
||||
|
||||
test("pageSize 超过上限返回 400", () => {
|
||||
const result = validatePagination("1", "201", "production");
|
||||
expect(result).toHaveProperty("status", 400);
|
||||
});
|
||||
|
||||
test("pageSize 等于上限 200 返回成功", () => {
|
||||
const result = validatePagination("1", "200", "production");
|
||||
expect(result).toEqual({ page: 1, pageSize: 200 });
|
||||
});
|
||||
});
|
||||
127
tests/server/static.test.ts
Normal file
127
tests/server/static.test.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import {
|
||||
contentTypeFor,
|
||||
hasFileExtension,
|
||||
htmlResponse,
|
||||
serveStaticAsset,
|
||||
type StaticAssets,
|
||||
} from "../../src/server/static";
|
||||
|
||||
function createTestAssets(): StaticAssets {
|
||||
return {
|
||||
files: {
|
||||
"/assets/index-a1b2c3.css": new Blob([".app{}"], { type: "text/css" }),
|
||||
"/assets/index-a1b2c3.js": new Blob(["console.log(1)"], { type: "text/javascript" }),
|
||||
"/assets/vendor-react-x9y8z7.js": new Blob(["react"], { type: "text/javascript" }),
|
||||
"/favicon.svg": new Blob(["<svg/>"], { type: "image/svg+xml" }),
|
||||
},
|
||||
indexHtml: new Blob(["<!doctype html><html></html>"], { type: "text/html" }),
|
||||
};
|
||||
}
|
||||
|
||||
describe("contentTypeFor", () => {
|
||||
test("JavaScript 文件", () => {
|
||||
expect(contentTypeFor("/assets/index-a1b2c3.js")).toBe("text/javascript; charset=utf-8");
|
||||
});
|
||||
|
||||
test("mjs 文件", () => {
|
||||
expect(contentTypeFor("/assets/chunk.mjs")).toBe("text/javascript; charset=utf-8");
|
||||
});
|
||||
|
||||
test("CSS 文件", () => {
|
||||
expect(contentTypeFor("/assets/style.css")).toBe("text/css; charset=utf-8");
|
||||
});
|
||||
|
||||
test("SVG 文件", () => {
|
||||
expect(contentTypeFor("/icon.svg")).toBe("image/svg+xml");
|
||||
});
|
||||
|
||||
test("未知扩展名返回 octet-stream", () => {
|
||||
expect(contentTypeFor("/file.xyz")).toBe("application/octet-stream");
|
||||
});
|
||||
|
||||
test("无扩展名返回 octet-stream", () => {
|
||||
expect(contentTypeFor("/noext")).toBe("application/octet-stream");
|
||||
});
|
||||
});
|
||||
|
||||
describe("hasFileExtension", () => {
|
||||
test("有扩展名", () => {
|
||||
expect(hasFileExtension("/assets/index.js")).toBe(true);
|
||||
expect(hasFileExtension("/favicon.svg")).toBe(true);
|
||||
});
|
||||
|
||||
test("无扩展名", () => {
|
||||
expect(hasFileExtension("/dashboard")).toBe(false);
|
||||
expect(hasFileExtension("/")).toBe(false);
|
||||
expect(hasFileExtension("/api/targets")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("htmlResponse", () => {
|
||||
test("返回 HTML 响应带正确 headers", async () => {
|
||||
const blob = new Blob(["<html></html>"]);
|
||||
const response = htmlResponse(blob);
|
||||
|
||||
expect(response.headers.get("Content-Type")).toBe("text/html; charset=utf-8");
|
||||
expect(response.headers.get("Cache-Control")).toBe("no-cache");
|
||||
expect(await response.text()).toBe("<html></html>");
|
||||
});
|
||||
});
|
||||
|
||||
describe("serveStaticAsset", () => {
|
||||
test("根路径返回 indexHtml", async () => {
|
||||
const assets = createTestAssets();
|
||||
const response = serveStaticAsset("/", assets);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toBe("text/html; charset=utf-8");
|
||||
expect(response.headers.get("Cache-Control")).toBe("no-cache");
|
||||
expect(await response.text()).toBe("<!doctype html><html></html>");
|
||||
});
|
||||
|
||||
test("已知资源返回对应文件和 immutable 缓存", async () => {
|
||||
const assets = createTestAssets();
|
||||
const response = serveStaticAsset("/assets/index-a1b2c3.js", assets);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toBe("text/javascript; charset=utf-8");
|
||||
expect(response.headers.get("Cache-Control")).toBe("public, max-age=31536000, immutable");
|
||||
expect(await response.text()).toBe("console.log(1)");
|
||||
});
|
||||
|
||||
test("未知带扩展名路径返回 404", () => {
|
||||
const assets = createTestAssets();
|
||||
const response = serveStaticAsset("/assets/missing.js", assets);
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
test("SPA fallback — 无扩展名路径返回 indexHtml", async () => {
|
||||
const assets = createTestAssets();
|
||||
const response = serveStaticAsset("/dashboard", assets);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toBe("text/html; charset=utf-8");
|
||||
expect(response.headers.get("Cache-Control")).toBe("no-cache");
|
||||
expect(await response.text()).toBe("<!doctype html><html></html>");
|
||||
});
|
||||
|
||||
test("SVG 资源返回正确 Content-Type", () => {
|
||||
const assets = createTestAssets();
|
||||
const response = serveStaticAsset("/favicon.svg", assets);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toBe("image/svg+xml");
|
||||
expect(response.headers.get("Cache-Control")).toBe("public, max-age=31536000, immutable");
|
||||
});
|
||||
|
||||
test("CSS 资源返回正确 Content-Type", () => {
|
||||
const assets = createTestAssets();
|
||||
const response = serveStaticAsset("/assets/index-a1b2c3.css", assets);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toBe("text/css; charset=utf-8");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user