- 重命名 ContentRules→ContentExpectations, KeyValueExpect→KeyedExpectations - 新增 Raw/Resolved 双层模型:resolve 阶段物化为执行计划,store 持久化 Raw 快照 - HTTP body 按需读取:status/headers 失败或无 body expectation 时不读取 body - 新增 displayValueExpectation() 解包 failure.expected 用户可读展示 - 修复 checkEarlyTimeout 独立 lte/lt 检查,修复 KeyedExpectations JSON Schema - 新增 expect/value.ts(resolve/check/display)、keyed.ts、content.ts、headers.ts、status.ts - 删除旧 normalize.ts/matcher.ts/validate-matcher.ts/key-value.ts - 更新 DEVELOPMENT.md:expect 五层管线表、displayValueExpectation、1.7↔1.10 交叉引用 - 同步 13 个 main specs,归档 refactor-expect-type-model 变更(62/62 tasks)
176 lines
6.5 KiB
TypeScript
176 lines
6.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import {
|
|
applyValueMatcher,
|
|
checkValueExpectation,
|
|
displayValueExpectation,
|
|
evaluateJsonPath,
|
|
resolveValueExpectation,
|
|
} from "../../../../../src/server/checker/expect/value";
|
|
|
|
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("applyValueMatcher", () => {
|
|
test("equals 操作符", () => {
|
|
expect(applyValueMatcher("ok", { equals: "ok" })).toBe(true);
|
|
expect(applyValueMatcher("ok", { equals: "error" })).toBe(false);
|
|
expect(applyValueMatcher(42, { equals: 42 })).toBe(true);
|
|
expect(applyValueMatcher(42, { equals: 41 })).toBe(false);
|
|
expect(applyValueMatcher(null, { equals: null })).toBe(true);
|
|
expect(applyValueMatcher(true, { equals: true })).toBe(true);
|
|
});
|
|
|
|
test("equals 支持 JSON 对象和数组", () => {
|
|
expect(applyValueMatcher({ status: "ok" }, { equals: { status: "ok" } })).toBe(true);
|
|
expect(applyValueMatcher({ status: "ok" }, { equals: { status: "fail" } })).toBe(false);
|
|
expect(applyValueMatcher(["a", "b"], { equals: ["a", "b"] })).toBe(true);
|
|
expect(applyValueMatcher(["a", "b"], { equals: ["b", "a"] })).toBe(false);
|
|
});
|
|
|
|
test("contains 操作符", () => {
|
|
expect(applyValueMatcher("hello world", { contains: "hello" })).toBe(true);
|
|
expect(applyValueMatcher("hello world", { contains: "missing" })).toBe(false);
|
|
expect(applyValueMatcher(12345, { contains: "23" })).toBe(true);
|
|
});
|
|
|
|
test("regex matcher", () => {
|
|
expect(applyValueMatcher("v2.1.0", { regex: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
|
|
expect(applyValueMatcher("v2.1", { regex: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
|
|
expect(applyValueMatcher("abc123", { regex: "^\\w+\\d+$" })).toBe(true);
|
|
});
|
|
|
|
test("empty 操作符", () => {
|
|
expect(applyValueMatcher("", { empty: true })).toBe(true);
|
|
expect(applyValueMatcher(null, { empty: true })).toBe(true);
|
|
expect(applyValueMatcher(undefined, { empty: true })).toBe(true);
|
|
expect(applyValueMatcher([], { empty: true })).toBe(true);
|
|
expect(applyValueMatcher({}, { empty: true })).toBe(true);
|
|
expect(applyValueMatcher("ok", { empty: true })).toBe(false);
|
|
expect(applyValueMatcher(0, { empty: true })).toBe(false);
|
|
expect(applyValueMatcher(false, { empty: true })).toBe(false);
|
|
expect(applyValueMatcher([1, 2], { empty: false })).toBe(true);
|
|
expect(applyValueMatcher([], { empty: false })).toBe(false);
|
|
});
|
|
|
|
test("exists 操作符", () => {
|
|
expect(applyValueMatcher("ok", { exists: true })).toBe(true);
|
|
expect(applyValueMatcher(null, { exists: true })).toBe(true);
|
|
expect(applyValueMatcher(undefined, { exists: true })).toBe(false);
|
|
expect(applyValueMatcher(undefined, { exists: false })).toBe(true);
|
|
expect(applyValueMatcher("ok", { exists: false })).toBe(false);
|
|
});
|
|
|
|
test("gte 操作符", () => {
|
|
expect(applyValueMatcher(10, { gte: 5 })).toBe(true);
|
|
expect(applyValueMatcher(5, { gte: 5 })).toBe(true);
|
|
expect(applyValueMatcher(3, { gte: 5 })).toBe(false);
|
|
expect(applyValueMatcher("10", { gte: 5 })).toBe(true);
|
|
});
|
|
|
|
test("lte 操作符", () => {
|
|
expect(applyValueMatcher(3, { lte: 5 })).toBe(true);
|
|
expect(applyValueMatcher(5, { lte: 5 })).toBe(true);
|
|
expect(applyValueMatcher(10, { lte: 5 })).toBe(false);
|
|
});
|
|
|
|
test("gt 操作符", () => {
|
|
expect(applyValueMatcher(10, { gt: 5 })).toBe(true);
|
|
expect(applyValueMatcher(5, { gt: 5 })).toBe(false);
|
|
});
|
|
|
|
test("lt 操作符", () => {
|
|
expect(applyValueMatcher(3, { lt: 5 })).toBe(true);
|
|
expect(applyValueMatcher(5, { lt: 5 })).toBe(false);
|
|
});
|
|
|
|
test("多操作符 AND 组合", () => {
|
|
expect(applyValueMatcher(7, { gte: 5, lte: 10 })).toBe(true);
|
|
expect(applyValueMatcher(3, { gte: 5, lte: 10 })).toBe(false);
|
|
expect(applyValueMatcher(15, { gte: 5, lte: 10 })).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("resolveValueExpectation", () => {
|
|
test("原始值解析为 equals matcher", () => {
|
|
expect(resolveValueExpectation("ok")).toEqual({ equals: "ok" });
|
|
expect(resolveValueExpectation(42)).toEqual({ equals: 42 });
|
|
expect(resolveValueExpectation(null)).toEqual({ equals: null });
|
|
expect(resolveValueExpectation(true)).toEqual({ equals: true });
|
|
});
|
|
|
|
test("对象 matcher 原样保留", () => {
|
|
const matcher = { gte: 10 };
|
|
expect(resolveValueExpectation(matcher)).toBe(matcher);
|
|
expect(resolveValueExpectation({ contains: "ell" })).toEqual({ contains: "ell" });
|
|
});
|
|
|
|
test("undefined 返回 undefined", () => {
|
|
expect(resolveValueExpectation(undefined)).toBeUndefined();
|
|
});
|
|
|
|
test("failure expected 使用用户可读的 equals 值", () => {
|
|
const result = checkValueExpectation("actual", resolveValueExpectation("expected"), {
|
|
path: "finishReason",
|
|
phase: "finishReason",
|
|
});
|
|
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.expected).toBe("expected");
|
|
});
|
|
|
|
test("displayValueExpectation 保留多 matcher 对象", () => {
|
|
expect(displayValueExpectation({ contains: "ok", regex: "^ok$" })).toEqual({ contains: "ok", regex: "^ok$" });
|
|
});
|
|
});
|