- 新增顶层 variables 段支持 string/number/boolean 字面量
- target 字符串字段支持 、、{...} 转义语法
- 变量解析优先级: variables -> process.env -> 默认值 -> 报错
- 完整引用保留原始类型,部分引用拼接为字符串
- 变量替换在 YAML 解析后、AJV 校验前执行
- 替换仅作用于 targets,跳过 id/type 字段
- target 新增必填 id 字段作为唯一标识,name 改为可选展示名称
- 数据库存储/API/前端全面迁移到 id 标识
- 统一 checker 运行时类型检查为 es-toolkit predicates
- 同步 delta specs 到主 specs,归档 config-variables 变更
171 lines
5.6 KiB
TypeScript
171 lines
5.6 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import { validateDbConfig } from "../../../../../src/server/checker/runner/db/validate";
|
|
|
|
describe("validateDbConfig", () => {
|
|
test("空配置无问题", () => {
|
|
const result = validateDbConfig({ defaults: {}, targets: [] });
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
test("缺少 db.url 返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [{ id: "test", name: "test", type: "db" }],
|
|
});
|
|
expect(result.length).toBeGreaterThan(0);
|
|
const dbError = result.find((e) => e.path.includes("db"));
|
|
expect(dbError).toBeDefined();
|
|
expect(dbError!.code).toBe("required");
|
|
});
|
|
|
|
test("db.url 为空字符串返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [{ db: { url: "" }, id: "test", name: "test", type: "db" }],
|
|
});
|
|
const urlError = result.find((e) => e.path.includes("db.url"));
|
|
expect(urlError).toBeDefined();
|
|
expect(urlError!.code).toBe("required");
|
|
});
|
|
|
|
test("db.query 为空字符串返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [{ db: { query: "", url: "sqlite://:memory:" }, id: "test", name: "test", type: "db" }],
|
|
});
|
|
const queryError = result.find((e) => e.path.includes("db.query"));
|
|
expect(queryError).toBeDefined();
|
|
expect(queryError!.code).toBe("invalid-value");
|
|
});
|
|
|
|
test("db 分组未知字段返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [{ db: { timeout: 5, url: "sqlite://:memory:" }, id: "test", name: "test", type: "db" }],
|
|
});
|
|
const unknownError = result.find((e) => e.path.includes("db.timeout"));
|
|
expect(unknownError).toBeDefined();
|
|
expect(unknownError!.code).toBe("unknown-field");
|
|
});
|
|
|
|
test("expect.maxDurationMs 非数字返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{
|
|
db: { url: "sqlite://:memory:" },
|
|
expect: { maxDurationMs: "invalid" },
|
|
id: "test",
|
|
name: "test",
|
|
type: "db",
|
|
},
|
|
],
|
|
});
|
|
const durationError = result.find((e) => e.path.includes("expect.maxDurationMs"));
|
|
expect(durationError).toBeDefined();
|
|
expect(durationError!.code).toBe("invalid-type");
|
|
});
|
|
|
|
test("expect.rowCount 非法 operator 返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{ db: { url: "sqlite://:memory:" }, expect: { rowCount: { foo: 1 } }, id: "test", name: "test", type: "db" },
|
|
],
|
|
});
|
|
const rowCountError = result.find((e) => e.path.includes("expect.rowCount"));
|
|
expect(rowCountError).toBeDefined();
|
|
expect(rowCountError!.code).toBe("unknown-operator");
|
|
});
|
|
|
|
test("expect.rows 不是数组返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{ db: { url: "sqlite://:memory:" }, expect: { rows: "not-array" }, id: "test", name: "test", type: "db" },
|
|
],
|
|
});
|
|
const rowsError = result.find((e) => e.path.includes("expect.rows"));
|
|
expect(rowsError).toBeDefined();
|
|
expect(rowsError!.code).toBe("invalid-type");
|
|
});
|
|
|
|
test("expect.rows 元素不是对象返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{ db: { url: "sqlite://:memory:" }, expect: { rows: ["not-object"] }, id: "test", name: "test", type: "db" },
|
|
],
|
|
});
|
|
const rowError = result.find((e) => e.path.includes("expect.rows[0]"));
|
|
expect(rowError).toBeDefined();
|
|
expect(rowError!.code).toBe("invalid-type");
|
|
});
|
|
|
|
test("expect.rows 中 match 正则非法返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{
|
|
db: { url: "sqlite://:memory:" },
|
|
expect: { rows: [{ name: { match: "[invalid" } }] },
|
|
id: "test",
|
|
name: "test",
|
|
type: "db",
|
|
},
|
|
],
|
|
});
|
|
const matchError = result.find((e) => e.path.includes("expect.rows[0].name"));
|
|
expect(matchError).toBeDefined();
|
|
expect(matchError!.code).toBe("invalid-regex");
|
|
});
|
|
|
|
test("expect 未知字段返回错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [{ db: { url: "sqlite://:memory:" }, expect: { status: [200] }, id: "test", name: "test", type: "db" }],
|
|
});
|
|
const unknownError = result.find((e) => e.path.includes("expect.status"));
|
|
expect(unknownError).toBeDefined();
|
|
expect(unknownError!.code).toBe("unknown-field");
|
|
});
|
|
|
|
test("有效配置无错误", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{
|
|
db: { query: "SELECT 1", url: "sqlite://:memory:" },
|
|
expect: { maxDurationMs: 5000, rowCount: { gte: 1 }, rows: [{ cnt: { gte: 1 } }] },
|
|
id: "test",
|
|
name: "test",
|
|
type: "db",
|
|
},
|
|
],
|
|
});
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
test("忽略非 db 类型 target", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [{ id: "test", name: "test", type: "http" }],
|
|
});
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
test("多个 db target 分别校验", () => {
|
|
const result = validateDbConfig({
|
|
defaults: {},
|
|
targets: [
|
|
{ db: { url: "sqlite://:memory:" }, id: "db1", name: "db1", type: "db" },
|
|
{ db: { url: "" }, id: "db2", name: "db2", type: "db" },
|
|
],
|
|
});
|
|
expect(result.length).toBeGreaterThan(0);
|
|
const db2Error = result.find((e) => e.targetName === "db2");
|
|
expect(db2Error).toBeDefined();
|
|
});
|
|
});
|