实现 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。
62 lines
2.0 KiB
TypeScript
62 lines
2.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
||
|
||
import { parsePingOutput } from "../../../../../src/server/checker/runner/icmp/parse";
|
||
|
||
describe("parsePingOutput", () => {
|
||
test("解析 Linux ping 输出", () => {
|
||
const stats = parsePingOutput(
|
||
`3 packets transmitted, 3 received, 0% packet loss, time 2003ms
|
||
rtt min/avg/max/mdev = 1.234/2.345/3.456/0.567 ms`,
|
||
"linux",
|
||
);
|
||
expect(stats).toEqual({
|
||
alive: true,
|
||
avgLatencyMs: 2.345,
|
||
maxLatencyMs: 3.456,
|
||
minLatencyMs: 1.234,
|
||
packetLoss: 0,
|
||
received: 3,
|
||
transmitted: 3,
|
||
});
|
||
});
|
||
|
||
test("解析 macOS ping 输出", () => {
|
||
const stats = parsePingOutput(
|
||
`3 packets transmitted, 3 packets received, 0.0% packet loss
|
||
round-trip min/avg/max/stddev = 1.234/2.345/3.456/0.567 ms`,
|
||
"darwin",
|
||
);
|
||
expect(stats?.avgLatencyMs).toBe(2.345);
|
||
expect(stats?.packetLoss).toBe(0);
|
||
});
|
||
|
||
test("解析 Windows 英文 ping 输出", () => {
|
||
const stats = parsePingOutput(
|
||
`Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
|
||
Approximate round trip times in milli-seconds:
|
||
Minimum = 1ms, Maximum = 3ms, Average = 2ms`,
|
||
"win32",
|
||
);
|
||
expect(stats).toMatchObject({ avgLatencyMs: 2, maxLatencyMs: 3, minLatencyMs: 1, packetLoss: 0 });
|
||
});
|
||
|
||
test("解析 Windows 中文 ping 输出", () => {
|
||
const stats = parsePingOutput(
|
||
`数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失),
|
||
往返行程的估计时间(以毫秒为单位):
|
||
最短 = 1ms,最长 = 3ms,平均 = 2ms`,
|
||
"win32",
|
||
);
|
||
expect(stats).toMatchObject({ avgLatencyMs: 2, maxLatencyMs: 3, minLatencyMs: 1, packetLoss: 0 });
|
||
});
|
||
|
||
test("解析全部丢包", () => {
|
||
const stats = parsePingOutput(`3 packets transmitted, 0 received, 100% packet loss, time 2003ms`, "linux");
|
||
expect(stats).toMatchObject({ alive: false, avgLatencyMs: null, maxLatencyMs: null, minLatencyMs: null });
|
||
});
|
||
|
||
test("无法解析返回 null", () => {
|
||
expect(parsePingOutput("unexpected output", "linux")).toBeNull();
|
||
});
|
||
});
|