- 新增 config-contract 模块(TypeBox fragments、Ajv 契约校验、ConfigValidationIssue) - CheckerDefinition 扩展为含 configKey、schemas、validate 的完整插件接口 - HTTP/Command 各自维护 contract.ts + validate.ts,校验从 resolve 中分离 - resolve 不再承担校验,只做默认值合并和路径/单位解析 - config-loader 流程: unknown → RawProbeConfig → ValidatedProbeConfig → ResolvedConfig - 导出 probe-config.schema.json,新增 schema/schema:check 脚本 - 更新 DEVELOPMENT.md 新增 1.7 开发新 Checker 完整指引 - 同步更新 4 个 main specs(probe-config、command-checker、expect-body-checkers、checker-runner-abstraction)
72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
import Ajv from "ajv";
|
|
import { describe, expect, test } from "bun:test";
|
|
|
|
import { createProbeConfigJsonSchema } from "../../../../src/server/checker/config-contract/export";
|
|
import { formatConfigIssues, issue } from "../../../../src/server/checker/config-contract/issues";
|
|
import { validateProbeConfigContract } from "../../../../src/server/checker/config-contract/validate";
|
|
import { createDefaultCheckerRegistry } from "../../../../src/server/checker/runner";
|
|
|
|
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" },
|
|
name: "api",
|
|
type: "http",
|
|
},
|
|
],
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
test("Ajv 错误转换为中文结构化 issue", () => {
|
|
const result = validateProbeConfigContract(
|
|
{
|
|
targets: [
|
|
{
|
|
group: 123,
|
|
http: { extra: true },
|
|
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 正则不合法');
|
|
});
|
|
});
|