1
0

test: 重构测试体系 — 建立组件测试层、补充后端测试、清理低质量测试

- 新增 jsdom + @testing-library/react 组件测试环境
- 新增 12 个组件测试,覆盖所有前端组件
- 补充后端 middleware 和 helpers 单元测试
- 删除伪测试 use-target-detail-logic.test.ts
- 精简过度枚举的 color-threshold.test.ts
- 新增 bunfig.toml 配置测试 preload
- 更新 DEVELOPMENT.md 测试章节
- 安装 @types/jsdom 修复类型声明
This commit is contained in:
2026-05-15 18:31:33 +08:00
parent 2b08f81a0d
commit 8793fbd786
24 changed files with 1392 additions and 143 deletions

View File

@@ -0,0 +1,109 @@
import { describe, expect, test } from "bun:test";
import { createApiError, createHeaders, formatDuration, jsonResponse } from "../../src/server/helpers";
describe("createApiError", () => {
test("创建错误响应对象", () => {
const result = createApiError("Not found", 404);
expect(result).toEqual({ error: "Not found", status: 404 });
});
test("支持不同的错误消息和状态码", () => {
const badRequest = createApiError("Bad request", 400);
const internalError = createApiError("Internal error", 500);
expect(badRequest).toEqual({ error: "Bad request", status: 400 });
expect(internalError).toEqual({ error: "Internal error", status: 500 });
});
});
describe("createHeaders", () => {
test("生产模式添加安全 headers", () => {
const headers = createHeaders("production", { "Content-Type": "application/json" });
expect(headers.get("X-Content-Type-Options")).toBe("nosniff");
expect(headers.get("Referrer-Policy")).toBe("strict-origin-when-cross-origin");
expect(headers.get("Content-Type")).toBe("application/json");
});
test("非生产模式不添加安全 headers", () => {
const headers = createHeaders("test", { "Content-Type": "application/json" });
expect(headers.get("X-Content-Type-Options")).toBeNull();
expect(headers.get("Referrer-Policy")).toBeNull();
expect(headers.get("Content-Type")).toBe("application/json");
});
test("保留传入的自定义 headers", () => {
const headers = createHeaders("production", { "X-Custom-Header": "custom-value" });
expect(headers.get("X-Custom-Header")).toBe("custom-value");
});
});
describe("jsonResponse", () => {
test("创建 JSON 响应", () => {
const body = { message: "Hello" };
const response = jsonResponse(body, { mode: "test" });
expect(response.status).toBe(200);
expect(response.headers.get("Content-Type")).toBe("application/json; charset=utf-8");
});
test("生产模式响应包含安全 headers", () => {
const response = jsonResponse({ data: "test" }, { mode: "production" });
expect(response.headers.get("X-Content-Type-Options")).toBe("nosniff");
expect(response.headers.get("Referrer-Policy")).toBe("strict-origin-when-cross-origin");
});
test("支持自定义状态码", () => {
const response = jsonResponse({ error: "Not found" }, { mode: "test", status: 404 });
expect(response.status).toBe(404);
});
test("支持自定义 headers", () => {
const response = jsonResponse(
{ data: "test" },
{
headers: { "X-Custom": "value" },
mode: "test",
},
);
expect(response.headers.get("X-Custom")).toBe("value");
});
test("响应 body 可以被解析为 JSON", async () => {
const body = { count: 42, message: "Hello" };
const response = jsonResponse(body, { mode: "test" });
const parsed = (await response.json()) as { count: number; message: string };
expect(parsed).toEqual(body);
});
});
describe("formatDuration", () => {
test("毫秒格式化", () => {
expect(formatDuration(100)).toBe("100ms");
expect(formatDuration(999)).toBe("999ms");
});
test("秒格式化(整秒)", () => {
expect(formatDuration(1000)).toBe("1s");
expect(formatDuration(5000)).toBe("5s");
expect(formatDuration(59000)).toBe("59s");
});
test("分钟格式化(整分钟)", () => {
expect(formatDuration(60000)).toBe("1m");
expect(formatDuration(120000)).toBe("2m");
expect(formatDuration(300000)).toBe("5m");
});
test("非整秒/整分钟保持毫秒", () => {
expect(formatDuration(1500)).toBe("1500ms");
expect(formatDuration(61123)).toBe("61123ms");
});
});

View File

@@ -0,0 +1,171 @@
import { describe, expect, test } from "bun:test";
import {
validateDashboardWindow,
validateMetricsBucket,
validatePagination,
validateRecentLimit,
validateTargetId,
validateTimeRange,
} from "../../src/server/middleware";
describe("validateTargetId", () => {
test("有效的 target ID 返回数字", () => {
const result = validateTargetId("123", "production");
expect(result).not.toHaveProperty("status");
expect((result as { id: number }).id).toBe(123);
});
test("无效的 target ID 返回 400", () => {
const invalid = ["0", "-1", "abc", "1.5", ""];
for (const id of invalid) {
const result = validateTargetId(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 });
});
});
describe("validateRecentLimit", () => {
test("默认值recentLimit=30", () => {
const result = validateRecentLimit(null, "production");
expect(result).toEqual({ recentLimit: 30 });
});
test("有效的 recentLimit 参数", () => {
const result = validateRecentLimit("50", "production");
expect(result).toEqual({ recentLimit: 50 });
});
test("无效的 recentLimit 参数返回 400", () => {
const invalid = ["0", "-1", "abc", "1.5"];
for (const limit of invalid) {
const result = validateRecentLimit(limit, "production");
expect(result).toHaveProperty("status", 400);
}
});
test("recentLimit 超过上限返回 400", () => {
const result = validateRecentLimit("201", "production");
expect(result).toHaveProperty("status", 400);
});
test("recentLimit 等于上限 200 返回成功", () => {
const result = validateRecentLimit("200", "production");
expect(result).toEqual({ recentLimit: 200 });
});
});
describe("validateDashboardWindow", () => {
test("默认值window=24h", () => {
const result = validateDashboardWindow(null, "production");
expect(result).not.toHaveProperty("status");
expect((result as { label: string }).label).toBe("24h");
});
test("window=24h 返回成功", () => {
const result = validateDashboardWindow("24h", "production");
expect(result).not.toHaveProperty("status");
expect((result as { label: string }).label).toBe("24h");
});
test("不支持的 window 参数返回 400", () => {
const result = validateDashboardWindow("7d", "production");
expect(result).toHaveProperty("status", 400);
});
});
describe("validateMetricsBucket", () => {
test("默认值bucket=1h", () => {
const result = validateMetricsBucket(null, "production");
expect(result).toEqual({ bucket: "1h" });
});
test("bucket=1h 返回成功", () => {
const result = validateMetricsBucket("1h", "production");
expect(result).toEqual({ bucket: "1h" });
});
test("不支持的 bucket 参数返回 400", () => {
const result = validateMetricsBucket("5m", "production");
expect(result).toHaveProperty("status", 400);
});
});