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
This commit is contained in:
@@ -84,6 +84,69 @@ describe("config contract", () => {
|
||||
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,
|
||||
@@ -144,4 +207,71 @@ describe("config contract", () => {
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user