1
0
Files
DiAL/tests/server/checker/config-contract/validate.test.ts
lanyuanxiaoyao cf847ccd7a feat: 重构配置生命周期为 Authoring/Normalized/Resolved 三层
将变量替换和 expect 简写展开统一放入 Normalized 阶段,
运行时 AJV 使用 Normalized schema,导出 schema 面向 Authoring Config。

主要变更:
- 新增 normalizer.ts 实现 normalizeAuthoringConfig()
- 拆分 Authoring/Normalized 双 schema,checker 接口支持 authoring/normalized 片段
- config-loader 流程:normalize → Normalized AJV → semantic → resolve
- validator 兼容层自动分派 raw/normalized expect 形态
- 删除 rawExpect,store.expect 列写入 null
- Authoring schema 对 integer/boolean/enum 字段接受变量引用
- 修复 DB/HTTP validate 入口守卫和 LLM options integer 变量引用
- 优化 compact() 避免 undefined 覆盖隐患
- 移除 content.ts 恒为 true 的前置条件
- 同步 5 个主规范并归档 change
2026-05-22 14:00:47 +08:00

278 lines
8.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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("Authoring schema 接受变量引用Normalized schema 拒绝变量引用和 primitive 简写", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const registry = createDefaultCheckerRegistry();
const authoring = ajv.compile(createProbeConfigJsonSchema(registry));
const normalizedConfig = {
targets: [
{
expect: { durationMs: 5000 },
http: { maxRedirects: "${MAX|5}", url: "https://example.com" },
id: "api",
type: "http",
},
],
};
expect(
authoring({
targets: [
{
expect: { durationMs: 5000, status: ["${STATUS}"] },
http: { ignoreSSL: "${SSL|false}", maxRedirects: "${MAX|5}", url: "https://example.com" },
id: "api",
type: "http",
},
],
}),
).toBe(false);
expect(
authoring({
targets: [
{
expect: { durationMs: 5000 },
http: {
ignoreSSL: "${SSL|false}",
maxRedirects: "${MAX|5}",
method: "${METHOD|GET}",
url: "https://example.com",
},
id: "api",
type: "http",
},
{
id: "llm-api",
llm: {
model: "gpt-4o-mini",
prompt: "ping",
provider: "${P|openai}",
url: "https://example.com/v1/chat/completions",
},
type: "llm",
},
],
}),
).toBe(true);
expect(validateProbeConfigContract(normalizedConfig, registry).config).toBeNull();
});
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 正则不合法');
});
test("Authoring schema 接受 server.listen.port、llm.options.maxOutputTokens 变量引用", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const validate = ajv.compile(createProbeConfigJsonSchema(createDefaultCheckerRegistry()));
expect(
validate({
server: { listen: { port: "${SERVER_PORT|3000}" } },
targets: [
{
id: "llm",
llm: {
model: "gpt-4o-mini",
options: { maxOutputTokens: "${MAX_TOKENS|100}" },
prompt: "ping",
provider: "openai",
url: "https://example.com/v1/chat/completions",
},
type: "llm",
},
],
}),
).toBe(true);
});
test("Authoring schema 拒绝 expect.status 和 expect.exitCode 数组内的变量引用", () => {
const ajv = new Ajv({
allErrors: true,
coerceTypes: false,
removeAdditional: false,
strict: true,
useDefaults: false,
});
const validate = ajv.compile(createProbeConfigJsonSchema(createDefaultCheckerRegistry()));
expect(
validate({
targets: [
{
expect: { status: ["${STATUS}"] },
http: { url: "https://example.com" },
id: "http",
type: "http",
},
],
}),
).toBe(false);
expect(
validate({
targets: [
{
cmd: { exec: "true" },
expect: { exitCode: ["${CODE}"] },
id: "cmd",
type: "cmd",
},
],
}),
).toBe(false);
});
});