1
0

refactor: expect 类型模型重构,Raw/Resolved 双层分离与断言基础设施内聚

- 重命名 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)
This commit is contained in:
2026-05-20 16:12:48 +08:00
parent 6098be2d9e
commit 60a54b483f
90 changed files with 2487 additions and 1493 deletions

View File

@@ -1,6 +1,12 @@
import { describe, expect, test } from "bun:test";
import { applyMatcher, checkExpectValue, evaluateJsonPath } from "../../../../../src/server/checker/expect/matcher";
import {
applyValueMatcher,
checkValueExpectation,
displayValueExpectation,
evaluateJsonPath,
resolveValueExpectation,
} from "../../../../../src/server/checker/expect/value";
describe("evaluateJsonPath", () => {
const obj = {
@@ -55,97 +61,115 @@ describe("evaluateJsonPath", () => {
});
});
describe("applyMatcher", () => {
describe("applyValueMatcher", () => {
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);
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(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);
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(applyMatcher("hello world", { contains: "hello" })).toBe(true);
expect(applyMatcher("hello world", { contains: "missing" })).toBe(false);
expect(applyMatcher(12345, { contains: "23" })).toBe(true);
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(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);
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(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);
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(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);
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(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);
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(applyMatcher(3, { lte: 5 })).toBe(true);
expect(applyMatcher(5, { lte: 5 })).toBe(true);
expect(applyMatcher(10, { lte: 5 })).toBe(false);
expect(applyValueMatcher(3, { lte: 5 })).toBe(true);
expect(applyValueMatcher(5, { lte: 5 })).toBe(true);
expect(applyValueMatcher(10, { lte: 5 })).toBe(false);
});
test("gt 操作符", () => {
expect(applyMatcher(10, { gt: 5 })).toBe(true);
expect(applyMatcher(5, { gt: 5 })).toBe(false);
expect(applyValueMatcher(10, { gt: 5 })).toBe(true);
expect(applyValueMatcher(5, { gt: 5 })).toBe(false);
});
test("lt 操作符", () => {
expect(applyMatcher(3, { lt: 5 })).toBe(true);
expect(applyMatcher(5, { lt: 5 })).toBe(false);
expect(applyValueMatcher(3, { lt: 5 })).toBe(true);
expect(applyValueMatcher(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);
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("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);
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("对象作为操作符", () => {
expect(checkExpectValue(42, { gte: 10 })).toBe(true);
expect(checkExpectValue(42, { gte: 100 })).toBe(false);
expect(checkExpectValue("hello", { contains: "ell" })).toBe(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$" });
});
});