- 引入共享 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
121 lines
3.8 KiB
TypeScript
121 lines
3.8 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
||
|
||
import {
|
||
checkResponded,
|
||
checkResponseSize,
|
||
checkResponseText,
|
||
checkSourceHost,
|
||
checkSourcePort,
|
||
} from "../../../../../src/server/checker/runner/udp/expect";
|
||
|
||
describe("checkResponded", () => {
|
||
it("responded=true 期望 true → 匹配", () => {
|
||
const result = checkResponded(true, true);
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("responded=false 期望 true → 不匹配", () => {
|
||
const result = checkResponded(false, true);
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.kind).toBe("mismatch");
|
||
expect(result.failure!.phase).toBe("responded");
|
||
});
|
||
|
||
it("responded=false 期望 false → 匹配", () => {
|
||
const result = checkResponded(false, false);
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("responded=true 期望 false → 不匹配", () => {
|
||
const result = checkResponded(true, false);
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.kind).toBe("mismatch");
|
||
expect(result.failure!.phase).toBe("responded");
|
||
});
|
||
});
|
||
|
||
describe("checkResponseSize", () => {
|
||
it("size=4 gte=4 → 匹配", () => {
|
||
const result = checkResponseSize(4, { gte: 4 });
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("size=2 gte=4 → 不匹配,phase=responseSize", () => {
|
||
const result = checkResponseSize(2, { gte: 4 });
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.kind).toBe("mismatch");
|
||
expect(result.failure!.phase).toBe("responseSize");
|
||
});
|
||
|
||
it("size=10 lt=20 → 匹配", () => {
|
||
const result = checkResponseSize(10, { lt: 20 });
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
|
||
it("size=5 equals=5 → 匹配", () => {
|
||
const result = checkResponseSize(5, { equals: 5 });
|
||
expect(result.matched).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe("checkResponseText", () => {
|
||
it("单条 contains 匹配", () => {
|
||
const result = checkResponseText("PONG", [{ contains: "PONG" }]);
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("单条 contains 不匹配,phase=response", () => {
|
||
const result = checkResponseText("PING", [{ contains: "PONG" }]);
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.kind).toBe("mismatch");
|
||
expect(result.failure!.phase).toBe("response");
|
||
});
|
||
|
||
it("多条规则全部匹配", () => {
|
||
const result = checkResponseText("hello world", [{ contains: "hello" }, { regex: "^hello" }]);
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("多条规则第二条失败 → 不匹配", () => {
|
||
const result = checkResponseText("hello world", [{ contains: "hello" }, { regex: "^world" }]);
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.phase).toBe("response");
|
||
expect(result.failure!.path).toBe("response[1]");
|
||
});
|
||
});
|
||
|
||
describe("checkSourceHost", () => {
|
||
it("equals 匹配", () => {
|
||
const result = checkSourceHost("127.0.0.1", { equals: "127.0.0.1" });
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("equals 不匹配", () => {
|
||
const result = checkSourceHost("10.0.0.1", { equals: "127.0.0.1" });
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.kind).toBe("mismatch");
|
||
expect(result.failure!.phase).toBe("sourceHost");
|
||
});
|
||
});
|
||
|
||
describe("checkSourcePort", () => {
|
||
it("equals 匹配", () => {
|
||
const result = checkSourcePort(9000, { equals: 9000 });
|
||
expect(result.matched).toBe(true);
|
||
expect(result.failure).toBeNull();
|
||
});
|
||
|
||
it("equals 不匹配", () => {
|
||
const result = checkSourcePort(8080, { equals: 9000 });
|
||
expect(result.matched).toBe(false);
|
||
expect(result.failure!.kind).toBe("mismatch");
|
||
expect(result.failure!.phase).toBe("sourcePort");
|
||
});
|
||
});
|