1
0
Files
DiAL/tests/server/checker/config-contract/validate.test.ts
lanyuanxiaoyao 60a54b483f 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)
2026-05-20 16:12:48 +08:00

148 lines
5.0 KiB
TypeScript

import Ajv from "ajv";
import { describe, expect, test } from "bun:test";
import { createDefaultCheckerRegistry } from "../../../../src/server/checker/runner";
import { createProbeConfigJsonSchema } from "../../../../src/server/checker/schema/export";
import { formatConfigIssues, issue } from "../../../../src/server/checker/schema/issues";
import { validateProbeConfigContract } from "../../../../src/server/checker/schema/validate";
describe("config contract", () => {
test("导出的 probe-config.schema.json 与 fragments 生成结果一致", async () => {
const expected = `${JSON.stringify(createProbeConfigJsonSchema(createDefaultCheckerRegistry()), null, 2)}\n`;
const actual = await Bun.file("probe-config.schema.json").text();
expect(actual).toBe(expected);
});
test("导出 schema 拒绝未知字段和小写 HTTP method", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const validate = ajv.compile(createProbeConfigJsonSchema(createDefaultCheckerRegistry()));
expect(
validate({
targets: [
{
http: { method: "get", unknownHttpField: true, url: "https://example.com" },
id: "api",
name: "api",
type: "http",
},
],
}),
).toBe(false);
});
test("导出 schema 支持 variables 且要求 target id", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const validate = ajv.compile(createProbeConfigJsonSchema(createDefaultCheckerRegistry()));
expect(
validate({
targets: [{ http: { url: "https://example.com" }, id: "api", type: "http" }],
variables: { base_url: "https://example.com", enabled: true, port: 443 },
}),
).toBe(true);
expect(
validate({
targets: [{ http: { url: "https://example.com" }, type: "http" }],
variables: { bad: null },
}),
).toBe(false);
});
test("导出 schema 支持 ValueMatcher primitive 简写且拒绝数组对象简写", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const validate = ajv.compile(createProbeConfigJsonSchema(createDefaultCheckerRegistry()));
const target = (durationMs: unknown) => ({
targets: [{ expect: { durationMs }, http: { url: "https://example.com" }, id: "api", type: "http" }],
});
expect(validate(target(5000))).toBe(true);
expect(validate(target("5000"))).toBe(true);
expect(validate(target(null))).toBe(true);
expect(validate(target([1, 2]))).toBe(false);
expect(validate(target({ foo: "bar" }))).toBe(false);
expect(validate(target({ equals: [1, 2] }))).toBe(true);
expect(validate(target({ equals: { status: "ok" } }))).toBe(true);
});
test("导出 schema 拒绝 KeyedExpectations 的数组和对象简写", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const validate = ajv.compile(createProbeConfigJsonSchema(createDefaultCheckerRegistry()));
const target = (headerValue: unknown) => ({
targets: [
{
expect: { headers: { "x-test": headerValue } },
http: { url: "https://example.com" },
id: "api",
type: "http",
},
],
});
expect(validate(target("ok"))).toBe(true);
expect(validate(target({ contains: "ok" }))).toBe(true);
expect(validate(target(["ok"]))).toBe(false);
expect(validate(target({ nested: "ok" }))).toBe(false);
expect(validate(target({ equals: { nested: "ok" } }))).toBe(true);
});
test("Ajv 错误转换为中文结构化 issue", () => {
const result = validateProbeConfigContract(
{
targets: [
{
group: 123,
http: { extra: true },
id: "api",
name: "api",
type: "http",
},
],
unknownRoot: true,
},
createDefaultCheckerRegistry(),
);
expect(result.config).toBeNull();
const message = formatConfigIssues(result.issues);
expect(message).toContain("unknownRoot 是未知字段");
expect(message).toContain('target "api" 的 group 类型不合法');
expect(message).toContain('target "api" 的 http.url 缺少必填字段');
expect(message).toContain('target "api" 的 http.extra 是未知字段');
});
test("ConfigValidationIssue 聚合渲染保留契约和语义错误", () => {
const message = formatConfigIssues([
issue("unknown-field", "targets[0].http.extra", "是未知字段", "api"),
issue("invalid-regex", "targets[0].expect.body[0].regex", "正则不合法", "api"),
]);
expect(message).toBe('target "api" 的 http.extra 是未知字段\ntarget "api" 的 expect.body[0].regex 正则不合法');
});
});