1
0

refactor: 引入 Checker 统一接口与 Runner 抽象机制

定义 Checker 接口(resolve/execute/serialize)和 CheckerRegistry
注册中心,消除 engine/config-loader/store 中硬编码类型分支。
按 checker 类型分子包(runner/http/、runner/command/),提取
共享 expect 到 runner/shared/。超时控制通过引擎注入 AbortSignal。
CheckFailure.phase 从联合类型改为 string。配置校验下沉到各
Checker.resolve() 内部。

新增 checker-runner-abstraction spec,更新 DEVELOPMENT.md。
This commit is contained in:
2026-05-12 17:08:57 +08:00
parent e1c33b4002
commit ce8baae3d1
41 changed files with 1493 additions and 1395 deletions

View File

@@ -0,0 +1,142 @@
import { describe, expect, test } from "bun:test";
import { checkHttpExpect } from "../../../../../src/server/checker/runner/http/expect";
function obs(overrides: { statusCode?: number; headers?: Record<string, string>; body?: string | null; durationMs?: number } = {}) {
return {
statusCode: overrides.statusCode ?? 200,
headers: overrides.headers ?? {},
body: overrides.body ?? "",
durationMs: overrides.durationMs ?? 100,
};
}
describe("checkHttpExpect", () => {
test("无 expect 配置时默认检查 status [200] 匹配成功", () => {
const r = checkHttpExpect(obs().statusCode, obs().headers, obs().body as string, obs().durationMs);
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
});
test("无 expect 配置时 status 非 200 匹配失败", () => {
const r = checkHttpExpect(500, {}, "", 100);
expect(r.matched).toBe(false);
expect(r.failure).not.toBeNull();
expect(r.failure!.phase).toBe("status");
expect(r.failure!.kind).toBe("mismatch");
});
test("status 匹配指定状态码", () => {
const cfg = { status: [200, 301] };
expect(checkHttpExpect(200, {}, "", 100, cfg).matched).toBe(true);
expect(checkHttpExpect(301, {}, "", 100, cfg).matched).toBe(true);
expect(checkHttpExpect(404, {}, "", 100, cfg).matched).toBe(false);
});
test("status 不匹配返回 phase=status 的失败", () => {
const r = checkHttpExpect(503, {}, "", 100, { status: [200] });
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("status");
expect(r.failure!.expected).toEqual([200]);
expect(r.failure!.actual).toBe(503);
});
test("duration 在限制内匹配成功", () => {
const r = checkHttpExpect(200, {}, "", 50, { maxDurationMs: 100 });
expect(r.matched).toBe(true);
});
test("duration 超过限制匹配失败", () => {
const r = checkHttpExpect(200, {}, "", 200, { maxDurationMs: 100 });
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("duration");
});
test("duration 恰好等于限制匹配成功", () => {
const r = checkHttpExpect(200, {}, "", 100, { maxDurationMs: 100 });
expect(r.matched).toBe(true);
});
test("headers 字符串格式检查(等于)", () => {
const h = { "content-type": "application/json", "x-api": "v1" };
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": "application/json" } }).matched).toBe(true);
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": "text/html" } }).matched).toBe(false);
});
test("headers 操作符格式检查", () => {
const h = { "content-type": "application/json" };
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { contains: "json" } } }).matched).toBe(true);
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { match: "^application/" } } }).matched).toBe(true);
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { contains: "xml" } } }).matched).toBe(false);
});
test("headers 大小写不敏感匹配", () => {
const h = { "content-type": "application/json" };
expect(checkHttpExpect(200, h, "", 100, { headers: { "Content-Type": "application/json" } }).matched).toBe(true);
});
test("headers 不存在时返回失败", () => {
const r = checkHttpExpect(200, {}, "", 100, { headers: { "x-missing": "value" } });
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("headers");
});
test("body 规则数组按顺序检查", () => {
const body = JSON.stringify({ status: "ok", count: 5 });
const r = checkHttpExpect(200, {}, body, 100, {
body: [{ contains: "ok" }, { json: { path: "$.count", gte: 1 } }],
});
expect(r.matched).toBe(true);
});
test("body 第一条规则失败立即返回", () => {
const r = checkHttpExpect(200, {}, "hello world", 100, {
body: [{ contains: "missing" }, { contains: "hello" }],
});
expect(r.matched).toBe(false);
expect(r.failure!.path).toBe("body[0]");
});
test("body 为 null 但有 body 规则时报错", () => {
const r = checkHttpExpect(200, {}, null, 100, { body: [{ contains: "test" }] });
expect(r.matched).toBe(false);
expect(r.failure!.kind).toBe("error");
});
test("完整流水线 status->duration->headers->body 全部通过", () => {
const r = checkHttpExpect(200, { "content-type": "application/json" }, JSON.stringify({ status: "healthy" }), 50, {
status: [200],
maxDurationMs: 100,
headers: { "content-type": { contains: "json" } },
body: [{ json: { path: "$.status", equals: "healthy" } }],
});
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
});
test("完整流水线 status 通过但 duration 失败", () => {
const r = checkHttpExpect(200, {}, "", 500, { status: [200], maxDurationMs: 100 });
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("duration");
});
test("完整流水线 status 和 duration 通过但 headers 失败", () => {
const r = checkHttpExpect(200, { "x-api": "v1" }, "", 50, {
status: [200],
maxDurationMs: 100,
headers: { "x-api": "v2" },
});
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("headers");
});
test("完整流水线 status/duration/headers 通过但 body 失败", () => {
const r = checkHttpExpect(200, { "content-type": "text/plain" }, "error occurred", 50, {
status: [200],
maxDurationMs: 100,
headers: { "content-type": "text/plain" },
body: [{ contains: "success" }],
});
expect(r.matched).toBe(false);
expect(r.failure!.phase).toBe("body");
});
});

