1
0
Files
DiAL/tests/server/checker/runner/shared/operator.test.ts
lanyuanxiaoyao bb6b2bc20b refactor: checker 模块内聚化 — 每个 checker 自包含于独立目录
将 checker 架构重构为完全内聚模式:每个 checker 目录包含自身的
types、schema、validate、execute、expect 和 index,新增 checker
只需创建一个目录并在 runner/index.ts 添加一行注册。

主要变更:
- runner/shared/ 拆分:断言基础设施迁入 checker/expect/,
  body.ts 迁入 http/,text.ts 迁入 command/
- config-contract/ 重命名为 schema/,schema.ts → builder.ts
- size.ts + parseDuration 合并为 utils.ts
- 顶层 types.ts 改为 base interface + index signature,
  checker 专属类型下沉到各自 types.ts
- runner/index.ts 改为显式数组注册模式
- 更新 DEVELOPMENT.md 项目结构和开发新 Checker 指南
2026-05-13 14:38:21 +08:00

150 lines
5.5 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { applyOperator, checkExpectValue, evaluateJsonPath } from "../../../../../src/server/checker/expect/operator";
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("applyOperator", () => {
test("equals 操作符", () => {
expect(applyOperator("ok", { equals: "ok" })).toBe(true);
expect(applyOperator("ok", { equals: "error" })).toBe(false);
expect(applyOperator(42, { equals: 42 })).toBe(true);
expect(applyOperator(42, { equals: 41 })).toBe(false);
expect(applyOperator(null, { equals: null })).toBe(true);
expect(applyOperator(true, { equals: true })).toBe(true);
});
test("equals 支持 JSON 对象和数组", () => {
expect(applyOperator({ status: "ok" }, { equals: { status: "ok" } })).toBe(true);
expect(applyOperator({ status: "ok" }, { equals: { status: "fail" } })).toBe(false);
expect(applyOperator(["a", "b"], { equals: ["a", "b"] })).toBe(true);
expect(applyOperator(["a", "b"], { equals: ["b", "a"] })).toBe(false);
});
test("contains 操作符", () => {
expect(applyOperator("hello world", { contains: "hello" })).toBe(true);
expect(applyOperator("hello world", { contains: "missing" })).toBe(false);
expect(applyOperator(12345, { contains: "23" })).toBe(true);
});
test("match 操作符", () => {
expect(applyOperator("v2.1.0", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
expect(applyOperator("v2.1", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
expect(applyOperator("abc123", { match: "^\\w+\\d+$" })).toBe(true);
});
test("empty 操作符", () => {
expect(applyOperator("", { empty: true })).toBe(true);
expect(applyOperator(null, { empty: true })).toBe(true);
expect(applyOperator(undefined, { empty: true })).toBe(true);
expect(applyOperator([], { empty: true })).toBe(true);
expect(applyOperator({}, { empty: true })).toBe(true);
expect(applyOperator("ok", { empty: true })).toBe(false);
expect(applyOperator([1, 2], { empty: false })).toBe(true);
expect(applyOperator([], { empty: false })).toBe(false);
});
test("exists 操作符", () => {
expect(applyOperator("ok", { exists: true })).toBe(true);
expect(applyOperator(null, { exists: true })).toBe(true);
expect(applyOperator(undefined, { exists: true })).toBe(false);
expect(applyOperator(undefined, { exists: false })).toBe(true);
expect(applyOperator("ok", { exists: false })).toBe(false);
});
test("gte 操作符", () => {
expect(applyOperator(10, { gte: 5 })).toBe(true);
expect(applyOperator(5, { gte: 5 })).toBe(true);
expect(applyOperator(3, { gte: 5 })).toBe(false);
expect(applyOperator("10", { gte: 5 })).toBe(true);
});
test("lte 操作符", () => {
expect(applyOperator(3, { lte: 5 })).toBe(true);
expect(applyOperator(5, { lte: 5 })).toBe(true);
expect(applyOperator(10, { lte: 5 })).toBe(false);
});
test("gt 操作符", () => {
expect(applyOperator(10, { gt: 5 })).toBe(true);
expect(applyOperator(5, { gt: 5 })).toBe(false);
});
test("lt 操作符", () => {
expect(applyOperator(3, { lt: 5 })).toBe(true);
expect(applyOperator(5, { lt: 5 })).toBe(false);
});
test("多操作符 AND 组合", () => {
expect(applyOperator(7, { gte: 5, lte: 10 })).toBe(true);
expect(applyOperator(3, { gte: 5, lte: 10 })).toBe(false);
expect(applyOperator(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);
});
});