将变量替换和 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
99 lines
3.7 KiB
TypeScript
99 lines
3.7 KiB
TypeScript
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";
|
|
|
|
function input(target: Record<string, unknown>): CheckerValidationInput {
|
|
return { targets: [target as CheckerValidationInput["targets"][number]] };
|
|
}
|
|
|
|
describe("HTTP/LLM headers reject case-insensitive duplicate keys", () => {
|
|
test("HTTP headers 大小写不同的重复 key 报错", () => {
|
|
const target = {
|
|
expect: { headers: { "Content-Type": "application/json", "content-type": "text/plain" } },
|
|
http: { url: "https://example.com" },
|
|
id: "dup",
|
|
type: "http",
|
|
};
|
|
|
|
const issues = validateHttpConfig(input(target));
|
|
expect(issues.some((i) => i.code === "duplicate-key" && i.path.includes("headers"))).toBe(true);
|
|
});
|
|
|
|
test("LLM headers 大小写不同的重复 key 报错", () => {
|
|
const target = {
|
|
expect: { headers: { "X-Trace": "a", "x-trace": "b" } },
|
|
id: "dup",
|
|
llm: {
|
|
mode: "stream",
|
|
model: "test-model",
|
|
prompt: "hello",
|
|
provider: "openai",
|
|
url: "https://example.com/v1/chat/completions",
|
|
},
|
|
type: "llm",
|
|
};
|
|
|
|
const issues = validateLlmConfig(input(target));
|
|
expect(issues.some((i) => i.code === "duplicate-key" && i.path.includes("headers"))).toBe(true);
|
|
});
|
|
|
|
test("HTTP headers 不同 key 不触发 duplicate-key", () => {
|
|
const target = {
|
|
expect: { headers: { Accept: "application/json", "Content-Type": "application/json" } },
|
|
http: { url: "https://example.com" },
|
|
id: "ok",
|
|
type: "http",
|
|
};
|
|
|
|
expect(validateHttpConfig(input(target)).some((i) => i.code === "duplicate-key")).toBe(false);
|
|
});
|
|
|
|
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: "Alice" }] },
|
|
id: "db-rows",
|
|
type: "db",
|
|
};
|
|
|
|
const result = normalizeAuthoringConfig({ targets: [authoring] });
|
|
const normalized = result.config as { targets: RawTargetConfig[] };
|
|
const issues = validateDbConfig({ targets: normalized.targets });
|
|
expect(issues).toHaveLength(0);
|
|
});
|
|
});
|