1
0
Files
DiAL/tests/server/checker/runner/shared/value-matcher-shorthand.test.ts
lanyuanxiaoyao 9b53c746f6 refactor: ICMP checker type 从 ping 统一改为 icmp,修复前端 UI 细节
- ICMP checker 的 type/configKey/YAML 配置键/接口属性名从 ping 改为 icmp
- IcmpChecker 添加 platform 构造函数注入,修复 Windows 测试兼容性
- 前端 target 表格延迟列优化:标题简化为「延迟」,单位下移到单元格,宽度 80px
- Drawer 概览页 Descriptions 添加 tableLayout=auto 收窄 label 宽度
- 同步更新 README.md、DEVELOPMENT.md、probes.example.yaml、JSON Schema 和全部测试
2026-05-20 00:02:23 +08:00

116 lines
3.9 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import type { CheckerValidationInput } from "../../../../../src/server/checker/runner/types";
import { validateCommandConfig } from "../../../../../src/server/checker/runner/cmd/validate";
import { validateDbConfig } from "../../../../../src/server/checker/runner/db/validate";
import { validateHttpConfig } from "../../../../../src/server/checker/runner/http/validate";
import { validatePingConfig } from "../../../../../src/server/checker/runner/icmp/validate";
import { validateLlmConfig } from "../../../../../src/server/checker/runner/llm/validate";
import { validateTcpConfig } from "../../../../../src/server/checker/runner/tcp/validate";
import { validateUdpConfig } from "../../../../../src/server/checker/runner/udp/validate";
function input(target: Record<string, unknown>): CheckerValidationInput {
return { defaults: {}, targets: [target as CheckerValidationInput["targets"][number]] };
}
describe("ValueMatcher primitive shorthand in checker validators", () => {
test("normalizes shorthand for all checker ValueMatcher fields", () => {
const targets = [
{
expect: { durationMs: 100 },
http: { url: "https://example.com" },
id: "http",
type: "http",
validate: validateHttpConfig,
},
{
expect: { durationMs: 100 },
id: "tcp",
tcp: { host: "127.0.0.1", port: 80 },
type: "tcp",
validate: validateTcpConfig,
},
{
expect: { durationMs: 100, responseSize: 1, sourceHost: "127.0.0.1", sourcePort: 53 },
id: "udp",
type: "udp",
udp: { host: "127.0.0.1", port: 53 },
validate: validateUdpConfig,
},
{
expect: { avgLatencyMs: 1, durationMs: 100, maxLatencyMs: 2, packetLossPercent: 0 },
icmp: { host: "127.0.0.1" },
id: "icmp",
type: "icmp",
validate: validatePingConfig,
},
{
cmd: { exec: "true" },
expect: { durationMs: 100 },
id: "cmd",
type: "cmd",
validate: validateCommandConfig,
},
{
db: { url: "sqlite://:memory:" },
expect: { durationMs: 100, rowCount: 1 },
id: "db",
type: "db",
validate: validateDbConfig,
},
{
expect: {
durationMs: 100,
finishReason: "stop",
rawFinishReason: null,
stream: { firstTokenMs: 10 },
usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
},
id: "llm",
llm: {
mode: "stream",
model: "test-model",
prompt: "hello",
provider: "openai",
url: "https://example.com/v1/chat/completions",
},
type: "llm",
validate: validateLlmConfig,
},
];
for (const target of targets) {
const { validate, ...config } = target;
expect(validate(input(config))).toHaveLength(0);
expect((config.expect as Record<string, unknown>)["durationMs"]).toEqual({ equals: 100 });
}
});
test("rejects array and object shorthand while accepting explicit equals", () => {
const arrayTarget = {
expect: { durationMs: [1, 2] },
http: { url: "https://example.com" },
id: "array",
type: "http",
};
const objectTarget = {
expect: { durationMs: { foo: "bar" } },
http: { url: "https://example.com" },
id: "object",
type: "http",
};
const equalsObjectTarget = {
expect: { durationMs: { equals: { foo: "bar" } } },
http: { url: "https://example.com" },
id: "equals-object",
type: "http",
};
expect(validateHttpConfig(input(arrayTarget)).some((issue) => issue.path.includes("durationMs"))).toBe(true);
expect(validateHttpConfig(input(objectTarget)).some((issue) => issue.code === "unknown-matcher")).toBe(true);
expect(validateHttpConfig(input(equalsObjectTarget))).toHaveLength(0);
});
});