1
0
Files
DiAL/tests/server/checker/runner/http/expect.test.ts
lanyuanxiaoyao 7a635a0a9f refactor: 统一 expect 断言体系,引入共享 ValueMatcher/ContentRules/KeyValueExpect 模型
- 引入共享 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
2026-05-19 14:24:27 +08:00

109 lines
3.6 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { checkHeaders, checkStatus } from "../../../../../src/server/checker/runner/http/expect";
describe("checkHeaders", () => {
test("未配置 headers expect 时匹配成功", () => {
const result = checkHeaders({});
expect(result.matched).toBe(true);
expect(result.failure).toBeNull();
});
test("字符串格式按等值匹配", () => {
const headers = { "content-type": "application/json", "x-api": "v1" };
expect(checkHeaders(headers, { "content-type": "application/json" }).matched).toBe(true);
expect(checkHeaders(headers, { "content-type": "text/html" }).matched).toBe(false);
});
test("header 名称按小写响应头匹配", () => {
const headers = { "content-type": "application/json" };
expect(checkHeaders(headers, { "Content-Type": "application/json" }).matched).toBe(true);
});
test("操作符格式匹配", () => {
const headers = { "content-type": "application/json" };
expect(checkHeaders(headers, { "content-type": { contains: "json" } }).matched).toBe(true);
expect(checkHeaders(headers, { "content-type": { regex: "^application/" } }).matched).toBe(true);
expect(checkHeaders(headers, { "content-type": { contains: "xml" } }).matched).toBe(false);
});
test("缺失 header 默认返回失败", () => {
const result = checkHeaders({}, { "x-missing": "value" });
expect(result.matched).toBe(false);
expect(result.failure!.phase).toBe("headers");
expect(result.failure!.kind).toBe("mismatch");
});
test("缺失 header 且 exists=false 时匹配成功", () => {
const result = checkHeaders({}, { "x-missing": { exists: false } });
expect(result.matched).toBe(true);
expect(result.failure).toBeNull();
});
});
describe("checkStatus 范围匹配", () => {
test("无 expect 配置时默认 status [200] 可由调用方使用 checkStatus 表达", () => {
const result = checkStatus(200, [200]);
expect(result.matched).toBe(true);
expect(result.failure).toBeNull();
});
test("status 不匹配返回 phase=status 的失败", () => {
const result = checkStatus(503, [200]);
expect(result.matched).toBe(false);
expect(result.failure!.phase).toBe("status");
expect(result.failure!.expected).toEqual([200]);
expect(result.failure!.actual).toBe(503);
});
test("2xx 范围匹配 200", () => {
expect(checkStatus(200, ["2xx"]).matched).toBe(true);
});
test("2xx 范围匹配 204", () => {
expect(checkStatus(204, ["2xx"]).matched).toBe(true);
});
test("2xx 范围不匹配 301", () => {
expect(checkStatus(301, ["2xx"]).matched).toBe(false);
});
test("5xx 范围匹配 503", () => {
expect(checkStatus(503, ["5xx"]).matched).toBe(true);
});
test("混合精确值与范围模式命中精确值", () => {
expect(checkStatus(301, ["2xx", 301]).matched).toBe(true);
});
test("混合精确值与范围模式命中范围", () => {
expect(checkStatus(204, ["2xx", 301]).matched).toBe(true);
});
test("混合模式都不匹配", () => {
expect(checkStatus(404, ["2xx", 301]).matched).toBe(false);
});
test("纯精确值仍正常工作", () => {
expect(checkStatus(200, [200, 201]).matched).toBe(true);
expect(checkStatus(404, [200, 201]).matched).toBe(false);
});
test("1xx 范围匹配 101", () => {
expect(checkStatus(101, ["1xx"]).matched).toBe(true);
});
test("3xx 范围匹配 301", () => {
expect(checkStatus(301, ["3xx"]).matched).toBe(true);
});
test("4xx 范围匹配 404", () => {
expect(checkStatus(404, ["4xx"]).matched).toBe(true);
});
});