- 引入共享 ValueMatcher(equals/contains/regex/exists/empty/gt/gte/lt/lte) - 引入共享 ContentRules 数组(direct/json/css/xpath 提取器) - 引入共享 KeyValueExpect(动态键值断言,字面量等价 equals) - maxDurationMs → durationMs: ValueMatcher(所有 checker) - match → regex(固定无 flags) - Ping max* → packetLossPercent/avgLatencyMs/maxLatencyMs(ValueMatcher) - LLM finishReason/rawFinishReason → ValueMatcher - DB 新增 result: ContentRules - TCP banner → ContentRules 数组 - 删除旧模块:operator.ts、validate-operator.ts、duration.ts、body.ts、text.ts、output.ts - 更新全部 checker schema/validate/expect/execute - 更新 probe-config.schema.json、probes.example.yaml - 更新 README.md、DEVELOPMENT.md(含 expect 字段选择规范) - 同步 10 个 delta specs 到主 specs,归档 change
204 lines
7.8 KiB
TypeScript
204 lines
7.8 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import { checkContentRules } from "../../../../../src/server/checker/expect/content";
|
|
|
|
function checkBodyExpect(body: string, rules?: Parameters<typeof checkContentRules>[1]) {
|
|
return checkContentRules(body, rules, { path: "body", phase: "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({ code: 0, status: "ok" });
|
|
const r = checkBodyExpect(body, [{ json: { equals: "ok", path: "$.status" } }]);
|
|
expect(r.matched).toBe(true);
|
|
});
|
|
|
|
test("json 等值匹配失败", () => {
|
|
const body = JSON.stringify({ status: "ok" });
|
|
const r = checkBodyExpect(body, [{ json: { equals: "error", path: "$.status" } }]);
|
|
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: { gte: 10, path: "$.count" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(body, [{ json: { path: "$.version", regex: "\\d+\\.\\d+\\.\\d+" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(body, [{ json: { gte: 100, path: "$.count" } }]).matched).toBe(false);
|
|
});
|
|
|
|
test("json 路径不存在", () => {
|
|
const body = JSON.stringify({ status: "ok" });
|
|
const r = checkBodyExpect(body, [{ json: { equals: "value", path: "$.notExist" } }]);
|
|
expect(r.matched).toBe(false);
|
|
});
|
|
|
|
test("json 解析失败", () => {
|
|
const r = checkBodyExpect("not json", [{ json: { equals: "ok", path: "$.status" } }]);
|
|
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: { equals: "OK", selector: "div#health" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(html, [{ css: { equals: "1.0", selector: "span.ver" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(html, [{ css: { equals: "ERROR", selector: "div#health" } }]).matched).toBe(false);
|
|
});
|
|
|
|
test("css 选择器无匹配元素", () => {
|
|
const html = "<div>OK</div>";
|
|
const r = checkBodyExpect(html, [{ css: { equals: "OK", selector: "span.missing" } }]);
|
|
expect(r.matched).toBe(false);
|
|
});
|
|
|
|
test("css attr 提取", () => {
|
|
const html = '<meta name="version" content="2.0.1">';
|
|
expect(
|
|
checkBodyExpect(html, [{ css: { attr: "content", equals: "2.0.1", selector: 'meta[name="version"]' } }]).matched,
|
|
).toBe(true);
|
|
});
|
|
|
|
test("css exists 检查", () => {
|
|
const html = "<div id='test'>OK</div>";
|
|
expect(checkBodyExpect(html, [{ css: { exists: true, selector: "div#test" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(html, [{ css: { exists: false, selector: "span#missing" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(html, [{ css: { exists: false, selector: "div#test" } }]).matched).toBe(false);
|
|
});
|
|
|
|
test("xpath 节点文本匹配", () => {
|
|
const xml = "<root><status>ok</status><code>200</code></root>";
|
|
expect(checkBodyExpect(xml, [{ xpath: { equals: "ok", path: "/root/status/text()" } }]).matched).toBe(true);
|
|
expect(checkBodyExpect(xml, [{ xpath: { equals: "error", path: "/root/status/text()" } }]).matched).toBe(false);
|
|
});
|
|
|
|
test("xpath 无匹配节点", () => {
|
|
const xml = "<root><status>ok</status></root>";
|
|
const r = checkBodyExpect(xml, [{ xpath: { equals: "ok", path: "/root/missing/text()" } }]);
|
|
expect(r.matched).toBe(false);
|
|
});
|
|
|
|
test("规则数组按顺序检查,第一条失败立即返回", () => {
|
|
const body = JSON.stringify({ status: "error" });
|
|
const r = checkBodyExpect(body, [{ contains: "healthy" }, { json: { equals: "error", path: "$.status" } }]);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.path).toBe("body[0]");
|
|
});
|
|
|
|
test("多条规则全部通过", () => {
|
|
const body = JSON.stringify({ count: 5, status: "healthy" });
|
|
const r = checkBodyExpect(body, [
|
|
{ contains: "healthy" },
|
|
{ json: { equals: "healthy", path: "$.status" } },
|
|
{ json: { gte: 1, path: "$.count" } },
|
|
]);
|
|
expect(r.matched).toBe(true);
|
|
expect(r.failure).toBeNull();
|
|
});
|
|
|
|
test("多条 json 规则共享解析结果且全部通过", () => {
|
|
const body = JSON.stringify({ count: 5, status: "healthy" });
|
|
const originalParse = JSON.parse;
|
|
let parseCount = 0;
|
|
JSON.parse = ((text, reviver) => {
|
|
parseCount++;
|
|
return originalParse(text, reviver) as unknown;
|
|
}) as typeof JSON.parse;
|
|
|
|
try {
|
|
const r = checkBodyExpect(body, [
|
|
{ json: { equals: "healthy", path: "$.status" } },
|
|
{ json: { gte: 1, path: "$.count" } },
|
|
]);
|
|
expect(r.matched).toBe(true);
|
|
expect(r.failure).toBeNull();
|
|
expect(parseCount).toBe(1);
|
|
} finally {
|
|
JSON.parse = originalParse;
|
|
}
|
|
});
|
|
|
|
test("第二条规则失败返回正确索引", () => {
|
|
const body = JSON.stringify({ status: "ok" });
|
|
const r = checkBodyExpect(body, [{ contains: "ok" }, { json: { equals: "error", path: "$.status" } }]);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.path).toContain("body[1]");
|
|
});
|
|
|
|
test("JSON 响应不是合法 JSON 返回 error kind", () => {
|
|
const r = checkBodyExpect("not json", [{ json: { equals: "ok", path: "$.status" } }]);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.kind).toBe("error");
|
|
expect(r.failure!.phase).toBe("body");
|
|
expect(r.failure!.path).toContain("json");
|
|
});
|
|
|
|
test("CSS selector 无匹配元素返回 mismatch kind", () => {
|
|
const html = "<div>no match</div>";
|
|
const r = checkBodyExpect(html, [{ css: { equals: "test", selector: "span.missing" } }]);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.kind).toBe("mismatch");
|
|
expect(r.failure!.phase).toBe("body");
|
|
expect(r.failure!.path).toContain("css");
|
|
});
|
|
|
|
test("XPath 无匹配节点返回 mismatch kind", () => {
|
|
const xml = "<root><status>ok</status></root>";
|
|
const r = checkBodyExpect(xml, [{ xpath: { equals: "ok", path: "/root/missing/text()" } }]);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.kind).toBe("mismatch");
|
|
expect(r.failure!.phase).toBe("body");
|
|
expect(r.failure!.path).toContain("xpath");
|
|
});
|
|
|
|
test("regex 规则使用 regex 字段", () => {
|
|
const r = checkBodyExpect("status: ok", [{ regex: "^status:" }]);
|
|
expect(r.matched).toBe(true);
|
|
});
|
|
|
|
test("regex 规则失败返回 body phase", () => {
|
|
const r = checkBodyExpect("status: error", [{ regex: "^ok$" }]);
|
|
expect(r.matched).toBe(false);
|
|
expect(r.failure!.phase).toBe("body");
|
|
expect(r.failure!.path).toBe("body[0]");
|
|
});
|
|
});
|