1
0
Files
DiAL/tests/server/checker/runner/shared/body.test.ts
lanyuanxiaoyao ce8baae3d1 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。
2026-05-12 17:08:57 +08:00

139 lines
5.5 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { checkBodyExpect } from "../../../../../src/server/checker/runner/shared/body";
describe("checkBodyExpect (BodyRule[])", () => {
test("无规则返回匹配成功", () => {
const r = checkBodyExpect("anything");
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
});
test("空规则数组返回匹配成功", () => {
const r = checkBodyExpect("anything", []);
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
});
test("contains 规则匹配成功", () => {
const r = checkBodyExpect("hello world", [{ contains: "hello" }]);
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
});
test("contains 规则匹配失败", () => {
const r = checkBodyExpect("hello world", [{ contains: "missing" }]);
expect(r.matched).toBe(false);
expect(r.failure).not.toBeNull();
expect(r.failure!.kind).toBe("mismatch");
expect(r.failure!.phase).toBe("body");
expect(r.failure!.path).toBe("body[0]");
});
test("regex 规则匹配成功", () => {
const r = checkBodyExpect("status: ok", [{ regex: "ok" }]);
expect(r.matched).toBe(true);
});
test("regex 规则匹配失败", () => {
const r = checkBodyExpect("status: error", [{ regex: "^ok$" }]);
expect(r.matched).toBe(false);
expect(r.failure!.path).toBe("body[0]");
});
test("json 等值匹配成功", () => {
const body = JSON.stringify({ status: "ok", code: 0 });
const r = checkBodyExpect(body, [{ json: { path: "$.status", equals: "ok" } }]);
expect(r.matched).toBe(true);
});
test("json 等值匹配失败", () => {
const body = JSON.stringify({ status: "ok" });
const r = checkBodyExpect(body, [{ json: { path: "$.status", equals: "error" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.kind).toBe("mismatch");
});
test("json 操作符匹配", () => {
const body = JSON.stringify({ count: 42, version: "v2.1.0" });
expect(checkBodyExpect(body, [{ json: { path: "$.count", gte: 10 } }]).matched).toBe(true);
expect(checkBodyExpect(body, [{ json: { path: "$.version", match: "\\d+\\.\\d+\\.\\d+" } }]).matched).toBe(true);
expect(checkBodyExpect(body, [{ json: { path: "$.count", gte: 100 } }]).matched).toBe(false);
});
test("json 路径不存在", () => {
const body = JSON.stringify({ status: "ok" });
const r = checkBodyExpect(body, [{ json: { path: "$.notExist", equals: "value" } }]);
expect(r.matched).toBe(false);
});
test("json 解析失败", () => {
const r = checkBodyExpect("not json", [{ json: { path: "$.status", equals: "ok" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.kind).toBe("error");
});
test("css 文本内容匹配", () => {
const html = "<div id='health'>OK</div><span class='ver'>1.0</span>";
expect(checkBodyExpect(html, [{ css: { selector: "div#health", equals: "OK" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "span.ver", equals: "1.0" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "div#health", equals: "ERROR" } }]).matched).toBe(false);
});
test("css 选择器无匹配元素", () => {
const html = "<div>OK</div>";
const r = checkBodyExpect(html, [{ css: { selector: "span.missing", equals: "OK" } }]);
expect(r.matched).toBe(false);
});
test("css attr 提取", () => {
const html = '<meta name="version" content="2.0.1">';
expect(
checkBodyExpect(html, [{ css: { selector: 'meta[name="version"]', attr: "content", equals: "2.0.1" } }]).matched,
).toBe(true);
});
test("css exists 检查", () => {
const html = "<div id='test'>OK</div>";
expect(checkBodyExpect(html, [{ css: { selector: "div#test", exists: true } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "span#missing", exists: false } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "div#test", exists: false } }]).matched).toBe(false);
});
test("xpath 节点文本匹配", () => {
const xml = "<root><status>ok</status><code>200</code></root>";
expect(checkBodyExpect(xml, [{ xpath: { path: "/root/status/text()", equals: "ok" } }]).matched).toBe(true);
expect(checkBodyExpect(xml, [{ xpath: { path: "/root/status/text()", equals: "error" } }]).matched).toBe(false);
});
test("xpath 无匹配节点", () => {
const xml = "<root><status>ok</status></root>";
const r = checkBodyExpect(xml, [{ xpath: { path: "/root/missing/text()", equals: "ok" } }]);
expect(r.matched).toBe(false);
});
test("规则数组按顺序检查,第一条失败立即返回", () => {
const body = JSON.stringify({ status: "error" });
const r = checkBodyExpect(body, [{ contains: "healthy" }, { json: { path: "$.status", equals: "error" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.path).toBe("body[0]");
});
test("多条规则全部通过", () => {
const body = JSON.stringify({ status: "healthy", count: 5 });
const r = checkBodyExpect(body, [
{ contains: "healthy" },
{ json: { path: "$.status", equals: "healthy" } },
{ json: { path: "$.count", gte: 1 } },
]);
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
});
test("第二条规则失败返回正确索引", () => {
const body = JSON.stringify({ status: "ok" });
const r = checkBodyExpect(body, [{ contains: "ok" }, { json: { path: "$.status", equals: "error" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.path).toContain("body[1]");
});
});