View File

@@ -0,0 +1,180 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { HttpChecker } from "../../../../../src/server/checker/runner/http/runner";
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
import type { ResolvedHttpTarget } from "../../../../../src/server/checker/types";
const checker = new HttpChecker();
describe("HttpChecker", () => {
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 });
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;
}): ResolvedHttpTarget {
return {
type: "http",
name: "test-http",
group: "default",
http: {
url: overrides.url ?? `${baseUrl}/ok`,
method: overrides.method ?? "GET",
headers: overrides.headers ?? {},
body: overrides.body,
maxBodyBytes: overrides.maxBodyBytes ?? 1024 * 1024,
},
intervalMs: 60000,
timeoutMs: overrides.timeoutMs ?? 5000,
expect: overrides.expect as ResolvedHttpTarget["expect"],
};
}
function makeCtx(timeoutMs = 5000): CheckerContext {
const controller = new AbortController();
setTimeout(() => controller.abort(), timeoutMs);
return { signal: controller.signal };
}
test("成功请求 200", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok` }), makeCtx());
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 checker.execute(makeTarget({ url: `${baseUrl}/notfound` }), makeCtx());
expect(result.matched).toBe(false);
expect(result.statusDetail).toBe("HTTP 404");
expect(result.failure!.phase).toBe("status");
});
test("404 匹配自定义 status [404]", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound`, expect: { status: [404] } }), makeCtx());
expect(result.matched).toBe(true);
});
test("headers 检查通过", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { headers: { "x-custom": "test-value" } } }), makeCtx());
expect(result.matched).toBe(true);
});
test("headers 检查失败", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { headers: { "x-custom": "wrong-value" } } }), makeCtx());
expect(result.matched).toBe(false);
expect(result.failure!.phase).toBe("headers");
});
test("body contains 检查", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { body: [{ contains: "hello" }] } }), makeCtx());
expect(result.matched).toBe(true);
});
test("body contains 失败", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { body: [{ contains: "nonexistent" }] } }), makeCtx());
expect(result.matched).toBe(false);
expect(result.failure!.phase).toBe("body");
});
test("body json 检查", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/json`, expect: { body: [{ json: { path: "$.status", equals: "ok" } }] } }), makeCtx());
expect(result.matched).toBe(true);
});
test("响应体超过 maxBodyBytes", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/large`, maxBodyBytes: 100, expect: { body: [{ contains: "x" }] } }), makeCtx());
expect(result.matched).toBe(false);
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 checker.execute(makeTarget({ url: `http://localhost:${timeoutServer.port}/`, timeoutMs: 100 }), makeCtx(100));
expect(result.matched).toBe(false);
expect(result.failure!.message).toContain("超时");
} finally {
timeoutServer.stop();
}
});
test("快速失败status 失败时不读取 body", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound`, expect: { status: [200], body: [{ contains: "something" }] } }), makeCtx());
expect(result.matched).toBe(false);
expect(result.failure!.phase).toBe("status");
});
test("status 通过但 body 失败", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { status: [200], body: [{ contains: "not-in-body" }] } }), makeCtx());
expect(result.matched).toBe(false);
expect(result.failure!.phase).toBe("body");
});
test("无 expect 时默认检查 status 200", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: undefined }), makeCtx());
expect(result.matched).toBe(true);
});
test("POST 请求携带 body", async () => {
const result = await checker.execute(makeTarget({ url: `${baseUrl}/echo`, method: "POST", body: "test-body", headers: { "content-type": "text/plain" }, expect: { status: [200], body: [{ json: { path: "$.body", equals: "present" } }] } }), makeCtx());
expect(result.matched).toBe(true);
});
test("serialize 返回 URL 和 config JSON", () => {
const target = makeTarget({});
const s = checker.serialize(target);
expect(s.target).toBe(target.http.url);
const config = JSON.parse(s.config);
expect(config.url).toBe(target.http.url);
expect(config.method).toBe("GET");
});
});