1
0
Files
DiAL/tests/server/checker/runner/db/validate.test.ts
lanyuanxiaoyao 7a635a0a9f refactor: 统一 expect 断言体系,引入共享 ValueMatcher/ContentRules/KeyValueExpect 模型
- 引入共享 ValueMatcher(equals/contains/regex/exists/empty/gt/gte/lt/lte)
- 引入共享 ContentRules 数组(direct/json/css/xpath 提取器)
- 引入共享 KeyValueExpect(动态键值断言,字面量等价 equals)
- maxDurationMs → durationMs: ValueMatcher(所有 checker)
- match → regex(固定无 flags)
- Ping max* → packetLossPercent/avgLatencyMs/maxLatencyMs(ValueMatcher)
- LLM finishReason/rawFinishReason → ValueMatcher
- DB 新增 result: ContentRules
- TCP banner → ContentRules 数组
- 删除旧模块:operator.ts、validate-operator.ts、duration.ts、body.ts、text.ts、output.ts
- 更新全部 checker schema/validate/expect/execute
- 更新 probe-config.schema.json、probes.example.yaml
- 更新 README.md、DEVELOPMENT.md(含 expect 字段选择规范)
- 同步 10 个 delta specs 到主 specs,归档 change
2026-05-19 14:24:27 +08:00

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.durationMs 非 matcher 返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [
{
db: { url: "sqlite://:memory:" },
expect: { durationMs: "invalid" },
id: "test",
name: "test",
type: "db",
},
],
});
const durationError = result.find((e) => e.path.includes("expect.durationMs"));
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-matcher");
});
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 中 regex 正则非法返回错误", () => {
const result = validateDbConfig({
defaults: {},
targets: [
{
db: { url: "sqlite://:memory:" },
expect: { rows: [{ name: { regex: "[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: { durationMs: { lte: 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();
});
});