1
0
Files
DiAL/tests/server/checker/runner/tcp/expect.test.ts
lanyuanxiaoyao 60a54b483f refactor: expect 类型模型重构,Raw/Resolved 双层分离与断言基础设施内聚
- 重命名 ContentRules→ContentExpectations, KeyValueExpect→KeyedExpectations
- 新增 Raw/Resolved 双层模型:resolve 阶段物化为执行计划,store 持久化 Raw 快照
- HTTP body 按需读取:status/headers 失败或无 body expectation 时不读取 body
- 新增 displayValueExpectation() 解包 failure.expected 用户可读展示
- 修复 checkEarlyTimeout 独立 lte/lt 检查,修复 KeyedExpectations JSON Schema
- 新增 expect/value.ts(resolve/check/display)、keyed.ts、content.ts、headers.ts、status.ts
- 删除旧 normalize.ts/matcher.ts/validate-matcher.ts/key-value.ts
- 更新 DEVELOPMENT.md:expect 五层管线表、displayValueExpectation、1.7↔1.10 交叉引用
- 同步 13 个 main specs,归档 refactor-expect-type-model 变更(62/62 tasks)
2026-05-20 16:12:48 +08:00

72 lines
2.4 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import type { ContentExpectations } from "../../../../../src/server/checker/expect/types";
import { checkBanner, checkConnected } from "../../../../../src/server/checker/runner/tcp/expect";
function value(matcher: ContentExpectations[number]["matcher"]): ContentExpectations[number] {
return { kind: "value", matcher };
}
describe("checkConnected", () => {
test("connected=true 期望 true 匹配", () => {
const result = checkConnected(true, true);
expect(result.matched).toBe(true);
expect(result.failure).toBeNull();
});
test("connected=false 期望 false 匹配", () => {
const result = checkConnected(false, false);
expect(result.matched).toBe(true);
expect(result.failure).toBeNull();
});
test("connected=false 期望 true 不匹配", () => {
const result = checkConnected(false, true);
expect(result.matched).toBe(false);
expect(result.failure!.kind).toBe("mismatch");
expect(result.failure!.phase).toBe("connected");
});
test("connected=true 期望 false 不匹配", () => {
const result = checkConnected(true, false);
expect(result.matched).toBe(false);
expect(result.failure!.kind).toBe("mismatch");
expect(result.failure!.phase).toBe("connected");
});
});
describe("checkBanner", () => {
test("contains 匹配", () => {
const result = checkBanner("220 smtp.example.com ESMTP", [value({ contains: "ESMTP" })]);
expect(result.matched).toBe(true);
});
test("contains 不匹配", () => {
const result = checkBanner("220 smtp.example.com ESMTP", [value({ contains: "POSTFIX" })]);
expect(result.matched).toBe(false);
expect(result.failure!.kind).toBe("mismatch");
expect(result.failure!.phase).toBe("banner");
});
test("regex 正则匹配", () => {
const result = checkBanner("220 smtp.example.com ESMTP", [value({ regex: "^220" })]);
expect(result.matched).toBe(true);
});
test("空 banner 与 contains 空字符串", () => {
const result = checkBanner("", [value({ contains: "" })]);
expect(result.matched).toBe(true);
});
test("多 operator 同时匹配", () => {
const result = checkBanner("220 ESMTP", [value({ contains: "ESMTP", regex: "^220" })]);
expect(result.matched).toBe(true);
});
test("多 operator 部分不匹配", () => {
const result = checkBanner("220 ESMTP", [value({ contains: "ESMTP", regex: "^250" })]);
expect(result.matched).toBe(false);
});
});