feat: ValueMatcher 支持 primitive 原始值简写,等价于 { equals: value }
This commit is contained in:
@@ -3,12 +3,14 @@ import { mkdir, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import type { ValueMatcher } from "../../../src/server/checker/expect/types";
|
||||
import type { ResolvedCommandTarget } from "../../../src/server/checker/runner/cmd/types";
|
||||
import type { ResolvedHttpTarget } from "../../../src/server/checker/runner/http/types";
|
||||
import type { ResolvedPingTarget } from "../../../src/server/checker/runner/icmp/types";
|
||||
import type { ResolvedTcpTarget } from "../../../src/server/checker/runner/tcp/types";
|
||||
|
||||
import { loadConfig, parseDuration } from "../../../src/server/checker/config-loader";
|
||||
import { checkValueMatcher } from "../../../src/server/checker/expect/matcher";
|
||||
import { checkerRegistry } from "../../../src/server/checker/runner";
|
||||
import { CommandChecker } from "../../../src/server/checker/runner/cmd/execute";
|
||||
import { HttpChecker } from "../../../src/server/checker/runner/http/execute";
|
||||
@@ -288,6 +290,32 @@ targets:
|
||||
expect(config.targets[0]!.name).toBeNull();
|
||||
});
|
||||
|
||||
test("ValueMatcher primitive 简写在加载时归一化后可运行期匹配", async () => {
|
||||
const configPath = join(tempDir, "matcher-shorthand.yaml");
|
||||
await writeFile(
|
||||
configPath,
|
||||
`targets:
|
||||
- id: "api-health"
|
||||
type: http
|
||||
http:
|
||||
url: "http://example.com"
|
||||
expect:
|
||||
durationMs: 123
|
||||
`,
|
||||
);
|
||||
|
||||
const config = await loadConfig(configPath);
|
||||
const target = config.targets[0]! as ResolvedHttpTarget;
|
||||
|
||||
expect(target.expect?.durationMs).toEqual({ equals: 123 });
|
||||
expect(
|
||||
checkValueMatcher(123, target.expect?.durationMs as ValueMatcher, {
|
||||
path: "durationMs",
|
||||
phase: "duration",
|
||||
}).matched,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("name 为空字符串抛出错误", async () => {
|
||||
const configPath = join(tempDir, "empty-name.yaml");
|
||||
await writeFile(
|
||||
@@ -1076,8 +1104,8 @@ targets:
|
||||
await expect(loadConfig(configPath)).rejects.toThrow("5xx");
|
||||
});
|
||||
|
||||
test("expect.durationMs 非 matcher 抛出错误", async () => {
|
||||
const configPath = join(tempDir, "neg-duration.yaml");
|
||||
test("expect.durationMs 对象简写抛出错误", async () => {
|
||||
const configPath = join(tempDir, "bad-duration-object.yaml");
|
||||
await writeFile(
|
||||
configPath,
|
||||
`targets:
|
||||
@@ -1087,11 +1115,12 @@ targets:
|
||||
http:
|
||||
url: "http://example.com"
|
||||
expect:
|
||||
durationMs: -100
|
||||
durationMs:
|
||||
foo: "bar"
|
||||
`,
|
||||
);
|
||||
// eslint-disable-next-line @typescript-eslint/await-thenable
|
||||
await expect(loadConfig(configPath)).rejects.toThrow("expect.durationMs 必须为 matcher 对象");
|
||||
await expect(loadConfig(configPath)).rejects.toThrow("expect.durationMs.foo 是未知 matcher");
|
||||
});
|
||||
|
||||
test("expect.body 非数组抛出错误", async () => {
|
||||
|
||||
Reference in New Issue
Block a user