- 引入共享 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
130 lines
4.7 KiB
TypeScript
130 lines
4.7 KiB
TypeScript
import { afterEach, describe, expect, mock, test } from "bun:test";
|
|
|
|
import type { ResolvedPingTarget } from "../../../../../src/server/checker/runner/icmp/types";
|
|
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
|
|
|
|
import { IcmpChecker } from "../../../../../src/server/checker/runner/icmp/execute";
|
|
|
|
const checker = new IcmpChecker();
|
|
const originalSpawn = Bun.spawn;
|
|
|
|
afterEach(() => {
|
|
Bun.spawn = originalSpawn;
|
|
mock.restore();
|
|
});
|
|
|
|
function makeCtx(): CheckerContext {
|
|
return { signal: new AbortController().signal };
|
|
}
|
|
|
|
function makeTarget(overrides?: Partial<ResolvedPingTarget>): ResolvedPingTarget {
|
|
return {
|
|
description: null,
|
|
group: "default",
|
|
id: "ping-local",
|
|
intervalMs: 30000,
|
|
name: null,
|
|
ping: { count: 3, host: "127.0.0.1", packetSize: 56 },
|
|
timeoutMs: 10000,
|
|
type: "ping",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function mockSpawn(stdout: string, exitCode = 0) {
|
|
const calls: string[][] = [];
|
|
const spawnMock = mock((command: string[]) => {
|
|
calls.push(command);
|
|
return {
|
|
exitCode,
|
|
exited: Promise.resolve(exitCode),
|
|
kill: mock(() => undefined),
|
|
stderr: new Response("").body,
|
|
stdout: new Response(stdout).body,
|
|
};
|
|
});
|
|
Bun.spawn = spawnMock as unknown as typeof Bun.spawn;
|
|
return calls;
|
|
}
|
|
|
|
describe("IcmpChecker execute", () => {
|
|
test("执行 ping 并匹配默认 alive", async () => {
|
|
const calls = mockSpawn(`3 packets transmitted, 3 received, 0% packet loss, time 2003ms
|
|
rtt min/avg/max/mdev = 1.234/2.345/3.456/0.567 ms`);
|
|
const result = await checker.execute(makeTarget(), makeCtx());
|
|
expect(result.matched).toBe(true);
|
|
expect(result.failure).toBeNull();
|
|
expect(result.statusDetail).toBe("alive, avg 2.345ms, loss 0% (3/3)");
|
|
expect(calls[0]).toContain("ping");
|
|
});
|
|
|
|
test("alive 失败短路", async () => {
|
|
mockSpawn(`3 packets transmitted, 0 received, 100% packet loss, time 2003ms`);
|
|
const result = await checker.execute(
|
|
makeTarget({ expect: { alive: true, avgLatencyMs: { lte: 100 } } }),
|
|
makeCtx(),
|
|
);
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("alive");
|
|
expect(result.statusDetail).toBe("unreachable (0/3 received)");
|
|
});
|
|
|
|
test("反向 alive 断言通过", async () => {
|
|
mockSpawn(`3 packets transmitted, 0 received, 100% packet loss, time 2003ms`);
|
|
const result = await checker.execute(makeTarget({ expect: { alive: false } }), makeCtx());
|
|
expect(result.matched).toBe(true);
|
|
});
|
|
|
|
test("packetLoss 断言失败", async () => {
|
|
mockSpawn(`3 packets transmitted, 2 received, 33% packet loss, time 2003ms
|
|
rtt min/avg/max/mdev = 1.234/156.000/340.000/0.567 ms`);
|
|
const result = await checker.execute(makeTarget({ expect: { packetLossPercent: { lte: 10 } } }), makeCtx());
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.phase).toBe("packetLoss");
|
|
expect(result.statusDetail).toContain("max 340ms");
|
|
});
|
|
|
|
test("解析失败返回结构化错误", async () => {
|
|
mockSpawn("unexpected output");
|
|
const result = await checker.execute(makeTarget(), makeCtx());
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure).toMatchObject({ kind: "error", path: "parse", phase: "ping" });
|
|
});
|
|
|
|
test("spawn 失败返回 ping 命令不可用", async () => {
|
|
Bun.spawn = mock(() => {
|
|
throw new Error("ENOENT");
|
|
});
|
|
const result = await checker.execute(makeTarget(), makeCtx());
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure?.message).toContain("ping 命令不可用");
|
|
expect(result.statusDetail).toBe("ping command not found");
|
|
});
|
|
|
|
test("预 abort 返回超时错误", async () => {
|
|
mockSpawn(`3 packets transmitted, 3 received, 0% packet loss, time 2003ms`);
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
const result = await checker.execute(makeTarget(), { signal: controller.signal });
|
|
expect(result.matched).toBe(false);
|
|
expect(result.failure).toMatchObject({ path: "timeout", phase: "ping" });
|
|
});
|
|
});
|
|
|
|
describe("IcmpChecker resolve", () => {
|
|
test("解析默认值", () => {
|
|
const target = checker.resolve(
|
|
{ id: "ping", ping: { host: "10.0.0.1" }, type: "ping" },
|
|
{ configDir: "/tmp", defaultIntervalMs: 30000, defaults: {}, defaultTimeoutMs: 10000 },
|
|
);
|
|
expect(target.ping).toEqual({ count: 3, host: "10.0.0.1", packetSize: 56 });
|
|
expect(target.group).toBe("default");
|
|
});
|
|
|
|
test("serialize 返回摘要和配置", () => {
|
|
const serialized = checker.serialize(makeTarget({ ping: { count: 5, host: "10.0.0.1", packetSize: 1472 } }));
|
|
expect(serialized.target).toBe("ping 10.0.0.1");
|
|
expect(JSON.parse(serialized.config)).toEqual({ count: 5, host: "10.0.0.1", packetSize: 1472 });
|
|
});
|
|
});
|