- 引入共享 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
152 lines
5.5 KiB
TypeScript
152 lines
5.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import { applyMatcher, checkExpectValue, evaluateJsonPath } from "../../../../../src/server/checker/expect/matcher";
|
|
|
|
describe("evaluateJsonPath", () => {
|
|
const obj = {
|
|
active: true,
|
|
code: 0,
|
|
data: {
|
|
count: 42,
|
|
items: [{ name: "a" }, { name: "b" }],
|
|
nested: { deep: "value" },
|
|
},
|
|
emptyArr: [],
|
|
emptyObj: {},
|
|
error: null,
|
|
status: "ok",
|
|
};
|
|
|
|
test("简单字段访问", () => {
|
|
expect(evaluateJsonPath(obj, "$.status")).toBe("ok");
|
|
expect(evaluateJsonPath(obj, "$.code")).toBe(0);
|
|
expect(evaluateJsonPath(obj, "$.active")).toBe(true);
|
|
expect(evaluateJsonPath(obj, "$.error")).toBeNull();
|
|
});
|
|
|
|
test("嵌套对象访问", () => {
|
|
expect(evaluateJsonPath(obj, "$.data.count")).toBe(42);
|
|
expect(evaluateJsonPath(obj, "$.data.nested.deep")).toBe("value");
|
|
});
|
|
|
|
test("数组索引访问", () => {
|
|
expect(evaluateJsonPath(obj, "$.data.items[0].name")).toBe("a");
|
|
expect(evaluateJsonPath(obj, "$.data.items[1].name")).toBe("b");
|
|
});
|
|
|
|
test("路径不存在返回 undefined", () => {
|
|
expect(evaluateJsonPath(obj, "$.notExist")).toBeUndefined();
|
|
expect(evaluateJsonPath(obj, "$.data.notExist")).toBeUndefined();
|
|
expect(evaluateJsonPath(obj, "$.data.items[99]")).toBeUndefined();
|
|
});
|
|
|
|
test("空对象和空数组", () => {
|
|
expect(evaluateJsonPath(obj, "$.emptyObj")).toEqual({});
|
|
expect(evaluateJsonPath(obj, "$.emptyArr")).toEqual([]);
|
|
});
|
|
|
|
test("非 $ 开头路径返回 undefined", () => {
|
|
expect(evaluateJsonPath(obj, "status")).toBeUndefined();
|
|
expect(evaluateJsonPath(obj, ".status")).toBeUndefined();
|
|
});
|
|
|
|
test("null 对象上访问", () => {
|
|
expect(evaluateJsonPath(null, "$.any")).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("applyMatcher", () => {
|
|
test("equals 操作符", () => {
|
|
expect(applyMatcher("ok", { equals: "ok" })).toBe(true);
|
|
expect(applyMatcher("ok", { equals: "error" })).toBe(false);
|
|
expect(applyMatcher(42, { equals: 42 })).toBe(true);
|
|
expect(applyMatcher(42, { equals: 41 })).toBe(false);
|
|
expect(applyMatcher(null, { equals: null })).toBe(true);
|
|
expect(applyMatcher(true, { equals: true })).toBe(true);
|
|
});
|
|
|
|
test("equals 支持 JSON 对象和数组", () => {
|
|
expect(applyMatcher({ status: "ok" }, { equals: { status: "ok" } })).toBe(true);
|
|
expect(applyMatcher({ status: "ok" }, { equals: { status: "fail" } })).toBe(false);
|
|
expect(applyMatcher(["a", "b"], { equals: ["a", "b"] })).toBe(true);
|
|
expect(applyMatcher(["a", "b"], { equals: ["b", "a"] })).toBe(false);
|
|
});
|
|
|
|
test("contains 操作符", () => {
|
|
expect(applyMatcher("hello world", { contains: "hello" })).toBe(true);
|
|
expect(applyMatcher("hello world", { contains: "missing" })).toBe(false);
|
|
expect(applyMatcher(12345, { contains: "23" })).toBe(true);
|
|
});
|
|
|
|
test("regex matcher", () => {
|
|
expect(applyMatcher("v2.1.0", { regex: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
|
|
expect(applyMatcher("v2.1", { regex: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
|
|
expect(applyMatcher("abc123", { regex: "^\\w+\\d+$" })).toBe(true);
|
|
});
|
|
|
|
test("empty 操作符", () => {
|
|
expect(applyMatcher("", { empty: true })).toBe(true);
|
|
expect(applyMatcher(null, { empty: true })).toBe(true);
|
|
expect(applyMatcher(undefined, { empty: true })).toBe(true);
|
|
expect(applyMatcher([], { empty: true })).toBe(true);
|
|
expect(applyMatcher({}, { empty: true })).toBe(true);
|
|
expect(applyMatcher("ok", { empty: true })).toBe(false);
|
|
expect(applyMatcher(0, { empty: true })).toBe(false);
|
|
expect(applyMatcher(false, { empty: true })).toBe(false);
|
|
expect(applyMatcher([1, 2], { empty: false })).toBe(true);
|
|
expect(applyMatcher([], { empty: false })).toBe(false);
|
|
});
|
|
|
|
test("exists 操作符", () => {
|
|
expect(applyMatcher("ok", { exists: true })).toBe(true);
|
|
expect(applyMatcher(null, { exists: true })).toBe(true);
|
|
expect(applyMatcher(undefined, { exists: true })).toBe(false);
|
|
expect(applyMatcher(undefined, { exists: false })).toBe(true);
|
|
expect(applyMatcher("ok", { exists: false })).toBe(false);
|
|
});
|
|
|
|
test("gte 操作符", () => {
|
|
expect(applyMatcher(10, { gte: 5 })).toBe(true);
|
|
expect(applyMatcher(5, { gte: 5 })).toBe(true);
|
|
expect(applyMatcher(3, { gte: 5 })).toBe(false);
|
|
expect(applyMatcher("10", { gte: 5 })).toBe(true);
|
|
});
|
|
|
|
test("lte 操作符", () => {
|
|
expect(applyMatcher(3, { lte: 5 })).toBe(true);
|
|
expect(applyMatcher(5, { lte: 5 })).toBe(true);
|
|
expect(applyMatcher(10, { lte: 5 })).toBe(false);
|
|
});
|
|
|
|
test("gt 操作符", () => {
|
|
expect(applyMatcher(10, { gt: 5 })).toBe(true);
|
|
expect(applyMatcher(5, { gt: 5 })).toBe(false);
|
|
});
|
|
|
|
test("lt 操作符", () => {
|
|
expect(applyMatcher(3, { lt: 5 })).toBe(true);
|
|
expect(applyMatcher(5, { lt: 5 })).toBe(false);
|
|
});
|
|
|
|
test("多操作符 AND 组合", () => {
|
|
expect(applyMatcher(7, { gte: 5, lte: 10 })).toBe(true);
|
|
expect(applyMatcher(3, { gte: 5, lte: 10 })).toBe(false);
|
|
expect(applyMatcher(15, { gte: 5, lte: 10 })).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("checkExpectValue", () => {
|
|
test("原始值直接比较", () => {
|
|
expect(checkExpectValue("ok", "ok")).toBe(true);
|
|
expect(checkExpectValue("ok", "error")).toBe(false);
|
|
expect(checkExpectValue(42, 42)).toBe(true);
|
|
expect(checkExpectValue(null, null)).toBe(true);
|
|
});
|
|
|
|
test("对象作为操作符", () => {
|
|
expect(checkExpectValue(42, { gte: 10 })).toBe(true);
|
|
expect(checkExpectValue(42, { gte: 100 })).toBe(false);
|
|
expect(checkExpectValue("hello", { contains: "ell" })).toBe(true);
|
|
});
|
|
});
|