1
0

feat: ValueMatcher 支持 primitive 原始值简写,等价于 { equals: value }

This commit is contained in:
2026-05-19 17:07:47 +08:00
parent 8d8549d07f
commit 12cd05b04e
37 changed files with 1836 additions and 1022 deletions

View File

@@ -1,4 +1,4 @@
import type { ContentRules, ValueMatcher } from "../../expect/types";
import type { ContentRules, ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface CommandDefaultsConfig {
@@ -7,7 +7,7 @@ export interface CommandDefaultsConfig {
}
export interface CommandExpectConfig {
durationMs?: ValueMatcher;
durationMs?: ValueMatcherInput;
exitCode?: number[];
stderr?: ContentRules;
stdout?: ContentRules;

View File

@@ -3,6 +3,7 @@ import { isNumber, isPlainObject, 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 { issue, joinPath } from "../../schema/issues";
import { parseSize } from "../../utils";
@@ -41,6 +42,9 @@ function validateCommandExpect(target: Record<string, unknown>, path: string): C
if (expect === undefined || expect === null || !isPlainObject(expect)) return [];
const issues: ConfigValidationIssue[] = [];
const expectPath = joinPath(path, "expect");
normalizeExpectMatchers(expect, ["durationMs"]);
if (expect["stdout"] !== undefined) {
issues.push(...validateContentRules(expect["stdout"], joinPath(expectPath, "stdout"), targetName));
}

View File

@@ -1,12 +1,12 @@
import { isPlainObject } from "es-toolkit";
import type { ExpectResult, KeyValueExpect, ValueMatcher } from "../../expect/types";
import type { ExpectResult, KeyValueExpect, ValueMatcherInput } from "../../expect/types";
import { mismatchFailure } from "../../expect/failure";
import { checkKeyValueExpect } from "../../expect/key-value";
import { checkValueMatcher } from "../../expect/matcher";
export function checkRowCount(actual: number, matcher: ValueMatcher): ExpectResult {
export function checkRowCount(actual: number, matcher: ValueMatcherInput): ExpectResult {
return checkValueMatcher(actual, matcher, {
message: `rowCount ${actual} 不满足条件`,
path: "rowCount",

View File

@@ -1,10 +1,10 @@
import type { ContentRules, KeyValueExpect, ValueMatcher } from "../../expect/types";
import type { ContentRules, KeyValueExpect, ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface DbExpectConfig {
durationMs?: ValueMatcher;
durationMs?: ValueMatcherInput;
result?: ContentRules;
rowCount?: ValueMatcher;
rowCount?: ValueMatcherInput;
rows?: KeyValueExpect[];
}

View File

@@ -3,6 +3,7 @@ import { isPlainObject, isString } from "es-toolkit";
import type { ConfigValidationIssue } from "../../schema/issues";
import type { CheckerValidationInput } from "../types";
import { normalizeExpectMatchers } from "../../expect/normalize";
import { validateContentRules, validateKeyValueExpect, validateValueMatcher } from "../../expect/validate-matcher";
import { issue, joinPath } from "../../schema/issues";
@@ -44,6 +45,8 @@ function validateDbExpect(target: Record<string, unknown>, path: string): Config
const issues: ConfigValidationIssue[] = [];
const expectPath = joinPath(path, "expect");
normalizeExpectMatchers(expect, ["durationMs", "rowCount"]);
if (expect["durationMs"] !== undefined) {
issues.push(...validateValueMatcher(expect["durationMs"], joinPath(expectPath, "durationMs"), targetName));
}

View File

@@ -6,7 +6,7 @@ import type { HttpExpectConfig, HttpTargetConfig, ResolvedHttpTarget } from "./t
import { checkContentRules } from "../../expect/content";
import { errorFailure, mismatchFailure } from "../../expect/failure";
import { checkValueMatcher } from "../../expect/matcher";
import { checkValueMatcher, isValueMatcherObject } from "../../expect/matcher";
import { parseSize } from "../../utils";
import { checkHeaders, checkStatus } from "./expect";
import { httpCheckerSchemas } from "./schema";
@@ -198,7 +198,7 @@ function checkEarlyTimeout(
start: number,
durationMatcher: HttpExpectConfig["durationMs"] | undefined,
): null | { elapsed: number; failure: CheckResult["failure"] } {
if (durationMatcher === undefined) return null;
if (!isValueMatcherObject(durationMatcher)) return null;
const limit = Math.min(
durationMatcher.lte ?? Number.POSITIVE_INFINITY,
durationMatcher.lt ?? Number.POSITIVE_INFINITY,

View File

@@ -1,4 +1,4 @@
import type { ContentRules, KeyValueExpect, ValueMatcher } from "../../expect/types";
import type { ContentRules, KeyValueExpect, ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface HttpDefaultsConfig {
@@ -9,7 +9,7 @@ export interface HttpDefaultsConfig {
export interface HttpExpectConfig {
body?: ContentRules;
durationMs?: ValueMatcher;
durationMs?: ValueMatcherInput;
headers?: KeyValueExpect;
status?: Array<number | string>;
}

View File

@@ -3,6 +3,7 @@ import { isNumber, isString } from "es-toolkit";
import type { ConfigValidationIssue } from "../../schema/issues";
import type { CheckerValidationInput } from "../types";
import { normalizeExpectMatchers } from "../../expect/normalize";
import {
isPlainRecord,
validateContentRules,
@@ -67,6 +68,8 @@ function validateHttpExpect(target: Record<string, unknown>, path: string): Conf
const issues: ConfigValidationIssue[] = [];
const expectPath = joinPath(path, "expect");
normalizeExpectMatchers(expect, ["durationMs"]);
if (isPlainRecord(expect["headers"])) {
issues.push(...validateKeyValueExpect(expect["headers"], joinPath(expectPath, "headers"), targetName));
}

View File

@@ -1,4 +1,4 @@
import type { ExpectResult, ValueMatcher } from "../../expect/types";
import type { ExpectResult, ValueMatcherInput } from "../../expect/types";
import { mismatchFailure } from "../../expect/failure";
import { checkValueMatcher } from "../../expect/matcher";
@@ -17,7 +17,7 @@ export function checkAlive(actual: boolean, expected: boolean): ExpectResult {
};
}
export function checkAvgLatency(actual: null | number, matcher: undefined | ValueMatcher): ExpectResult {
export function checkAvgLatency(actual: null | number, matcher: undefined | ValueMatcherInput): ExpectResult {
return checkValueMatcher(actual, matcher, {
message: "平均延迟不满足条件",
path: "avgLatencyMs",
@@ -25,7 +25,7 @@ export function checkAvgLatency(actual: null | number, matcher: undefined | Valu
});
}
export function checkMaxLatency(actual: null | number, matcher: undefined | ValueMatcher): ExpectResult {
export function checkMaxLatency(actual: null | number, matcher: undefined | ValueMatcherInput): ExpectResult {
return checkValueMatcher(actual, matcher, {
message: "最大延迟不满足条件",
path: "maxLatencyMs",
@@ -33,7 +33,7 @@ export function checkMaxLatency(actual: null | number, matcher: undefined | Valu
});
}
export function checkPacketLoss(actual: number, matcher: undefined | ValueMatcher): ExpectResult {
export function checkPacketLoss(actual: number, matcher: undefined | ValueMatcherInput): ExpectResult {
return checkValueMatcher(actual, matcher, {
message: "丢包率不满足条件",
path: "packetLossPercent",

View File

@@ -1,12 +1,12 @@
import type { ValueMatcher } from "../../expect/types";
import type { ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface PingExpectConfig {
alive?: boolean;
avgLatencyMs?: ValueMatcher;
durationMs?: ValueMatcher;
maxLatencyMs?: ValueMatcher;
packetLossPercent?: ValueMatcher;
avgLatencyMs?: ValueMatcherInput;
durationMs?: ValueMatcherInput;
maxLatencyMs?: ValueMatcherInput;
packetLossPercent?: ValueMatcherInput;
}
export interface PingStats {

View File

@@ -3,6 +3,7 @@ import { isNumber, isPlainObject, isString } from "es-toolkit";
import type { ConfigValidationIssue } from "../../schema/issues";
import type { CheckerValidationInput } from "../types";
import { normalizeExpectMatchers } from "../../expect/normalize";
import { validateValueMatcher } from "../../expect/validate-matcher";
import { issue, joinPath } from "../../schema/issues";
@@ -46,6 +47,8 @@ function validatePingExpect(target: Record<string, unknown>, path: string): Conf
const targetName = getTargetName(target);
const expectPath = joinPath(path, "expect");
normalizeExpectMatchers(expect, ["packetLossPercent", "avgLatencyMs", "maxLatencyMs", "durationMs"]);
if (expect["alive"] !== undefined && typeof expect["alive"] !== "boolean") {
issues.push(issue("invalid-type", joinPath(expectPath, "alive"), "必须为布尔值", targetName));
}

View File

@@ -1,6 +1,6 @@
import type { JSONObject } from "@ai-sdk/provider";
import type { ContentRules, KeyValueExpect, ValueMatcher } from "../../expect/types";
import type { ContentRules, KeyValueExpect, ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface LlmCheckObservation {
@@ -24,11 +24,11 @@ export interface LlmDefaultsConfig {
}
export interface LlmExpectConfig {
durationMs?: ValueMatcher;
finishReason?: ValueMatcher;
durationMs?: ValueMatcherInput;
finishReason?: ValueMatcherInput;
headers?: KeyValueExpect;
output?: ContentRules;
rawFinishReason?: ValueMatcher;
rawFinishReason?: ValueMatcherInput;
status?: Array<number | string>;
stream?: LlmStreamExpect;
usage?: LlmUsageExpect;
@@ -57,7 +57,7 @@ export type LlmProvider = "anthropic" | "openai" | "openai-responses";
export interface LlmStreamExpect {
completed?: boolean;
firstTokenMs?: ValueMatcher;
firstTokenMs?: ValueMatcherInput;
}
export interface LlmStreamObservation {
@@ -80,9 +80,9 @@ export interface LlmTargetConfig {
}
export interface LlmUsageExpect {
inputTokens?: ValueMatcher;
outputTokens?: ValueMatcher;
totalTokens?: ValueMatcher;
inputTokens?: ValueMatcherInput;
outputTokens?: ValueMatcherInput;
totalTokens?: ValueMatcherInput;
}
export interface LlmUsageObservation {

View File

@@ -3,6 +3,7 @@ import { isBoolean, isNumber, isString } from "es-toolkit";
import type { ConfigValidationIssue } from "../../schema/issues";
import type { CheckerValidationInput } from "../types";
import { normalizeExpectMatchers } from "../../expect/normalize";
import {
isPlainRecord,
validateContentRules,
@@ -72,6 +73,8 @@ function validateLlmExpect(
const issues: ConfigValidationIssue[] = [];
const expectPath = joinPath(path, "expect");
normalizeExpectMatchers(expect, ["durationMs", "finishReason", "rawFinishReason"]);
if (Array.isArray(expect["status"])) {
issues.push(...validateStatusValues(expect["status"], joinPath(expectPath, "status"), targetName));
}
@@ -286,6 +289,8 @@ function validateStreamExpect(stream: unknown, path: string, targetName?: string
if (!isPlainRecord(stream)) return [issue("invalid-type", path, "必须为对象", targetName)];
const issues: ConfigValidationIssue[] = [];
normalizeExpectMatchers(stream, ["firstTokenMs"]);
if (stream["completed"] !== undefined && !isBoolean(stream["completed"])) {
issues.push(issue("invalid-type", joinPath(path, "completed"), "必须为布尔值", targetName));
}
@@ -316,6 +321,8 @@ function validateUsageExpect(usage: unknown, path: string, targetName?: string):
if (!isPlainRecord(usage)) return [issue("invalid-type", path, "必须为对象", targetName)];
const issues: ConfigValidationIssue[] = [];
normalizeExpectMatchers(usage, ["inputTokens", "outputTokens", "totalTokens"]);
for (const key of ["inputTokens", "outputTokens", "totalTokens"]) {
if (usage[key] !== undefined) {
issues.push(...validateValueMatcher(usage[key], joinPath(path, key), targetName));

View File

@@ -1,4 +1,4 @@
import type { ContentRules, ValueMatcher } from "../../expect/types";
import type { ContentRules, ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface ResolvedTcpConfig {
@@ -27,7 +27,7 @@ export interface TcpDefaultsConfig {
export interface TcpExpectConfig {
banner?: ContentRules;
connected?: boolean;
durationMs?: ValueMatcher;
durationMs?: ValueMatcherInput;
}
export interface TcpTargetConfig {

View File

@@ -3,6 +3,7 @@ import { isNumber, isPlainObject, 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 { issue, joinPath } from "../../schema/issues";
@@ -75,6 +76,8 @@ function validateTcpExpect(
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));
}

View File

@@ -1,4 +1,4 @@
import type { ContentRules, ExpectResult, ValueMatcher } from "../../expect/types";
import type { ContentRules, ExpectResult, ValueMatcherInput } from "../../expect/types";
import { checkContentRules } from "../../expect/content";
import { mismatchFailure } from "../../expect/failure";
@@ -18,7 +18,7 @@ export function checkResponded(responded: boolean, expected: boolean): ExpectRes
};
}
export function checkResponseSize(size: number, matcher: ValueMatcher): ExpectResult {
export function checkResponseSize(size: number, matcher: ValueMatcherInput): ExpectResult {
return checkValueMatcher(size, matcher, {
message: "响应大小不满足条件",
path: "responseSize",
@@ -30,7 +30,7 @@ export function checkResponseText(text: string, rules: ContentRules): ExpectResu
return checkContentRules(text, rules, { path: "response", phase: "response" });
}
export function checkSourceHost(actual: string, matcher: ValueMatcher): ExpectResult {
export function checkSourceHost(actual: string, matcher: ValueMatcherInput): ExpectResult {
return checkValueMatcher(actual, matcher, {
message: "响应来源地址不满足条件",
path: "sourceHost",
@@ -38,7 +38,7 @@ export function checkSourceHost(actual: string, matcher: ValueMatcher): ExpectRe
});
}
export function checkSourcePort(actual: number, matcher: ValueMatcher): ExpectResult {
export function checkSourcePort(actual: number, matcher: ValueMatcherInput): ExpectResult {
return checkValueMatcher(actual, matcher, {
message: "响应来源端口不满足条件",
path: "sourcePort",

View File

@@ -1,4 +1,4 @@
import type { ContentRules, ValueMatcher } from "../../expect/types";
import type { ContentRules, ValueMatcherInput } from "../../expect/types";
import type { ResolvedTargetBase } from "../../types";
export interface ResolvedUdpConfig {
@@ -29,12 +29,12 @@ export interface UdpDefaultsConfig {
export type UdpEncoding = "base64" | "hex" | "text";
export interface UdpExpectConfig {
durationMs?: ValueMatcher;
durationMs?: ValueMatcherInput;
responded?: boolean;
response?: ContentRules;
responseSize?: ValueMatcher;
sourceHost?: ValueMatcher;
sourcePort?: ValueMatcher;
responseSize?: ValueMatcherInput;
sourceHost?: ValueMatcherInput;
sourcePort?: ValueMatcherInput;
}
export interface UdpTargetConfig {

View File

@@ -3,6 +3,7 @@ import { isNumber, isPlainObject, 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 { issue, joinPath } from "../../schema/issues";
@@ -75,6 +76,8 @@ function validateUdpExpect(target: Record<string, unknown>, path: string): Confi
const expectPath = joinPath(path, "expect");
const responded: unknown = expect["responded"];
normalizeExpectMatchers(expect, ["durationMs", "responseSize", "sourceHost", "sourcePort"]);
if (responded !== undefined && typeof responded !== "boolean") {
issues.push(issue("invalid-type", joinPath(expectPath, "responded"), "必须为布尔值", targetName));
}