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,12 +1,15 @@
import { describe, expect, test } from "bun:test";
import { checkContentRules } from "../../../../../src/server/checker/expect/content";
import type { RawContentExpectations } from "../../../../../src/server/checker/expect/types";
function checkBodyExpect(body: string, rules?: Parameters<typeof checkContentRules>[1]) {
return checkContentRules(body, rules, { path: "body", phase: "body" });
import { checkContentExpectations, resolveContentExpectations } from "../../../../../src/server/checker/expect/content";
function checkBodyExpect(body: string, rawExpectations?: RawContentExpectations) {
const resolved = resolveContentExpectations(rawExpectations);
return checkContentExpectations(body, resolved, { path: "body", phase: "body" });
}
describe("checkBodyExpect (BodyRule[])", () => {
describe("checkBodyExpect (ContentExpectations)", () => {
test("无规则返回匹配成功", () => {
const r = checkBodyExpect("anything");
expect(r.matched).toBe(true);
@@ -19,6 +22,22 @@ describe("checkBodyExpect (BodyRule[])", () => {
expect(r.failure).toBeNull();
});
test("resolve 输出显式 kind union 并物化 extractor 默认 exists", () => {
expect(
resolveContentExpectations([
{ contains: "ok" },
{ json: { path: "$.status" } },
{ css: { attr: "content", selector: "meta[name=status]" } },
{ xpath: { path: "/root/status" } },
]),
).toEqual([
{ kind: "value", matcher: { contains: "ok" } },
{ kind: "json", matcher: { exists: true }, path: "$.status" },
{ attr: "content", kind: "css", matcher: { exists: true }, selector: "meta[name=status]" },
{ kind: "xpath", matcher: { exists: true }, path: "/root/status" },
]);
});
test("contains 规则匹配成功", () => {
const r = checkBodyExpect("hello world", [{ contains: "hello" }]);
expect(r.matched).toBe(true);

View File

@@ -0,0 +1,65 @@
import { describe, expect, test } from "bun:test";
import type { CheckerValidationInput } from "../../../../../src/server/checker/runner/types";
import { validateDbConfig } from "../../../../../src/server/checker/runner/db/validate";
import { validateHttpConfig } from "../../../../../src/server/checker/runner/http/validate";
import { validateLlmConfig } from "../../../../../src/server/checker/runner/llm/validate";
function input(target: Record<string, unknown>): CheckerValidationInput {
return { defaults: {}, targets: [target as CheckerValidationInput["targets"][number]] };
}
describe("HTTP/LLM headers reject case-insensitive duplicate keys", () => {
test("HTTP headers 大小写不同的重复 key 报错", () => {
const target = {
expect: { headers: { "Content-Type": "application/json", "content-type": "text/plain" } },
http: { url: "https://example.com" },
id: "dup",
type: "http",
};
const issues = validateHttpConfig(input(target));
expect(issues.some((i) => i.code === "duplicate-key" && i.path.includes("headers"))).toBe(true);
});
test("LLM headers 大小写不同的重复 key 报错", () => {
const target = {
expect: { headers: { "X-Trace": "a", "x-trace": "b" } },
id: "dup",
llm: {
mode: "stream",
model: "test-model",
prompt: "hello",
provider: "openai",
url: "https://example.com/v1/chat/completions",
},
type: "llm",
};
const issues = validateLlmConfig(input(target));
expect(issues.some((i) => i.code === "duplicate-key" && i.path.includes("headers"))).toBe(true);
});
test("HTTP headers 不同 key 不触发 duplicate-key", () => {
const target = {
expect: { headers: { Accept: "application/json", "Content-Type": "application/json" } },
http: { url: "https://example.com" },
id: "ok",
type: "http",
};
expect(validateHttpConfig(input(target)).some((i) => i.code === "duplicate-key")).toBe(false);
});
test("DB rows 保留大小写敏感不触发 duplicate-key", () => {
const target = {
db: { query: "SELECT 1", url: "sqlite://:memory:" },
expect: { rows: [{ Name: "a", name: "b" }] },
id: "dup-rows",
type: "db",
};
expect(validateDbConfig(input(target)).some((i) => i.code === "duplicate-key")).toBe(false);
});
});

View File

@@ -1,9 +1,9 @@
import { describe, expect, test } from "bun:test";
import { checkValueMatcher } from "../../../../../src/server/checker/expect/matcher";
import { checkValueExpectation } from "../../../../../src/server/checker/expect/value";
function checkDuration(durationMs: number, maxDurationMs?: number) {
return checkValueMatcher(durationMs, maxDurationMs === undefined ? undefined : { lte: maxDurationMs }, {
return checkValueExpectation(durationMs, maxDurationMs === undefined ? undefined : { lte: maxDurationMs }, {
path: "durationMs",
phase: "duration",
});

View File

@@ -0,0 +1,34 @@
import { describe, expect, test } from "bun:test";
import { checkKeyedExpectations, resolveKeyedExpectations } from "../../../../../src/server/checker/expect/keyed";
describe("KeyedExpectations", () => {
test("resolve 将 Raw Record 转为保持顺序的有序数组", () => {
expect(resolveKeyedExpectations({ Count: { gte: 1 }, Name: "alice" })).toEqual([
{ key: "Count", matcher: { gte: 1 } },
{ key: "Name", matcher: { equals: "alice" } },
]);
});
test("failure.path 包含基础路径和原始 key", () => {
const result = checkKeyedExpectations(
{ "content-type": "text/plain" },
resolveKeyedExpectations({ "Content-Type": { contains: "json" } }),
{ normalizeKey: (key) => key.toLowerCase(), path: "headers", phase: "headers" },
);
expect(result.matched).toBe(false);
expect(result.failure?.path).toBe("headers.Content-Type");
});
test("DB rows 默认保持大小写敏感匹配", () => {
const expectations = resolveKeyedExpectations({ Name: "alice", name: "bob" });
expect(
checkKeyedExpectations({ Name: "alice", name: "bob" }, expectations, { path: "rows[0]", phase: "rows" }).matched,
).toBe(true);
expect(
checkKeyedExpectations({ Name: "bob", name: "alice" }, expectations, { path: "rows[0]", phase: "rows" }).matched,
).toBe(false);
});
});

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$" });
});
});

View File

@@ -1,9 +1,12 @@
import { describe, expect, test } from "bun:test";
import { checkContentRules } from "../../../../../src/server/checker/expect/content";
import type { RawContentExpectations } from "../../../../../src/server/checker/expect/types";
function checkTextRules(text: string, rules: Parameters<typeof checkContentRules>[1], phase: string) {
return checkContentRules(text, rules, { path: phase, phase });
import { checkContentExpectations, resolveContentExpectations } from "../../../../../src/server/checker/expect/content";
function checkTextRules(text: string, rawRules: RawContentExpectations, phase: string) {
const resolved = resolveContentExpectations(rawRules);
return checkContentExpectations(text, resolved, { path: phase, phase });
}
describe("checkTextRules", () => {

View File

@@ -15,7 +15,7 @@ function input(target: Record<string, unknown>): CheckerValidationInput {
}
describe("ValueMatcher primitive shorthand in checker validators", () => {
test("normalizes shorthand for all checker ValueMatcher fields", () => {
test("accepts shorthand for all checker ValueMatcher fields", () => {
const targets = [
{
expect: { durationMs: 100 },
@@ -82,9 +82,10 @@ describe("ValueMatcher primitive shorthand in checker validators", () => {
for (const target of targets) {
const { validate, ...config } = target;
const original = structuredClone(config);
expect(validate(input(config))).toHaveLength(0);
expect((config.expect as Record<string, unknown>)["durationMs"]).toEqual({ equals: 100 });
expect(config).toEqual(original);
}
});