1
0

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)
This commit is contained in:
2026-05-20 16:12:48 +08:00
parent 6098be2d9e
commit 60a54b483f
90 changed files with 2487 additions and 1493 deletions

View File

@@ -2,10 +2,11 @@ import { isError } from "es-toolkit";
import type { CheckResult, RawTargetConfig } from "../../types";
import type { CheckerContext, CheckerDefinition, CheckerValidationInput, ResolveContext } from "../types";
import type { ResolvedTcpTarget, TcpExpectConfig, TcpTargetConfig } from "./types";
import type { RawTcpExpectConfig, ResolvedTcpExpectConfig, ResolvedTcpTarget, TcpTargetConfig } from "./types";
import { resolveContentExpectations } from "../../expect/content";
import { errorFailure } from "../../expect/failure";
import { checkValueMatcher } from "../../expect/matcher";
import { checkValueExpectation, resolveValueExpectation } from "../../expect/value";
import { parseSize } from "../../utils";
import { checkBanner, checkConnected } from "./expect";
import { tcpCheckerSchemas } from "./schema";
@@ -159,7 +160,7 @@ export class TcpChecker implements CheckerDefinition<ResolvedTcpTarget> {
}
const durationMs = Math.round(performance.now() - start);
const durationResult = checkValueMatcher(durationMs, expect?.durationMs, {
const durationResult = checkValueExpectation(durationMs, expect?.durationMs, {
message: "durationMs mismatch",
path: "durationMs",
phase: "duration",
@@ -212,13 +213,23 @@ export class TcpChecker implements CheckerDefinition<ResolvedTcpTarget> {
const maxBannerBytes = parseSize(t.tcp.maxBannerBytes ?? tcpDefaults?.maxBannerBytes ?? DEFAULT_MAX_BANNER_BYTES);
const bannerReadTimeout = t.tcp.bannerReadTimeout ?? tcpDefaults?.bannerReadTimeout ?? DEFAULT_BANNER_READ_TIMEOUT;
const rawExpect = target.expect as RawTcpExpectConfig | undefined;
const resolvedExpect: ResolvedTcpExpectConfig = rawExpect
? {
banner: resolveContentExpectations(rawExpect.banner),
connected: rawExpect.connected ?? true,
durationMs: resolveValueExpectation(rawExpect.durationMs),
}
: { connected: true };
return {
description: null,
expect: target.expect as TcpExpectConfig | undefined,
expect: resolvedExpect,
group: target.group ?? "default",
id: t.id,
intervalMs: context.defaultIntervalMs,
name: t.name ?? null,
rawExpect,
tcp: {
bannerReadTimeout,
host: t.tcp.host,

View File

@@ -1,13 +1,13 @@
import type { ContentRules, ExpectResult } from "../../expect/types";
import type { ContentExpectations, ExpectationResult } from "../../expect/types";
import { checkContentRules } from "../../expect/content";
import { checkContentExpectations } from "../../expect/content";
import { mismatchFailure } from "../../expect/failure";
export function checkBanner(banner: string, rules: ContentRules): ExpectResult {
return checkContentRules(banner, rules, { path: "banner", phase: "banner" });
export function checkBanner(banner: string, expectations: ContentExpectations): ExpectationResult {
return checkContentExpectations(banner, expectations, { path: "banner", phase: "banner" });
}
export function checkConnected(connected: boolean, expected: boolean): ExpectResult {
export function checkConnected(connected: boolean, expected: boolean): ExpectationResult {
if (connected === expected) return { failure: null, matched: true };
if (!connected && expected) {
return {

View File

@@ -2,7 +2,11 @@ import { Type } from "@sinclair/typebox";
import type { CheckerSchemas } from "../types";
import { createContentRulesSchema, createValueMatcherSchema, sizeSchema } from "../../schema/fragments";
import {
createRawContentExpectationsSchema,
createRawValueExpectationSchema,
sizeSchema,
} from "../../schema/fragments";
export const tcpCheckerSchemas: CheckerSchemas = {
config: Type.Object(
@@ -24,9 +28,9 @@ export const tcpCheckerSchemas: CheckerSchemas = {
),
expect: Type.Object(
{
banner: Type.Optional(createContentRulesSchema()),
banner: Type.Optional(createRawContentExpectationsSchema()),
connected: Type.Optional(Type.Boolean()),
durationMs: Type.Optional(createValueMatcherSchema()),
durationMs: Type.Optional(createRawValueExpectationSchema()),
},
{ additionalProperties: false },
),

View File

@@ -1,6 +1,17 @@
import type { ContentRules, ValueMatcherInput } from "../../expect/types";
import type {
ContentExpectations,
RawContentExpectations,
RawValueExpectation,
ValueExpectation,
} from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface RawTcpExpectConfig {
banner?: RawContentExpectations;
connected?: boolean;
durationMs?: RawValueExpectation;
}
export interface ResolvedTcpConfig {
bannerReadTimeout: number;
host: string;
@@ -9,11 +20,18 @@ export interface ResolvedTcpConfig {
readBanner: boolean;
}
export interface ResolvedTcpExpectConfig {
banner?: ContentExpectations;
connected: boolean;
durationMs?: ValueExpectation;
}
export interface ResolvedTcpTarget extends ResolvedTargetBase {
expect?: TcpExpectConfig;
expect?: ResolvedTcpExpectConfig;
group: string;
intervalMs: number;
name: null | string;
rawExpect?: RawTcpExpectConfig;
tcp: ResolvedTcpConfig;
timeoutMs: number;
type: "tcp";
@@ -24,12 +42,6 @@ export interface TcpDefaultsConfig {
maxBannerBytes?: number | string;
}
export interface TcpExpectConfig {
banner?: ContentRules;
connected?: boolean;
durationMs?: ValueMatcherInput;
}
export interface TcpTargetConfig {
bannerReadTimeout?: number;
host: string;

View File

@@ -1,10 +1,9 @@
import { isNumber, isPlainObject, isString } from "es-toolkit";
import { isNumber, isString } from "es-toolkit";
import type { ConfigValidationIssue } from "../../schema/issues";
import type { CheckerValidationInput } from "../types";
import { normalizeExpectMatchers } from "../../expect/normalize";
import { validateContentRules, validateValueMatcher } from "../../expect/validate-matcher";
import { isPlainRecord, validateRawContentExpectations, validateRawValueExpectation } from "../../expect/validate";
import { issue, joinPath } from "../../schema/issues";
export function validateTcpConfig(input: CheckerValidationInput): ConfigValidationIssue[] {
@@ -14,7 +13,7 @@ export function validateTcpConfig(input: CheckerValidationInput): ConfigValidati
for (let i = 0; i < input.targets.length; i++) {
const target = input.targets[i] as unknown;
if (!isPlainObject(target)) continue;
if (!isPlainRecord(target)) continue;
if (target["type"] !== "tcp") continue;
issues.push(...validateTcpTarget(target, `targets[${i}]`));
}
@@ -34,7 +33,7 @@ function isNonNegativeFiniteNumber(value: unknown): boolean {
function validateTcpDefaults(input: CheckerValidationInput): ConfigValidationIssue[] {
const issues: ConfigValidationIssue[] = [];
const defaults = input.defaults["tcp"];
if (defaults === undefined || defaults === null || !isPlainObject(defaults)) return issues;
if (defaults === undefined || defaults === null || !isPlainRecord(defaults)) return issues;
const targetName = "defaults.tcp";
@@ -72,18 +71,16 @@ function validateTcpExpect(
): ConfigValidationIssue[] {
const targetName = getTargetName(target);
const expect = target["expect"];
if (expect === undefined || expect === null || !isPlainObject(expect)) return [];
if (expect === undefined || expect === null || !isPlainRecord(expect)) return [];
const issues: ConfigValidationIssue[] = [];
const expectPath = joinPath(path, "expect");
normalizeExpectMatchers(expect, ["durationMs"]);
if (expect["connected"] !== undefined && typeof expect["connected"] !== "boolean") {
issues.push(issue("invalid-type", joinPath(expectPath, "connected"), "必须为布尔值", targetName));
}
if (expect["durationMs"] !== undefined) {
issues.push(...validateValueMatcher(expect["durationMs"], joinPath(expectPath, "durationMs"), targetName));
issues.push(...validateRawValueExpectation(expect["durationMs"], joinPath(expectPath, "durationMs"), targetName));
}
if (expect["banner"] !== undefined) {
@@ -92,7 +89,7 @@ function validateTcpExpect(
issue("invalid-value", joinPath(expectPath, "banner"), "banner 断言需要启用 tcp.readBanner", targetName),
);
} else {
issues.push(...validateContentRules(expect["banner"], joinPath(expectPath, "banner"), targetName));
issues.push(...validateRawContentExpectations(expect["banner"], joinPath(expectPath, "banner"), targetName));
}
}
@@ -111,7 +108,7 @@ function validateTcpTarget(target: Record<string, unknown>, path: string): Confi
const targetName = getTargetName(target);
const tcp = target["tcp"];
if (!isPlainObject(tcp)) {
if (!isPlainRecord(tcp)) {
issues.push(issue("required", joinPath(path, "tcp"), "缺少 tcp 配置分组", targetName));
issues.push(...validateTcpExpect(target, path, false));
return issues;