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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -911,11 +911,6 @@ targets:
|
||||
const config = await loadConfig(configPath);
|
||||
const t = config.targets[0]!;
|
||||
if (t.type === "http") {
|
||||
expect(t.rawExpect).toEqual({
|
||||
body: [{ contains: "ok" }, { json: { equals: "ok", path: "$.status" } }],
|
||||
durationMs: { lte: 3000 },
|
||||
status: [200, 201],
|
||||
});
|
||||
expect(t.expect).toEqual({
|
||||
body: [
|
||||
{ kind: "value", matcher: { contains: "ok" } },
|
||||
@@ -952,12 +947,6 @@ targets:
|
||||
const config = await loadConfig(configPath);
|
||||
const t = config.targets[0]!;
|
||||
if (t.type === "cmd") {
|
||||
expect(t.rawExpect).toEqual({
|
||||
durationMs: { lte: 5000 },
|
||||
exitCode: [0, 2],
|
||||
stderr: [{ empty: true }],
|
||||
stdout: [{ contains: "ok" }, { regex: "done" }],
|
||||
});
|
||||
expect(t.expect).toEqual({
|
||||
durationMs: { lte: 5000 },
|
||||
exitCode: [0, 2],
|
||||
@@ -1291,7 +1280,7 @@ targets:
|
||||
equals: "ok"
|
||||
`,
|
||||
);
|
||||
await expectConfigLoadError(configPath, "json.path");
|
||||
await expectConfigLoadError(configPath, "path 必须为以");
|
||||
});
|
||||
|
||||
test("body css selector 为空抛出错误", async () => {
|
||||
@@ -1310,7 +1299,7 @@ targets:
|
||||
selector: ""
|
||||
`,
|
||||
);
|
||||
await expectConfigLoadError(configPath, "css.selector 必须为非空字符串");
|
||||
await expectConfigLoadError(configPath, "selector 必须为非空字符串");
|
||||
});
|
||||
|
||||
test("旧 match matcher 抛出错误", async () => {
|
||||
@@ -1329,7 +1318,7 @@ targets:
|
||||
match: "[invalid"
|
||||
`,
|
||||
);
|
||||
await expectConfigLoadError(configPath, "match 是未知 matcher");
|
||||
await expectConfigLoadError(configPath, "match 是未知字段");
|
||||
});
|
||||
|
||||
test("operator gte 非数字抛出错误", async () => {
|
||||
@@ -1410,7 +1399,7 @@ targets:
|
||||
path: ""
|
||||
`,
|
||||
);
|
||||
await expectConfigLoadError(configPath, "xpath.path 必须为非空字符串");
|
||||
await expectConfigLoadError(configPath, "path 必须为非空字符串");
|
||||
});
|
||||
|
||||
test("expect headers 非对象抛出错误", async () => {
|
||||
|
||||
@@ -183,7 +183,7 @@ describe("CommandChecker", () => {
|
||||
test("resolve 未配置 expect 时物化默认 exitCode", () => {
|
||||
const result = checker.resolve({ cmd: { exec: "true" }, id: "test", type: "cmd" }, makeResolveContext());
|
||||
|
||||
expect(result.rawExpect).toBeUndefined();
|
||||
expect("rawExpect" in result).toBe(false);
|
||||
expect(result.expect).toEqual({ exitCode: [0] });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -975,7 +975,7 @@ describe("HttpChecker.resolve", () => {
|
||||
makeResolveContext(),
|
||||
);
|
||||
|
||||
expect(result.rawExpect).toBeUndefined();
|
||||
expect("rawExpect" in result).toBe(false);
|
||||
expect(result.expect).toEqual({ status: [200] });
|
||||
});
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ describe("IcmpChecker resolve", () => {
|
||||
);
|
||||
expect(target.icmp).toEqual({ count: 3, host: "10.0.0.1", packetSize: 56 });
|
||||
expect(target.group).toBe("default");
|
||||
expect(target.rawExpect).toBeUndefined();
|
||||
expect("rawExpect" in target).toBe(false);
|
||||
expect(target.expect).toEqual({ alive: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -16,8 +16,10 @@ describe("LLM registry integration", () => {
|
||||
|
||||
test("llm checker schemas 有效", () => {
|
||||
const checker = checkerRegistry.get("llm");
|
||||
expect(checker.schemas.config).toBeDefined();
|
||||
expect(checker.schemas.expect).toBeDefined();
|
||||
expect(checker.schemas.authoring.config).toBeDefined();
|
||||
expect(checker.schemas.authoring.expect).toBeDefined();
|
||||
expect(checker.schemas.normalized.config).toBeDefined();
|
||||
expect(checker.schemas.normalized.expect).toBeDefined();
|
||||
});
|
||||
|
||||
test("llm checker validate 方法可用", () => {
|
||||
|
||||
@@ -60,9 +60,11 @@ describe("LlmChecker schema", () => {
|
||||
expect(checker?.configKey).toBe("llm");
|
||||
});
|
||||
|
||||
test("schemas 包含 config、expect", () => {
|
||||
test("schemas 包含 authoring 和 normalized config/expect", () => {
|
||||
expect(checker).toBeDefined();
|
||||
expect(Object.keys(checker!.schemas).sort()).toEqual(["config", "expect"].sort());
|
||||
expect(Object.keys(checker!.schemas).sort()).toEqual(["authoring", "normalized"].sort());
|
||||
expect(checker!.schemas.authoring.config).toBeDefined();
|
||||
expect(checker!.schemas.normalized.expect).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -249,13 +251,13 @@ describe("LlmChecker resolve", () => {
|
||||
expect(resolved.group).toBe("default");
|
||||
expect(resolved.intervalMs).toBe(30000);
|
||||
expect(resolved.timeoutMs).toBe(10000);
|
||||
expect(resolved.rawExpect).toBeUndefined();
|
||||
expect("rawExpect" in resolved).toBe(false);
|
||||
expect(resolved.expect).toEqual({ status: [200] });
|
||||
});
|
||||
|
||||
test("stream mode 未配置 expect.stream 时不物化 completed", () => {
|
||||
const raw = makeRawTarget({
|
||||
expect: { output: [{ contains: "OK" }] },
|
||||
expect: { output: [{ kind: "value", matcher: { contains: "OK" } }] },
|
||||
llm: {
|
||||
mode: "stream",
|
||||
model: "gpt-4o-mini",
|
||||
@@ -267,13 +269,13 @@ describe("LlmChecker resolve", () => {
|
||||
|
||||
const resolved = asLlm(checker.resolve(raw, makeResolveContext()));
|
||||
|
||||
expect(resolved.rawExpect).toEqual({ output: [{ contains: "OK" }] });
|
||||
expect("rawExpect" in resolved).toBe(false);
|
||||
expect(resolved.expect?.stream).toBeUndefined();
|
||||
});
|
||||
|
||||
test("配置 expect.stream 但省略 completed 时默认 true", () => {
|
||||
const raw = makeRawTarget({
|
||||
expect: { stream: { firstTokenMs: 100 } },
|
||||
expect: { stream: { firstTokenMs: { equals: 100 } } },
|
||||
llm: {
|
||||
mode: "stream",
|
||||
model: "gpt-4o-mini",
|
||||
@@ -285,7 +287,7 @@ describe("LlmChecker resolve", () => {
|
||||
|
||||
const resolved = asLlm(checker.resolve(raw, makeResolveContext()));
|
||||
|
||||
expect(resolved.rawExpect).toEqual({ stream: { firstTokenMs: 100 } });
|
||||
expect("rawExpect" in resolved).toBe(false);
|
||||
expect(resolved.expect?.stream).toEqual({ completed: true, firstTokenMs: { equals: 100 } });
|
||||
});
|
||||
|
||||
|
||||
@@ -14,8 +14,14 @@ function createChecker(type: string): Checker {
|
||||
execute: () => Promise.resolve<CheckResult>({} as unknown as CheckResult),
|
||||
resolve: () => ({}) as unknown as ResolvedTargetBase,
|
||||
schemas: {
|
||||
config: Type.Object({}, { additionalProperties: false }),
|
||||
expect: Type.Object({}, { additionalProperties: false }),
|
||||
authoring: {
|
||||
config: Type.Object({}, { additionalProperties: false }),
|
||||
expect: Type.Object({}, { additionalProperties: false }),
|
||||
},
|
||||
normalized: {
|
||||
config: Type.Object({}, { additionalProperties: false }),
|
||||
expect: Type.Object({}, { additionalProperties: false }),
|
||||
},
|
||||
},
|
||||
serialize: () => ({ config: "", target: "" }),
|
||||
type,
|
||||
@@ -68,7 +74,9 @@ describe("CheckerRegistry", () => {
|
||||
|
||||
expect(first.supportedTypes).toEqual(["http", "cmd", "db", "tcp", "icmp", "udp", "llm", "custom"]);
|
||||
expect(second.supportedTypes).toEqual(["http", "cmd", "db", "tcp", "icmp", "udp", "llm"]);
|
||||
expect(first.definitions.every((checker) => checker.schemas.config && checker.schemas.expect)).toBe(true);
|
||||
expect(
|
||||
first.definitions.every((checker) => checker.schemas.authoring.config && checker.schemas.normalized.expect),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("默认 registry 注册 icmp type", () => {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import type { CheckerValidationInput } from "../../../../../src/server/checker/runner/types";
|
||||
import type { RawTargetConfig } from "../../../../../src/server/checker/types";
|
||||
|
||||
import { normalizeAuthoringConfig } from "../../../../../src/server/checker/normalizer";
|
||||
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";
|
||||
@@ -52,14 +54,45 @@ describe("HTTP/LLM headers reject case-insensitive duplicate keys", () => {
|
||||
expect(validateHttpConfig(input(target)).some((i) => i.code === "duplicate-key")).toBe(false);
|
||||
});
|
||||
|
||||
test("DB rows 保留大小写敏感不触发 duplicate-key", () => {
|
||||
const target = {
|
||||
test("HTTP normalized headers 大小写不同的重复 key 报错", () => {
|
||||
const authoring = {
|
||||
expect: { headers: { "Content-Type": "application/json", "content-type": "text/plain" } },
|
||||
http: { url: "https://example.com" },
|
||||
id: "dup",
|
||||
type: "http",
|
||||
};
|
||||
|
||||
const result = normalizeAuthoringConfig({ targets: [authoring] });
|
||||
const normalized = result.config as { targets: RawTargetConfig[] };
|
||||
const issues = validateHttpConfig({ targets: normalized.targets });
|
||||
expect(issues.some((i) => i.code === "duplicate-key" && i.path.includes("headers"))).toBe(true);
|
||||
});
|
||||
|
||||
test("HTTP normalized headers unsafe regex 报错", () => {
|
||||
const authoring = {
|
||||
expect: { headers: { "x-test": { regex: "(\\d+)*x" } } },
|
||||
http: { url: "https://example.com" },
|
||||
id: "unsafe",
|
||||
type: "http",
|
||||
};
|
||||
|
||||
const result = normalizeAuthoringConfig({ targets: [authoring] });
|
||||
const normalized = result.config as { targets: RawTargetConfig[] };
|
||||
const issues = validateHttpConfig({ targets: normalized.targets });
|
||||
expect(issues.some((i) => i.code === "unsafe-regex" && i.path.includes("headers"))).toBe(true);
|
||||
});
|
||||
|
||||
test("DB normalized rows 通过校验", () => {
|
||||
const authoring = {
|
||||
db: { query: "SELECT 1", url: "sqlite://:memory:" },
|
||||
expect: { rows: [{ Name: "a", name: "b" }] },
|
||||
id: "dup-rows",
|
||||
expect: { rows: [{ Name: "Alice" }] },
|
||||
id: "db-rows",
|
||||
type: "db",
|
||||
};
|
||||
|
||||
expect(validateDbConfig(input(target)).some((i) => i.code === "duplicate-key")).toBe(false);
|
||||
const result = normalizeAuthoringConfig({ targets: [authoring] });
|
||||
const normalized = result.config as { targets: RawTargetConfig[] };
|
||||
const issues = validateDbConfig({ targets: normalized.targets });
|
||||
expect(issues).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -314,7 +314,7 @@ describe("TcpChecker resolve", () => {
|
||||
expect(target.name).toBeNull();
|
||||
expect(target.intervalMs).toBe(30000);
|
||||
expect(target.timeoutMs).toBe(10000);
|
||||
expect(target.rawExpect).toBeUndefined();
|
||||
expect("rawExpect" in target).toBe(false);
|
||||
expect(target.expect).toEqual({ connected: true });
|
||||
});
|
||||
|
||||
@@ -343,7 +343,11 @@ describe("TcpChecker resolve", () => {
|
||||
test("expect 配置解析", () => {
|
||||
const target = checker.resolve(
|
||||
{
|
||||
expect: { banner: [{ contains: "ESMTP" }], connected: false, durationMs: { lte: 5000 } },
|
||||
expect: {
|
||||
banner: [{ kind: "value", matcher: { contains: "ESMTP" } }],
|
||||
connected: false,
|
||||
durationMs: { lte: 5000 },
|
||||
},
|
||||
id: "t",
|
||||
tcp: { host: "127.0.0.1", port: 80, readBanner: true },
|
||||
type: "tcp",
|
||||
@@ -355,7 +359,7 @@ describe("TcpChecker resolve", () => {
|
||||
connected: false,
|
||||
durationMs: { lte: 5000 },
|
||||
});
|
||||
expect(target.rawExpect).toEqual({ banner: [{ contains: "ESMTP" }], connected: false, durationMs: { lte: 5000 } });
|
||||
expect("rawExpect" in target).toBe(false);
|
||||
});
|
||||
|
||||
test("name 和 group 解析", () => {
|
||||
|
||||
@@ -340,7 +340,7 @@ describe("UdpChecker resolve", () => {
|
||||
expect(target.udp.encoding).toBe("text");
|
||||
expect(target.udp.responseEncoding).toBe("text");
|
||||
expect(target.udp.maxResponseBytes).toBe(4096);
|
||||
expect(target.rawExpect).toBeUndefined();
|
||||
expect("rawExpect" in target).toBe(false);
|
||||
expect(target.expect).toEqual({ responded: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ const httpTarget: ResolvedHttpTarget = {
|
||||
id: "test-http",
|
||||
intervalMs: 30000,
|
||||
name: "test-http",
|
||||
rawExpect: { body: [{ contains: "ok" }], durationMs: 3000 },
|
||||
timeoutMs: 10000,
|
||||
type: "http",
|
||||
};
|
||||
@@ -107,7 +106,7 @@ describe("ProbeStore", () => {
|
||||
expect(config.maxRedirects).toBe(0);
|
||||
expect(t.interval_ms).toBe(30000);
|
||||
expect(t.timeout_ms).toBe(10000);
|
||||
expect(JSON.parse(t.expect!)).toEqual({ body: [{ contains: "ok" }], durationMs: 3000 });
|
||||
expect(t.expect).toBeNull();
|
||||
});
|
||||
|
||||
test("cmd target 字段正确", () => {
|
||||
|
||||
@@ -1,8 +1,35 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
|
||||
import { normalizeAuthoringConfig } from "../../../src/server/checker/normalizer";
|
||||
import { extractVariables, resolveVariables } from "../../../src/server/checker/variables";
|
||||
|
||||
describe("config variables", () => {
|
||||
test("normalizeAuthoringConfig 替换变量、展开 expect 简写并移除 variables", () => {
|
||||
const result = normalizeAuthoringConfig({
|
||||
targets: [
|
||||
{
|
||||
expect: { durationMs: "${maxMs}" },
|
||||
http: { url: "${url}" },
|
||||
id: "api",
|
||||
type: "http",
|
||||
},
|
||||
],
|
||||
variables: { maxMs: 1000, url: "https://example.com" },
|
||||
});
|
||||
|
||||
expect(result.issues).toHaveLength(0);
|
||||
expect(result.config).toEqual({
|
||||
targets: [
|
||||
{
|
||||
expect: { durationMs: { equals: 1000 } },
|
||||
http: { url: "https://example.com" },
|
||||
id: "api",
|
||||
type: "http",
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test("提取合法 variables 类型", () => {
|
||||
const result = extractVariables({ variables: { enabled: true, host: "example.com", port: 5432 } });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user