1
0
Files
DiAL/tests/server/checker/expect/http.test.ts
lanyuanxiaoyao b8810f1182 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 与示例配置
2026-05-10 22:25:21 +08:00

166 lines
5.9 KiB
TypeScript

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