feat: 重构为多类型 checker 通用框架,支持 HTTP 与命令检查
- 引入 typed target 判别联合,支持 http 与 command 两种 checker - expect 重构为有序规则数组,按配置顺序快速失败并生成结构化 failure - 新增 command runner,支持 exec + args 本地命令执行 - 引入全局并发限制 maxConcurrentChecks 和 size 解析 (KB/MB/GB) - HTTP/command 各自独立 expect pipeline,应用领域默认成功语义 - SQLite schema、API、Dashboard 全链路调整为 checker 通用契约 - 补充完整测试覆盖(192 tests),更新 README 与示例配置
This commit is contained in:
@@ -1,118 +1,258 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkExpect } from "../../../src/server/checker/fetcher";
|
||||
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
||||
import { runHttpCheck } from "../../../src/server/checker/fetcher";
|
||||
|
||||
const emptyHeaders: Record<string, string> = {};
|
||||
|
||||
describe("checkExpect", () => {
|
||||
test("无 expect 配置时 matched 为 true", () => {
|
||||
expect(checkExpect(200, "ok", 100, emptyHeaders, undefined)).toBe(true);
|
||||
});
|
||||
|
||||
test("status 匹配", () => {
|
||||
expect(checkExpect(200, "", 100, emptyHeaders, { status: [200, 201] })).toBe(true);
|
||||
expect(checkExpect(201, "", 100, emptyHeaders, { status: [200, 201] })).toBe(true);
|
||||
expect(checkExpect(404, "", 100, emptyHeaders, { status: [200, 201] })).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 匹配", () => {
|
||||
const headers = { "content-type": "application/json", "x-custom": "test" };
|
||||
expect(checkExpect(200, "", 100, headers, { headers: { "Content-Type": "application/json" } })).toBe(true);
|
||||
expect(checkExpect(200, "", 100, headers, { headers: { "Content-Type": "text/html" } })).toBe(false);
|
||||
expect(checkExpect(200, "", 100, headers, { headers: { "X-Missing": "test" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body.contains 匹配", () => {
|
||||
expect(checkExpect(200, "hello world", 100, emptyHeaders, { body: { contains: "hello" } })).toBe(true);
|
||||
expect(checkExpect(200, "hello world", 100, emptyHeaders, { body: { contains: "missing" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body.regex 匹配", () => {
|
||||
expect(checkExpect(200, "status: ok", 100, emptyHeaders, { body: { regex: "status.*ok" } })).toBe(true);
|
||||
expect(checkExpect(200, "status: error", 100, emptyHeaders, { body: { regex: "status.*ok" } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body.json 匹配", () => {
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "ok" }), 100, emptyHeaders, { body: { json: { "$.status": "ok" } } }),
|
||||
).toBe(true);
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "error" }), 100, emptyHeaders, {
|
||||
body: { json: { "$.status": "ok" } },
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test("body.json 解析失败", () => {
|
||||
expect(checkExpect(200, "not json", 100, emptyHeaders, { body: { json: { "$.status": "ok" } } })).toBe(false);
|
||||
});
|
||||
|
||||
test("body 多种方法 AND 组合", () => {
|
||||
expect(
|
||||
checkExpect(200, "healthy", 100, emptyHeaders, {
|
||||
body: {
|
||||
contains: "healthy",
|
||||
regex: "healthy",
|
||||
},
|
||||
}),
|
||||
).toBe(true);
|
||||
|
||||
expect(
|
||||
checkExpect(200, "healthy", 100, emptyHeaders, {
|
||||
body: {
|
||||
contains: "healthy",
|
||||
regex: "unhealthy",
|
||||
},
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test("maxLatencyMs 匹配", () => {
|
||||
expect(checkExpect(200, "", 100, emptyHeaders, { maxLatencyMs: 200 })).toBe(true);
|
||||
expect(checkExpect(200, "", 300, emptyHeaders, { maxLatencyMs: 200 })).toBe(false);
|
||||
expect(checkExpect(200, "", 200, emptyHeaders, { maxLatencyMs: 200 })).toBe(true);
|
||||
});
|
||||
|
||||
test("多条 expect 全部通过", () => {
|
||||
expect(
|
||||
checkExpect(200, "healthy", 100, emptyHeaders, {
|
||||
status: [200],
|
||||
body: { contains: "healthy" },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("多条 expect 部分失败", () => {
|
||||
expect(
|
||||
checkExpect(200, "healthy", 500, emptyHeaders, {
|
||||
status: [200],
|
||||
body: { contains: "healthy" },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test("status + headers + body + maxLatencyMs 全组合", () => {
|
||||
const headers = { "content-type": "application/json" };
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "ok" }), 100, headers, {
|
||||
status: [200],
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: { contains: "ok", json: { "$.status": "ok" } },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("全组合中 headers 失败", () => {
|
||||
const headers = { "content-type": "text/html" };
|
||||
expect(
|
||||
checkExpect(200, JSON.stringify({ status: "ok" }), 100, headers, {
|
||||
status: [200],
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: { contains: "ok", json: { "$.status": "ok" } },
|
||||
maxLatencyMs: 200,
|
||||
}),
|
||||
).toBe(false);
|
||||
describe("runHttpCheck", () => {
|
||||
test("checkExpect 已移除", async () => {
|
||||
const mod = await import("../../../src/server/checker/fetcher");
|
||||
expect((mod as Record<string, unknown>).checkExpect).toBeUndefined();
|
||||
expect((mod as Record<string, unknown>).fetchTarget).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("runHttpCheck 集成", () => {
|
||||
let server: ReturnType<typeof Bun.serve>;
|
||||
let baseUrl: string;
|
||||
|
||||
beforeAll(() => {
|
||||
server = Bun.serve({
|
||||
port: 0,
|
||||
fetch(req) {
|
||||
const url = new URL(req.url);
|
||||
switch (url.pathname) {
|
||||
case "/ok":
|
||||
return new Response("hello world", {
|
||||
headers: { "content-type": "text/plain", "x-custom": "test-value" },
|
||||
});
|
||||
case "/json":
|
||||
return new Response(JSON.stringify({ status: "ok" }), {
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
case "/echo":
|
||||
return new Response(JSON.stringify({ method: req.method, body: req.body ? "present" : "empty" }), {
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
case "/large":
|
||||
return new Response("x".repeat(2000));
|
||||
case "/notfound":
|
||||
return new Response("not found", { status: 404 });
|
||||
case "/slow":
|
||||
return new Response("slow", { status: 200 });
|
||||
default:
|
||||
return new Response("ok");
|
||||
}
|
||||
},
|
||||
});
|
||||
baseUrl = `http://localhost:${server.port}`;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
server.stop();
|
||||
});
|
||||
|
||||
function makeTarget(overrides: {
|
||||
url?: string;
|
||||
method?: string;
|
||||
body?: string;
|
||||
headers?: Record<string, string>;
|
||||
expect?: Record<string, unknown>;
|
||||
maxBodyBytes?: number;
|
||||
timeoutMs?: number;
|
||||
}) {
|
||||
return {
|
||||
type: "http" as const,
|
||||
name: "test-http",
|
||||
http: {
|
||||
url: overrides.url ?? `${baseUrl}/ok`,
|
||||
method: overrides.method ?? "GET",
|
||||
headers: overrides.headers ?? ({} as Record<string, string>),
|
||||
body: overrides.body,
|
||||
maxBodyBytes: overrides.maxBodyBytes ?? 1024 * 1024,
|
||||
},
|
||||
intervalMs: 60000,
|
||||
timeoutMs: overrides.timeoutMs ?? 5000,
|
||||
expect: overrides.expect as import("../../../src/server/checker/types").HttpExpectConfig | undefined,
|
||||
};
|
||||
}
|
||||
|
||||
test("成功请求 200", async () => {
|
||||
const result = await runHttpCheck(makeTarget({ url: `${baseUrl}/ok` }));
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("HTTP 200");
|
||||
expect(result.durationMs).not.toBeNull();
|
||||
expect(result.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("404 不匹配默认 status [200]", async () => {
|
||||
const result = await runHttpCheck(makeTarget({ url: `${baseUrl}/notfound` }));
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.statusDetail).toBe("HTTP 404");
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
});
|
||||
|
||||
test("404 匹配自定义 status [404]", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/notfound`,
|
||||
expect: { status: [404] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 检查通过", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { headers: { "x-custom": "test-value" } },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 检查失败", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { headers: { "x-custom": "wrong-value" } },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("body contains 检查", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { body: [{ contains: "hello" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("body contains 失败", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { body: [{ contains: "nonexistent" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure!.phase).toBe("body");
|
||||
});
|
||||
|
||||
test("body json 检查", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/json`,
|
||||
expect: { body: [{ json: { path: "$.status", equals: "ok" } }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
test("响应体超过 maxBodyBytes", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/large`,
|
||||
maxBodyBytes: 100,
|
||||
expect: { body: [{ contains: "x" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("body");
|
||||
expect(result.failure!.message).toContain("超过限制");
|
||||
});
|
||||
|
||||
test("请求超时", async () => {
|
||||
const timeoutServer = Bun.serve({
|
||||
port: 0,
|
||||
async fetch() {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10000));
|
||||
return new Response("late");
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `http://localhost:${timeoutServer.port}/`,
|
||||
timeoutMs: 100,
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.message).toContain("超时");
|
||||
} finally {
|
||||
timeoutServer.stop();
|
||||
}
|
||||
});
|
||||
|
||||
test("快速失败:status 失败时不读取 body", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/notfound`,
|
||||
expect: { status: [200], body: [{ contains: "something" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
});
|
||||
|
||||
test("快速失败:headers 失败时不读取 body", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { headers: { "x-missing": "value" }, body: [{ contains: "hello" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("status 通过但 body 失败", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { status: [200], body: [{ contains: "not-in-body" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.failure!.phase).toBe("body");
|
||||
});
|
||||
|
||||
test("无 expect 时默认检查 status 200", async () => {
|
||||
const result = await runHttpCheck(makeTarget({ url: `${baseUrl}/ok`, expect: undefined }));
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
test("POST 请求携带 body", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/echo`,
|
||||
method: "POST",
|
||||
body: "test-body",
|
||||
headers: { "content-type": "text/plain" },
|
||||
expect: { status: [200], body: [{ json: { path: "$.body", equals: "present" } }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
test("仅 contains 规则时不解析 JSON", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { body: [{ contains: "hello world" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user