1
0
Files
DiAL/tests/server/checker/runner/icmp/command.test.ts
lanyuanxiaoyao 550c427814 feat: 新增 ICMP/Ping checker,支持跨平台主机存活检测与延迟监控
实现 type: ping checker,通过 Bun.spawn 调用系统 ping 命令,自行实现跨平台
输出解析器(Linux/macOS/Windows 含中文 locale),支持 alive、丢包率、延迟、
耗时等 expect 断言,复用现有 checker 架构零外部依赖。

包含完整的类型定义、TypeBox schema、语义校验、命令构建、解析、断言、执行、
注册、配置加载测试,以及 probe-config.schema.json 更新和文档更新。

审查修复:提取 buildPingCommand 为独立纯函数并补充跨平台单测,补充
maxDurationMs/maxAvgLatencyMs 类型非法和空字符串 host 边界测试用例。

变更已归档,delta specs 已同步至 main specs。
2026-05-18 10:45:17 +08:00

55 lines
1.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import type { ResolvedPingTarget } from "../../../../../src/server/checker/runner/icmp/types";
import { buildPingCommand } from "../../../../../src/server/checker/runner/icmp/command";
function makeTarget(overrides?: Partial<ResolvedPingTarget>): ResolvedPingTarget {
return {
description: null,
group: "default",
id: "test",
intervalMs: 30000,
name: null,
ping: { count: 3, host: "10.0.0.1", packetSize: 56 },
timeoutMs: 10000,
type: "ping",
...overrides,
};
}
describe("buildPingCommand", () => {
test("Linux 默认参数", () => {
const cmd = buildPingCommand(makeTarget(), "linux");
expect(cmd).toEqual(["ping", "-c", "3", "-s", "56", "-W", "10", "10.0.0.1"]);
});
test("Linux 秒向上取整", () => {
const cmd = buildPingCommand(makeTarget({ timeoutMs: 10500 }), "linux");
expect(cmd[6]).toBe("11");
});
test("Linux timeoutMs < 1000 向上取整为 1", () => {
const cmd = buildPingCommand(makeTarget({ timeoutMs: 500 }), "linux");
expect(cmd[6]).toBe("1");
});
test("macOS 毫秒", () => {
const cmd = buildPingCommand(makeTarget(), "darwin");
expect(cmd).toEqual(["ping", "-c", "3", "-s", "56", "-W", "10000", "10.0.0.1"]);
});
test("Windows 格式", () => {
const cmd = buildPingCommand(makeTarget(), "win32");
expect(cmd).toEqual(["ping", "-n", "3", "-l", "56", "-w", "10000", "10.0.0.1"]);
});
test("自定义 count 和 packetSize", () => {
const cmd = buildPingCommand(
makeTarget({ ping: { count: 5, host: "10.0.0.1", packetSize: 1472 }, timeoutMs: 5000 }),
"linux",
);
expect(cmd).toEqual(["ping", "-c", "5", "-s", "1472", "-W", "5", "10.0.0.1"]);
});
});