- 移除 DefaultsConfig 类型、ProbeConfig.defaults 字段 - 移除 CheckerSchemas.defaults、ResolveContext.defaults、CheckerValidationInput.defaults - 更新所有 checker schema/resolve/validate 删除 defaults 合并逻辑 - 更新 config-loader 不再读取传递 defaults - 更新测试、README、DEVELOPMENT、probes.example.yaml - 重新生成 probe-config.schema.json(不含 defaults) - 同步 delta specs 到主规范 - 归档 openspec change
155 lines
4.9 KiB
TypeScript
155 lines
4.9 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import type { CheckerValidationInput } from "../../../../../src/server/checker/runner/types";
|
|
|
|
import { validateTcpConfig } from "../../../../../src/server/checker/runner/tcp/validate";
|
|
|
|
function makeInput(targets: unknown[]): CheckerValidationInput {
|
|
return {
|
|
targets: targets as CheckerValidationInput["targets"],
|
|
};
|
|
}
|
|
|
|
describe("validateTcpConfig", () => {
|
|
test("合法 tcp target 无错误", () => {
|
|
const issues = validateTcpConfig(makeInput([{ id: "t1", tcp: { host: "127.0.0.1", port: 80 }, type: "tcp" }]));
|
|
expect(issues).toHaveLength(0);
|
|
});
|
|
|
|
test("缺少 tcp 分组", () => {
|
|
const issues = validateTcpConfig(makeInput([{ id: "t1", type: "tcp" }]));
|
|
expect(issues.length).toBeGreaterThan(0);
|
|
expect(issues.some((i) => i.message.includes("tcp"))).toBe(true);
|
|
});
|
|
|
|
test("缺少 host", () => {
|
|
const issues = validateTcpConfig(makeInput([{ id: "t1", tcp: { port: 80 }, type: "tcp" }]));
|
|
expect(issues.some((i) => i.path.includes("host"))).toBe(true);
|
|
});
|
|
|
|
test("缺少 port", () => {
|
|
const issues = validateTcpConfig(makeInput([{ id: "t1", tcp: { host: "127.0.0.1" }, type: "tcp" }]));
|
|
expect(issues.some((i) => i.path.includes("port"))).toBe(true);
|
|
});
|
|
|
|
test("端口超范围", () => {
|
|
const issues = validateTcpConfig(makeInput([{ id: "t1", tcp: { host: "127.0.0.1", port: 99999 }, type: "tcp" }]));
|
|
expect(issues.some((i) => i.path.includes("port"))).toBe(true);
|
|
});
|
|
|
|
test("端口为 0", () => {
|
|
const issues = validateTcpConfig(makeInput([{ id: "t1", tcp: { host: "127.0.0.1", port: 0 }, type: "tcp" }]));
|
|
expect(issues.some((i) => i.path.includes("port"))).toBe(true);
|
|
});
|
|
|
|
test("readBanner 非布尔值", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([{ id: "t1", tcp: { host: "127.0.0.1", port: 80, readBanner: "yes" }, type: "tcp" }]),
|
|
);
|
|
expect(issues.some((i) => i.path.includes("readBanner"))).toBe(true);
|
|
});
|
|
|
|
test("bannerReadTimeout 非数字", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([{ id: "t1", tcp: { bannerReadTimeout: "slow", host: "127.0.0.1", port: 80 }, type: "tcp" }]),
|
|
);
|
|
expect(issues.some((i) => i.path.includes("bannerReadTimeout"))).toBe(true);
|
|
});
|
|
|
|
test("tcp 分组未知字段", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([{ id: "t1", tcp: { host: "127.0.0.1", port: 80, tls: true }, type: "tcp" }]),
|
|
);
|
|
expect(issues.some((i) => i.message.includes("未知字段"))).toBe(true);
|
|
});
|
|
|
|
test("expect.banner 未开启 readBanner", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([
|
|
{
|
|
expect: { banner: [{ contains: "ESMTP" }] },
|
|
id: "t1",
|
|
tcp: { host: "127.0.0.1", port: 25 },
|
|
type: "tcp",
|
|
},
|
|
]),
|
|
);
|
|
expect(issues.some((i) => i.message.includes("readBanner"))).toBe(true);
|
|
});
|
|
|
|
test("expect.banner 开启 readBanner 无错误", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([
|
|
{
|
|
expect: { banner: [{ contains: "ESMTP" }] },
|
|
id: "t1",
|
|
tcp: { host: "127.0.0.1", port: 25, readBanner: true },
|
|
type: "tcp",
|
|
},
|
|
]),
|
|
);
|
|
expect(issues).toHaveLength(0);
|
|
});
|
|
|
|
test("expect connected 非布尔值", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([
|
|
{
|
|
expect: { connected: "yes" },
|
|
id: "t1",
|
|
tcp: { host: "127.0.0.1", port: 80 },
|
|
type: "tcp",
|
|
},
|
|
]),
|
|
);
|
|
expect(issues.some((i) => i.path.includes("connected"))).toBe(true);
|
|
});
|
|
|
|
test("expect durationMs 数组简写非法", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([
|
|
{
|
|
expect: { durationMs: [1, 2] },
|
|
id: "t1",
|
|
tcp: { host: "127.0.0.1", port: 80 },
|
|
type: "tcp",
|
|
},
|
|
]),
|
|
);
|
|
expect(issues.some((i) => i.code === "invalid-type" && i.path.includes("durationMs"))).toBe(true);
|
|
});
|
|
|
|
test("expect 未知字段", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([
|
|
{
|
|
expect: { status: [200] },
|
|
id: "t1",
|
|
tcp: { host: "127.0.0.1", port: 80 },
|
|
type: "tcp",
|
|
},
|
|
]),
|
|
);
|
|
expect(issues.some((i) => i.message.includes("未知字段"))).toBe(true);
|
|
});
|
|
|
|
test("expect.banner regex 正则非法", () => {
|
|
const issues = validateTcpConfig(
|
|
makeInput([
|
|
{
|
|
expect: { banner: [{ regex: "[invalid" }] },
|
|
id: "t1",
|
|
tcp: { host: "127.0.0.1", port: 25, readBanner: true },
|
|
type: "tcp",
|
|
},
|
|
]),
|
|
);
|
|
expect(issues.some((i) => i.message.includes("正则"))).toBe(true);
|
|
});
|
|
|
|
test("非 tcp 类型 target 跳过", () => {
|
|
const issues = validateTcpConfig(makeInput([{ http: { url: "http://example.com" }, id: "t1", type: "http" }]));
|
|
expect(issues).toHaveLength(0);
|
|
});
|
|
});
|