refactor: 引入 Checker 统一接口与 Runner 抽象机制
定义 Checker 接口(resolve/execute/serialize)和 CheckerRegistry 注册中心,消除 engine/config-loader/store 中硬编码类型分支。 按 checker 类型分子包(runner/http/、runner/command/),提取 共享 expect 到 runner/shared/。超时控制通过引擎注入 AbortSignal。 CheckFailure.phase 从联合类型改为 string。配置校验下沉到各 Checker.resolve() 内部。 新增 checker-runner-abstraction spec,更新 DEVELOPMENT.md。
This commit is contained in:
@@ -5,6 +5,20 @@ import type { HistoryResponse, SummaryResponse, TargetStatus, HealthResponse } f
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { checkerRegistry } from "../../src/server/checker/runner";
|
||||
import { HttpChecker } from "../../src/server/checker/runner/http/runner";
|
||||
import { CommandChecker } from "../../src/server/checker/runner/command/runner";
|
||||
|
||||
function ensureRegistered() {
|
||||
if (!checkerRegistry.supportedTypes.includes("http")) {
|
||||
checkerRegistry.register(new HttpChecker());
|
||||
checkerRegistry.register(new CommandChecker());
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
ensureRegistered();
|
||||
});
|
||||
|
||||
const staticAssets: StaticAssets = {
|
||||
indexHtml: new Blob(['<!doctype html><title>DiAL</title><div id="root"></div>'], {
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { runCommandCheck } from "../../../src/server/checker/command-runner";
|
||||
import type { ResolvedCommandTarget } from "../../../src/server/checker/types";
|
||||
|
||||
function makeTarget(
|
||||
command: Partial<ResolvedCommandTarget["command"]>,
|
||||
overrides?: Partial<ResolvedCommandTarget>,
|
||||
): ResolvedCommandTarget {
|
||||
return {
|
||||
type: "command",
|
||||
name: "test-cmd",
|
||||
group: "default",
|
||||
command: {
|
||||
exec: "echo",
|
||||
args: ["hello"],
|
||||
cwd: "/tmp",
|
||||
env: {},
|
||||
maxOutputBytes: 1024 * 1024,
|
||||
...command,
|
||||
},
|
||||
intervalMs: 60000,
|
||||
timeoutMs: 5000,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("runCommandCheck", () => {
|
||||
test("exitCode=0 成功", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "true", args: [] }));
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=0");
|
||||
expect(result.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("exitCode=1 不匹配默认 [0]", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "false", args: [] }));
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.statusDetail).toBe("exitCode=1");
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("exitCode");
|
||||
});
|
||||
|
||||
test("exitCode=1 匹配自定义 [1]", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "false", args: [] }, { expect: { exitCode: [1] } }));
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=1");
|
||||
});
|
||||
|
||||
test("命令不存在返回 spawn 错误", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "/nonexistent/command/xyz" }));
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("exitCode");
|
||||
expect(result.failure!.message).toBeTruthy();
|
||||
});
|
||||
|
||||
test("超时返回错误", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "sleep", args: ["10"] }, { timeoutMs: 100 }));
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.message).toContain("超时");
|
||||
});
|
||||
|
||||
test("stdout 输出捕获", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "echo", args: ["hello world"] }));
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 匹配 expect", async () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "echo", args: ["hello"] }, { expect: { stdout: [{ contains: "hello" }] } }),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 不匹配 expect", async () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "echo", args: ["hello"] }, { expect: { stdout: [{ contains: "nonexistent" }] } }),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
test("stderr 匹配 expect", async () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "bash", args: ["-c", "echo error >&2"] }, { expect: { stderr: [{ contains: "error" }] } }),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("输出超过 maxOutputBytes", async () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({
|
||||
exec: "bash",
|
||||
args: ["-c", "yes | head -1000"],
|
||||
maxOutputBytes: 10,
|
||||
}),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.message).toContain("超过限制");
|
||||
});
|
||||
|
||||
test("durationMs 非空", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "true", args: [] }));
|
||||
expect(result.durationMs).not.toBeNull();
|
||||
expect(result.durationMs!).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
test("ls 命令执行成功", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "ls", args: ["/tmp"] }));
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=0");
|
||||
});
|
||||
|
||||
test("不使用 shell,通配符不被展开", async () => {
|
||||
const result = await runCommandCheck(
|
||||
makeTarget({ exec: "echo", args: ["*"] }, { expect: { stdout: [{ contains: "*" }] } }),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("不提供 stdin,等待输入的命令会阻塞超时", async () => {
|
||||
const result = await runCommandCheck(makeTarget({ exec: "bash", args: ["-c", "read line"] }, { timeoutMs: 500 }));
|
||||
expect(result.matched).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,20 @@ import { readRuntimeConfig } from "../../../src/server/config";
|
||||
import { mkdir, rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { checkerRegistry } from "../../../src/server/checker/runner";
|
||||
import { HttpChecker } from "../../../src/server/checker/runner/http/runner";
|
||||
import { CommandChecker } from "../../../src/server/checker/runner/command/runner";
|
||||
|
||||
function ensureRegistered() {
|
||||
if (!checkerRegistry.supportedTypes.includes("http")) {
|
||||
checkerRegistry.register(new HttpChecker());
|
||||
checkerRegistry.register(new CommandChecker());
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
ensureRegistered();
|
||||
});
|
||||
|
||||
describe("parseDuration", () => {
|
||||
test("解析秒", () => {
|
||||
|
||||
@@ -2,6 +2,16 @@ import { describe, expect, test } from "bun:test";
|
||||
import { ProbeEngine } from "../../../src/server/checker/engine";
|
||||
import type { ProbeStore } from "../../../src/server/checker/store";
|
||||
import type { ResolvedCommandTarget, ResolvedHttpTarget, ResolvedTarget } from "../../../src/server/checker/types";
|
||||
import { checkerRegistry } from "../../../src/server/checker/runner";
|
||||
import { HttpChecker } from "../../../src/server/checker/runner/http/runner";
|
||||
import { CommandChecker } from "../../../src/server/checker/runner/command/runner";
|
||||
|
||||
function ensureRegistered() {
|
||||
if (!checkerRegistry.supportedTypes.includes("http")) {
|
||||
checkerRegistry.register(new HttpChecker());
|
||||
checkerRegistry.register(new CommandChecker());
|
||||
}
|
||||
}
|
||||
|
||||
function createMockStore(targetNames: string[]) {
|
||||
let nextId = 1;
|
||||
@@ -49,6 +59,7 @@ function makeCommandTarget(name: string, overrides?: Partial<ResolvedCommandTarg
|
||||
|
||||
describe("ProbeEngine", () => {
|
||||
test("start/stop 不抛错", () => {
|
||||
ensureRegistered();
|
||||
const mockStore = createMockStore(["test"]) as unknown as ProbeStore;
|
||||
const targets: ResolvedTarget[] = [makeCommandTarget("test")];
|
||||
const engine = new ProbeEngine(mockStore, targets);
|
||||
|
||||
@@ -1,168 +0,0 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkCommandExpect } from "../../../../src/server/checker/expect/command";
|
||||
import type { CommandObservation } from "../../../../src/server/checker/expect/command";
|
||||
import type { CommandExpectConfig } from "../../../../src/server/checker/types";
|
||||
|
||||
function obs(overrides: Partial<CommandObservation> = {}): CommandObservation {
|
||||
return {
|
||||
exitCode: 0,
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
durationMs: 100,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("checkCommandExpect", () => {
|
||||
test("无 expect 配置时默认检查 exitCode [0] 匹配成功", () => {
|
||||
const r = checkCommandExpect(obs());
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("无 expect 配置时 exitCode 非 0 匹配失败", () => {
|
||||
const r = checkCommandExpect(obs({ exitCode: 1 }));
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.phase).toBe("exitCode");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
|
||||
test("exitCode 匹配指定退出码", () => {
|
||||
const cfg: CommandExpectConfig = { exitCode: [0, 1] };
|
||||
expect(checkCommandExpect(obs({ exitCode: 0 }), cfg).matched).toBe(true);
|
||||
expect(checkCommandExpect(obs({ exitCode: 1 }), cfg).matched).toBe(true);
|
||||
expect(checkCommandExpect(obs({ exitCode: 2 }), cfg).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("exitCode 不匹配返回 phase=exitCode 的失败", () => {
|
||||
const r = checkCommandExpect(obs({ exitCode: 2 }), { exitCode: [0] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("exitCode");
|
||||
expect(r.failure!.expected).toEqual([0]);
|
||||
expect(r.failure!.actual).toBe(2);
|
||||
});
|
||||
|
||||
test("duration 在限制内匹配成功", () => {
|
||||
const r = checkCommandExpect(obs({ durationMs: 50 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("duration 超过限制匹配失败", () => {
|
||||
const r = checkCommandExpect(obs({ durationMs: 200 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("stdout TextRule 数组匹配", () => {
|
||||
const o = obs({ stdout: "build completed successfully" });
|
||||
expect(checkCommandExpect(o, { stdout: [{ contains: "completed" }] }).matched).toBe(true);
|
||||
expect(checkCommandExpect(o, { stdout: [{ contains: "failed" }] }).matched).toBe(false);
|
||||
expect(checkCommandExpect(o, { stdout: [{ match: "completed.*successfully$" }] }).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 多条规则全部通过", () => {
|
||||
const o = obs({ stdout: "version: 3.2.1, build: ok" });
|
||||
const r = checkCommandExpect(o, {
|
||||
stdout: [{ contains: "version" }, { match: "\\d+\\.\\d+\\.\\d+" }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 第一条规则失败立即返回", () => {
|
||||
const o = obs({ stdout: "error occurred" });
|
||||
const r = checkCommandExpect(o, {
|
||||
stdout: [{ contains: "success" }, { contains: "error" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
expect(r.failure!.path).toBe("stdout[0]");
|
||||
});
|
||||
|
||||
test("stderr TextRule 数组匹配", () => {
|
||||
const o = obs({ stderr: "warning: deprecated" });
|
||||
expect(checkCommandExpect(o, { stderr: [{ contains: "warning" }] }).matched).toBe(true);
|
||||
expect(checkCommandExpect(o, { stderr: [{ contains: "error" }] }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("stdout 失败阻止 stderr 检查", () => {
|
||||
const o = obs({ stdout: "bad output", stderr: "warning message" });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
stdout: [{ contains: "success" }],
|
||||
stderr: [{ contains: "warning" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
test("stdout 通过但 stderr 失败", () => {
|
||||
const o = obs({ stdout: "ok", stderr: "fatal error" });
|
||||
const r = checkCommandExpect(o, {
|
||||
stdout: [{ contains: "ok" }],
|
||||
stderr: [{ equals: "clean" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stderr");
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode->duration->stdout->stderr 全部通过", () => {
|
||||
const o = obs({
|
||||
exitCode: 0,
|
||||
durationMs: 50,
|
||||
stdout: "build success",
|
||||
stderr: "",
|
||||
});
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "success" }],
|
||||
stderr: [{ empty: true }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode 通过但 duration 失败", () => {
|
||||
const o = obs({ exitCode: 0, durationMs: 500 });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "ok" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode/duration 通过但 stdout 失败", () => {
|
||||
const o = obs({ exitCode: 0, durationMs: 50, stdout: "error" });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "success" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
test("完整流水线 exitCode/duration/stdout 通过但 stderr 失败", () => {
|
||||
const o = obs({ exitCode: 0, durationMs: 50, stdout: "ok", stderr: "warning" });
|
||||
const r = checkCommandExpect(o, {
|
||||
exitCode: [0],
|
||||
maxDurationMs: 100,
|
||||
stdout: [{ contains: "ok" }],
|
||||
stderr: [{ empty: true }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stderr");
|
||||
});
|
||||
|
||||
test("stdout 操作符组合", () => {
|
||||
const o = obs({ stdout: "count: 42" });
|
||||
expect(
|
||||
checkCommandExpect(o, {
|
||||
stdout: [{ contains: "count" }, { match: "\\d+" }],
|
||||
}).matched,
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,165 +0,0 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkHttpExpect } from "../../../../src/server/checker/expect/http";
|
||||
import type { HttpObservation } from "../../../../src/server/checker/expect/http";
|
||||
import type { HttpExpectConfig } from "../../../../src/server/checker/types";
|
||||
|
||||
function obs(overrides: Partial<HttpObservation> = {}): HttpObservation {
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {},
|
||||
body: "",
|
||||
durationMs: 100,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("checkHttpExpect", () => {
|
||||
test("无 expect 配置时默认检查 status [200] 匹配成功", () => {
|
||||
const r = checkHttpExpect(obs());
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("无 expect 配置时 status 非 200 匹配失败", () => {
|
||||
const r = checkHttpExpect(obs({ statusCode: 500 }));
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.phase).toBe("status");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
|
||||
test("status 匹配指定状态码", () => {
|
||||
const cfg: HttpExpectConfig = { status: [200, 301] };
|
||||
expect(checkHttpExpect(obs({ statusCode: 200 }), cfg).matched).toBe(true);
|
||||
expect(checkHttpExpect(obs({ statusCode: 301 }), cfg).matched).toBe(true);
|
||||
expect(checkHttpExpect(obs({ statusCode: 404 }), cfg).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("status 不匹配返回 phase=status 的失败", () => {
|
||||
const r = checkHttpExpect(obs({ statusCode: 503 }), { status: [200] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("status");
|
||||
expect(r.failure!.expected).toEqual([200]);
|
||||
expect(r.failure!.actual).toBe(503);
|
||||
});
|
||||
|
||||
test("duration 在限制内匹配成功", () => {
|
||||
const r = checkHttpExpect(obs({ durationMs: 50 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("duration 超过限制匹配失败", () => {
|
||||
const r = checkHttpExpect(obs({ durationMs: 200 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("duration 恰好等于限制匹配成功", () => {
|
||||
const r = checkHttpExpect(obs({ durationMs: 100 }), { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 字符串格式检查(等于)", () => {
|
||||
const o = obs({ headers: { "content-type": "application/json", "x-api": "v1" } });
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": "application/json" } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": "text/html" } }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 操作符格式检查", () => {
|
||||
const o = obs({ headers: { "content-type": "application/json" } });
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": { contains: "json" } } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": { match: "^application/" } } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(o, { headers: { "content-type": { contains: "xml" } } }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 大小写不敏感匹配", () => {
|
||||
const o = obs({ headers: { "content-type": "application/json" } });
|
||||
expect(checkHttpExpect(o, { headers: { "Content-Type": "application/json" } }).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 不存在时返回失败", () => {
|
||||
const o = obs({ headers: {} });
|
||||
const r = checkHttpExpect(o, { headers: { "x-missing": "value" } });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("body 规则数组按顺序检查", () => {
|
||||
const o = obs({ body: JSON.stringify({ status: "ok", count: 5 }) });
|
||||
const r = checkHttpExpect(o, {
|
||||
body: [{ contains: "ok" }, { json: { path: "$.count", gte: 1 } }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("body 第一条规则失败立即返回", () => {
|
||||
const o = obs({ body: "hello world" });
|
||||
const r = checkHttpExpect(o, {
|
||||
body: [{ contains: "missing" }, { contains: "hello" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.path).toBe("body[0]");
|
||||
});
|
||||
|
||||
test("body 为 null 但有 body 规则时报错", () => {
|
||||
const o = obs({ body: null });
|
||||
const r = checkHttpExpect(o, { body: [{ contains: "test" }] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.kind).toBe("error");
|
||||
});
|
||||
|
||||
test("完整流水线 status->duration->headers->body 全部通过", () => {
|
||||
const o = obs({
|
||||
statusCode: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ status: "healthy" }),
|
||||
durationMs: 50,
|
||||
});
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "content-type": { contains: "json" } },
|
||||
body: [{ json: { path: "$.status", equals: "healthy" } }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("完整流水线 status 通过但 duration 失败", () => {
|
||||
const o = obs({ statusCode: 200, durationMs: 500 });
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("完整流水线 status 和 duration 通过但 headers 失败", () => {
|
||||
const o = obs({ statusCode: 200, durationMs: 50, headers: { "x-api": "v1" } });
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "x-api": "v2" },
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("完整流水线 status/duration/headers 通过但 body 失败", () => {
|
||||
const o = obs({
|
||||
statusCode: 200,
|
||||
durationMs: 50,
|
||||
headers: { "content-type": "text/plain" },
|
||||
body: "error occurred",
|
||||
});
|
||||
const r = checkHttpExpect(o, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "content-type": "text/plain" },
|
||||
body: [{ contains: "success" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("body");
|
||||
});
|
||||
});
|
||||
25
tests/server/checker/runner/command/expect.test.ts
Normal file
25
tests/server/checker/runner/command/expect.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkExitCode } from "../../../../../src/server/checker/runner/command/expect";
|
||||
|
||||
describe("checkExitCode", () => {
|
||||
test("exitCode 在允许列表中匹配成功", () => {
|
||||
const r = checkExitCode(0, [0]);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("exitCode 不在允许列表中匹配失败", () => {
|
||||
const r = checkExitCode(1, [0]);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("exitCode");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
expect(r.failure!.expected).toEqual([0]);
|
||||
expect(r.failure!.actual).toBe(1);
|
||||
});
|
||||
|
||||
test("多个允许退出码", () => {
|
||||
expect(checkExitCode(0, [0, 1]).matched).toBe(true);
|
||||
expect(checkExitCode(1, [0, 1]).matched).toBe(true);
|
||||
expect(checkExitCode(2, [0, 1]).matched).toBe(false);
|
||||
});
|
||||
});
|
||||
116
tests/server/checker/runner/command/runner.test.ts
Normal file
116
tests/server/checker/runner/command/runner.test.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { CommandChecker } from "../../../../../src/server/checker/runner/command/runner";
|
||||
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
|
||||
import type { ResolvedCommandTarget } from "../../../../../src/server/checker/types";
|
||||
|
||||
const checker = new CommandChecker();
|
||||
|
||||
function makeTarget(
|
||||
command: Partial<ResolvedCommandTarget["command"]>,
|
||||
overrides?: Partial<ResolvedCommandTarget>,
|
||||
): ResolvedCommandTarget {
|
||||
return {
|
||||
type: "command",
|
||||
name: "test-cmd",
|
||||
group: "default",
|
||||
command: {
|
||||
exec: "echo",
|
||||
args: ["hello"],
|
||||
cwd: "/tmp",
|
||||
env: {},
|
||||
maxOutputBytes: 1024 * 1024,
|
||||
...command,
|
||||
},
|
||||
intervalMs: 60000,
|
||||
timeoutMs: 5000,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeCtx(timeoutMs = 5000): CheckerContext {
|
||||
const controller = new AbortController();
|
||||
setTimeout(() => controller.abort(), timeoutMs);
|
||||
return { signal: controller.signal };
|
||||
}
|
||||
|
||||
describe("CommandChecker", () => {
|
||||
test("exitCode=0 成功", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "true", args: [] }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=0");
|
||||
expect(result.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("exitCode=1 不匹配默认 [0]", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "false", args: [] }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.statusDetail).toBe("exitCode=1");
|
||||
expect(result.failure!.phase).toBe("exitCode");
|
||||
});
|
||||
|
||||
test("exitCode=1 匹配自定义 [1]", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "false", args: [] }, { expect: { exitCode: [1] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("exitCode=1");
|
||||
});
|
||||
|
||||
test("命令不存在返回 spawn 错误", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "/nonexistent/command/xyz" }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("exitCode");
|
||||
expect(result.failure!.message).toBeTruthy();
|
||||
});
|
||||
|
||||
test("超时返回错误", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "sleep", args: ["10"] }, { timeoutMs: 100 }), makeCtx(100));
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.message).toContain("超时");
|
||||
});
|
||||
|
||||
test("stdout 输出捕获", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "echo", args: ["hello world"] }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 匹配 expect", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "echo", args: ["hello"] }, { expect: { stdout: [{ contains: "hello" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("stdout 不匹配 expect", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "echo", args: ["hello"] }, { expect: { stdout: [{ contains: "nonexistent" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("stdout");
|
||||
});
|
||||
|
||||
test("stderr 匹配 expect", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "bash", args: ["-c", "echo error >&2"] }, { expect: { stderr: [{ contains: "error" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("输出超过 maxOutputBytes", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "bash", args: ["-c", "yes | head -1000"], maxOutputBytes: 10 }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.message).toContain("超过限制");
|
||||
});
|
||||
|
||||
test("durationMs 非空", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "true", args: [] }), makeCtx());
|
||||
expect(result.durationMs).not.toBeNull();
|
||||
expect(result.durationMs!).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
test("不使用 shell,通配符不被展开", async () => {
|
||||
const result = await checker.execute(makeTarget({ exec: "echo", args: ["*"] }, { expect: { stdout: [{ contains: "*" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("serialize 返回命令摘要和 config JSON", () => {
|
||||
const target = makeTarget({ exec: "echo", args: ["hello"] });
|
||||
const s = checker.serialize(target);
|
||||
expect(s.target).toBe("exec echo hello");
|
||||
const config = JSON.parse(s.config);
|
||||
expect(config.exec).toBe("echo");
|
||||
expect(config.args).toEqual(["hello"]);
|
||||
});
|
||||
});
|
||||
142
tests/server/checker/runner/http/expect.test.ts
Normal file
142
tests/server/checker/runner/http/expect.test.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkHttpExpect } from "../../../../../src/server/checker/runner/http/expect";
|
||||
|
||||
function obs(overrides: { statusCode?: number; headers?: Record<string, string>; body?: string | null; durationMs?: number } = {}) {
|
||||
return {
|
||||
statusCode: overrides.statusCode ?? 200,
|
||||
headers: overrides.headers ?? {},
|
||||
body: overrides.body ?? "",
|
||||
durationMs: overrides.durationMs ?? 100,
|
||||
};
|
||||
}
|
||||
|
||||
describe("checkHttpExpect", () => {
|
||||
test("无 expect 配置时默认检查 status [200] 匹配成功", () => {
|
||||
const r = checkHttpExpect(obs().statusCode, obs().headers, obs().body as string, obs().durationMs);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("无 expect 配置时 status 非 200 匹配失败", () => {
|
||||
const r = checkHttpExpect(500, {}, "", 100);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.phase).toBe("status");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
|
||||
test("status 匹配指定状态码", () => {
|
||||
const cfg = { status: [200, 301] };
|
||||
expect(checkHttpExpect(200, {}, "", 100, cfg).matched).toBe(true);
|
||||
expect(checkHttpExpect(301, {}, "", 100, cfg).matched).toBe(true);
|
||||
expect(checkHttpExpect(404, {}, "", 100, cfg).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("status 不匹配返回 phase=status 的失败", () => {
|
||||
const r = checkHttpExpect(503, {}, "", 100, { status: [200] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("status");
|
||||
expect(r.failure!.expected).toEqual([200]);
|
||||
expect(r.failure!.actual).toBe(503);
|
||||
});
|
||||
|
||||
test("duration 在限制内匹配成功", () => {
|
||||
const r = checkHttpExpect(200, {}, "", 50, { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("duration 超过限制匹配失败", () => {
|
||||
const r = checkHttpExpect(200, {}, "", 200, { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("duration 恰好等于限制匹配成功", () => {
|
||||
const r = checkHttpExpect(200, {}, "", 100, { maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 字符串格式检查(等于)", () => {
|
||||
const h = { "content-type": "application/json", "x-api": "v1" };
|
||||
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": "application/json" } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": "text/html" } }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 操作符格式检查", () => {
|
||||
const h = { "content-type": "application/json" };
|
||||
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { contains: "json" } } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { match: "^application/" } } }).matched).toBe(true);
|
||||
expect(checkHttpExpect(200, h, "", 100, { headers: { "content-type": { contains: "xml" } } }).matched).toBe(false);
|
||||
});
|
||||
|
||||
test("headers 大小写不敏感匹配", () => {
|
||||
const h = { "content-type": "application/json" };
|
||||
expect(checkHttpExpect(200, h, "", 100, { headers: { "Content-Type": "application/json" } }).matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 不存在时返回失败", () => {
|
||||
const r = checkHttpExpect(200, {}, "", 100, { headers: { "x-missing": "value" } });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("body 规则数组按顺序检查", () => {
|
||||
const body = JSON.stringify({ status: "ok", count: 5 });
|
||||
const r = checkHttpExpect(200, {}, body, 100, {
|
||||
body: [{ contains: "ok" }, { json: { path: "$.count", gte: 1 } }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("body 第一条规则失败立即返回", () => {
|
||||
const r = checkHttpExpect(200, {}, "hello world", 100, {
|
||||
body: [{ contains: "missing" }, { contains: "hello" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.path).toBe("body[0]");
|
||||
});
|
||||
|
||||
test("body 为 null 但有 body 规则时报错", () => {
|
||||
const r = checkHttpExpect(200, {}, null, 100, { body: [{ contains: "test" }] });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.kind).toBe("error");
|
||||
});
|
||||
|
||||
test("完整流水线 status->duration->headers->body 全部通过", () => {
|
||||
const r = checkHttpExpect(200, { "content-type": "application/json" }, JSON.stringify({ status: "healthy" }), 50, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "content-type": { contains: "json" } },
|
||||
body: [{ json: { path: "$.status", equals: "healthy" } }],
|
||||
});
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("完整流水线 status 通过但 duration 失败", () => {
|
||||
const r = checkHttpExpect(200, {}, "", 500, { status: [200], maxDurationMs: 100 });
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
});
|
||||
|
||||
test("完整流水线 status 和 duration 通过但 headers 失败", () => {
|
||||
const r = checkHttpExpect(200, { "x-api": "v1" }, "", 50, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "x-api": "v2" },
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("完整流水线 status/duration/headers 通过但 body 失败", () => {
|
||||
const r = checkHttpExpect(200, { "content-type": "text/plain" }, "error occurred", 50, {
|
||||
status: [200],
|
||||
maxDurationMs: 100,
|
||||
headers: { "content-type": "text/plain" },
|
||||
body: [{ contains: "success" }],
|
||||
});
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("body");
|
||||
});
|
||||
});
|
||||
@@ -1,15 +1,11 @@
|
||||
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
||||
import { runHttpCheck } from "../../../src/server/checker/fetcher";
|
||||
import { HttpChecker } from "../../../../../src/server/checker/runner/http/runner";
|
||||
import type { CheckerContext } from "../../../../../src/server/checker/runner/types";
|
||||
import type { ResolvedHttpTarget } from "../../../../../src/server/checker/types";
|
||||
|
||||
describe("runHttpCheck", () => {
|
||||
test("checkExpect 已移除", async () => {
|
||||
const mod = await import("../../../src/server/checker/fetcher");
|
||||
expect((mod as Record<string, unknown>).checkExpect).toBeUndefined();
|
||||
expect((mod as Record<string, unknown>).fetchTarget).toBeUndefined();
|
||||
});
|
||||
});
|
||||
const checker = new HttpChecker();
|
||||
|
||||
describe("runHttpCheck 集成", () => {
|
||||
describe("HttpChecker", () => {
|
||||
let server: ReturnType<typeof Bun.serve>;
|
||||
let baseUrl: string;
|
||||
|
||||
@@ -35,8 +31,6 @@ describe("runHttpCheck 集成", () => {
|
||||
return new Response("x".repeat(2000));
|
||||
case "/notfound":
|
||||
return new Response("not found", { status: 404 });
|
||||
case "/slow":
|
||||
return new Response("slow", { status: 200 });
|
||||
default:
|
||||
return new Response("ok");
|
||||
}
|
||||
@@ -57,26 +51,32 @@ describe("runHttpCheck 集成", () => {
|
||||
expect?: Record<string, unknown>;
|
||||
maxBodyBytes?: number;
|
||||
timeoutMs?: number;
|
||||
}) {
|
||||
}): ResolvedHttpTarget {
|
||||
return {
|
||||
type: "http" as const,
|
||||
type: "http",
|
||||
name: "test-http",
|
||||
group: "default",
|
||||
http: {
|
||||
url: overrides.url ?? `${baseUrl}/ok`,
|
||||
method: overrides.method ?? "GET",
|
||||
headers: overrides.headers ?? ({} as Record<string, string>),
|
||||
headers: overrides.headers ?? {},
|
||||
body: overrides.body,
|
||||
maxBodyBytes: overrides.maxBodyBytes ?? 1024 * 1024,
|
||||
},
|
||||
intervalMs: 60000,
|
||||
timeoutMs: overrides.timeoutMs ?? 5000,
|
||||
expect: overrides.expect as import("../../../src/server/checker/types").HttpExpectConfig | undefined,
|
||||
expect: overrides.expect as ResolvedHttpTarget["expect"],
|
||||
};
|
||||
}
|
||||
|
||||
function makeCtx(timeoutMs = 5000): CheckerContext {
|
||||
const controller = new AbortController();
|
||||
setTimeout(() => controller.abort(), timeoutMs);
|
||||
return { signal: controller.signal };
|
||||
}
|
||||
|
||||
test("成功请求 200", async () => {
|
||||
const result = await runHttpCheck(makeTarget({ url: `${baseUrl}/ok` }));
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok` }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
expect(result.statusDetail).toBe("HTTP 200");
|
||||
expect(result.durationMs).not.toBeNull();
|
||||
@@ -84,85 +84,47 @@ describe("runHttpCheck 集成", () => {
|
||||
});
|
||||
|
||||
test("404 不匹配默认 status [200]", async () => {
|
||||
const result = await runHttpCheck(makeTarget({ url: `${baseUrl}/notfound` }));
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound` }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.statusDetail).toBe("HTTP 404");
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
});
|
||||
|
||||
test("404 匹配自定义 status [404]", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/notfound`,
|
||||
expect: { status: [404] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound`, expect: { status: [404] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 检查通过", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { headers: { "x-custom": "test-value" } },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { headers: { "x-custom": "test-value" } } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("headers 检查失败", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { headers: { "x-custom": "wrong-value" } },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { headers: { "x-custom": "wrong-value" } } }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("body contains 检查", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { body: [{ contains: "hello" }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { body: [{ contains: "hello" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("body contains 失败", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { body: [{ contains: "nonexistent" }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { body: [{ contains: "nonexistent" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("body");
|
||||
});
|
||||
|
||||
test("body json 检查", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/json`,
|
||||
expect: { body: [{ json: { path: "$.status", equals: "ok" } }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/json`, expect: { body: [{ json: { path: "$.status", equals: "ok" } }] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("响应体超过 maxBodyBytes", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/large`,
|
||||
maxBodyBytes: 100,
|
||||
expect: { body: [{ contains: "x" }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/large`, maxBodyBytes: 100, expect: { body: [{ contains: "x" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.phase).toBe("body");
|
||||
expect(result.failure!.message).toContain("超过限制");
|
||||
});
|
||||
@@ -177,14 +139,8 @@ describe("runHttpCheck 集成", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `http://localhost:${timeoutServer.port}/`,
|
||||
timeoutMs: 100,
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `http://localhost:${timeoutServer.port}/`, timeoutMs: 100 }), makeCtx(100));
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure).not.toBeNull();
|
||||
expect(result.failure!.message).toContain("超时");
|
||||
} finally {
|
||||
timeoutServer.stop();
|
||||
@@ -192,63 +148,33 @@ describe("runHttpCheck 集成", () => {
|
||||
});
|
||||
|
||||
test("快速失败:status 失败时不读取 body", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/notfound`,
|
||||
expect: { status: [200], body: [{ contains: "something" }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/notfound`, expect: { status: [200], body: [{ contains: "something" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("status");
|
||||
});
|
||||
|
||||
test("快速失败:headers 失败时不读取 body", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { headers: { "x-missing": "value" }, body: [{ contains: "hello" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("headers");
|
||||
});
|
||||
|
||||
test("status 通过但 body 失败", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { status: [200], body: [{ contains: "not-in-body" }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: { status: [200], body: [{ contains: "not-in-body" }] } }), makeCtx());
|
||||
expect(result.matched).toBe(false);
|
||||
expect(result.failure!.phase).toBe("body");
|
||||
});
|
||||
|
||||
test("无 expect 时默认检查 status 200", async () => {
|
||||
const result = await runHttpCheck(makeTarget({ url: `${baseUrl}/ok`, expect: undefined }));
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/ok`, expect: undefined }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("POST 请求携带 body", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/echo`,
|
||||
method: "POST",
|
||||
body: "test-body",
|
||||
headers: { "content-type": "text/plain" },
|
||||
expect: { status: [200], body: [{ json: { path: "$.body", equals: "present" } }] },
|
||||
}),
|
||||
);
|
||||
const result = await checker.execute(makeTarget({ url: `${baseUrl}/echo`, method: "POST", body: "test-body", headers: { "content-type": "text/plain" }, expect: { status: [200], body: [{ json: { path: "$.body", equals: "present" } }] } }), makeCtx());
|
||||
expect(result.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("仅 contains 规则时不解析 JSON", async () => {
|
||||
const result = await runHttpCheck(
|
||||
makeTarget({
|
||||
url: `${baseUrl}/ok`,
|
||||
expect: { body: [{ contains: "hello world" }] },
|
||||
}),
|
||||
);
|
||||
expect(result.matched).toBe(true);
|
||||
test("serialize 返回 URL 和 config JSON", () => {
|
||||
const target = makeTarget({});
|
||||
const s = checker.serialize(target);
|
||||
expect(s.target).toBe(target.http.url);
|
||||
const config = JSON.parse(s.config);
|
||||
expect(config.url).toBe(target.http.url);
|
||||
expect(config.method).toBe("GET");
|
||||
});
|
||||
});
|
||||
39
tests/server/checker/runner/registry.test.ts
Normal file
39
tests/server/checker/runner/registry.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { CheckerRegistry } from "../../../../src/server/checker/runner/registry";
|
||||
import type { Checker } from "../../../../src/server/checker/runner/types";
|
||||
|
||||
function createChecker(type: string): Checker {
|
||||
return {
|
||||
type,
|
||||
resolve: () => ({}) as any,
|
||||
execute: () => Promise.resolve({} as any),
|
||||
serialize: () => ({ target: "", config: "" }),
|
||||
};
|
||||
}
|
||||
|
||||
describe("CheckerRegistry", () => {
|
||||
test("注册并获取 Checker", () => {
|
||||
const registry = new CheckerRegistry();
|
||||
const checker = createChecker("http");
|
||||
registry.register(checker);
|
||||
expect(registry.get("http")).toBe(checker);
|
||||
});
|
||||
|
||||
test("获取未注册的 type 抛出错误", () => {
|
||||
const registry = new CheckerRegistry();
|
||||
expect(() => registry.get("unknown")).toThrow("不支持的 probe type");
|
||||
});
|
||||
|
||||
test("重复注册同一 type 抛出错误", () => {
|
||||
const registry = new CheckerRegistry();
|
||||
registry.register(createChecker("http"));
|
||||
expect(() => registry.register(createChecker("http"))).toThrow("已注册");
|
||||
});
|
||||
|
||||
test("查询支持的 type 列表", () => {
|
||||
const registry = new CheckerRegistry();
|
||||
registry.register(createChecker("http"));
|
||||
registry.register(createChecker("command"));
|
||||
expect(registry.supportedTypes).toEqual(["http", "command"]);
|
||||
});
|
||||
});
|
||||
@@ -1,149 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import {
|
||||
applyOperator,
|
||||
checkBodyExpect,
|
||||
checkExpectValue,
|
||||
evaluateJsonPath,
|
||||
} from "../../../../src/server/checker/expect/body";
|
||||
|
||||
describe("evaluateJsonPath", () => {
|
||||
const obj = {
|
||||
status: "ok",
|
||||
code: 0,
|
||||
active: true,
|
||||
error: null,
|
||||
data: {
|
||||
count: 42,
|
||||
items: [{ name: "a" }, { name: "b" }],
|
||||
nested: { deep: "value" },
|
||||
},
|
||||
emptyObj: {},
|
||||
emptyArr: [],
|
||||
};
|
||||
|
||||
test("简单字段访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.status")).toBe("ok");
|
||||
expect(evaluateJsonPath(obj, "$.code")).toBe(0);
|
||||
expect(evaluateJsonPath(obj, "$.active")).toBe(true);
|
||||
expect(evaluateJsonPath(obj, "$.error")).toBeNull();
|
||||
});
|
||||
|
||||
test("嵌套对象访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.count")).toBe(42);
|
||||
expect(evaluateJsonPath(obj, "$.data.nested.deep")).toBe("value");
|
||||
});
|
||||
|
||||
test("数组索引访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.items[0].name")).toBe("a");
|
||||
expect(evaluateJsonPath(obj, "$.data.items[1].name")).toBe("b");
|
||||
});
|
||||
|
||||
test("路径不存在返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "$.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.items[99]")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("空对象和空数组", () => {
|
||||
expect(evaluateJsonPath(obj, "$.emptyObj")).toEqual({});
|
||||
expect(evaluateJsonPath(obj, "$.emptyArr")).toEqual([]);
|
||||
});
|
||||
|
||||
test("非 $ 开头路径返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "status")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, ".status")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("null 对象上访问", () => {
|
||||
expect(evaluateJsonPath(null, "$.any")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyOperator", () => {
|
||||
test("equals 操作符", () => {
|
||||
expect(applyOperator("ok", { equals: "ok" })).toBe(true);
|
||||
expect(applyOperator("ok", { equals: "error" })).toBe(false);
|
||||
expect(applyOperator(42, { equals: 42 })).toBe(true);
|
||||
expect(applyOperator(42, { equals: 41 })).toBe(false);
|
||||
expect(applyOperator(null, { equals: null })).toBe(true);
|
||||
expect(applyOperator(true, { equals: true })).toBe(true);
|
||||
});
|
||||
|
||||
test("contains 操作符", () => {
|
||||
expect(applyOperator("hello world", { contains: "hello" })).toBe(true);
|
||||
expect(applyOperator("hello world", { contains: "missing" })).toBe(false);
|
||||
expect(applyOperator(12345, { contains: "23" })).toBe(true);
|
||||
});
|
||||
|
||||
test("match 操作符", () => {
|
||||
expect(applyOperator("v2.1.0", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
|
||||
expect(applyOperator("v2.1", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
|
||||
expect(applyOperator("abc123", { match: "^\\w+\\d+$" })).toBe(true);
|
||||
});
|
||||
|
||||
test("empty 操作符", () => {
|
||||
expect(applyOperator("", { empty: true })).toBe(true);
|
||||
expect(applyOperator(null, { empty: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { empty: true })).toBe(true);
|
||||
expect(applyOperator([], { empty: true })).toBe(true);
|
||||
expect(applyOperator({}, { empty: true })).toBe(true);
|
||||
expect(applyOperator("ok", { empty: true })).toBe(false);
|
||||
expect(applyOperator([1, 2], { empty: false })).toBe(true);
|
||||
expect(applyOperator([], { empty: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("exists 操作符", () => {
|
||||
expect(applyOperator("ok", { exists: true })).toBe(true);
|
||||
expect(applyOperator(null, { exists: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { exists: true })).toBe(false);
|
||||
expect(applyOperator(undefined, { exists: false })).toBe(true);
|
||||
expect(applyOperator("ok", { exists: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("gte 操作符", () => {
|
||||
expect(applyOperator(10, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5 })).toBe(false);
|
||||
expect(applyOperator("10", { gte: 5 })).toBe(true);
|
||||
});
|
||||
|
||||
test("lte 操作符", () => {
|
||||
expect(applyOperator(3, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(10, { lte: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("gt 操作符", () => {
|
||||
expect(applyOperator(10, { gt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("lt 操作符", () => {
|
||||
expect(applyOperator(3, { lt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("多操作符 AND 组合", () => {
|
||||
expect(applyOperator(7, { gte: 5, lte: 10 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5, lte: 10 })).toBe(false);
|
||||
expect(applyOperator(15, { gte: 5, lte: 10 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkExpectValue", () => {
|
||||
test("原始值直接比较", () => {
|
||||
expect(checkExpectValue("ok", "ok")).toBe(true);
|
||||
expect(checkExpectValue("ok", "error")).toBe(false);
|
||||
expect(checkExpectValue(42, 42)).toBe(true);
|
||||
expect(checkExpectValue(null, null)).toBe(true);
|
||||
});
|
||||
|
||||
test("对象作为操作符", () => {
|
||||
expect(checkExpectValue(42, { gte: 10 })).toBe(true);
|
||||
expect(checkExpectValue(42, { gte: 100 })).toBe(false);
|
||||
expect(checkExpectValue("hello", { contains: "ell" })).toBe(true);
|
||||
});
|
||||
});
|
||||
import { checkBodyExpect } from "../../../../../src/server/checker/runner/shared/body";
|
||||
|
||||
describe("checkBodyExpect (BodyRule[])", () => {
|
||||
test("无规则返回匹配成功", () => {
|
||||
@@ -234,11 +90,6 @@ describe("checkBodyExpect (BodyRule[])", () => {
|
||||
expect(
|
||||
checkBodyExpect(html, [{ css: { selector: 'meta[name="version"]', attr: "content", equals: "2.0.1" } }]).matched,
|
||||
).toBe(true);
|
||||
expect(
|
||||
checkBodyExpect(html, [
|
||||
{ css: { selector: 'meta[name="version"]', attr: "content", match: "\\d+\\.\\d+\\.\\d+" } },
|
||||
]).matched,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("css exists 检查", () => {
|
||||
29
tests/server/checker/runner/shared/duration.test.ts
Normal file
29
tests/server/checker/runner/shared/duration.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkDuration } from "../../../../../src/server/checker/runner/shared/duration";
|
||||
|
||||
describe("checkDuration", () => {
|
||||
test("未配置 maxDurationMs 返回匹配成功", () => {
|
||||
const r = checkDuration(100);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("duration 在限制内匹配成功", () => {
|
||||
const r = checkDuration(50, 100);
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("duration 等于限制匹配成功", () => {
|
||||
const r = checkDuration(100, 100);
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("duration 超过限制匹配失败", () => {
|
||||
const r = checkDuration(200, 100);
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure).not.toBeNull();
|
||||
expect(r.failure!.phase).toBe("duration");
|
||||
expect(r.failure!.kind).toBe("mismatch");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { truncateActual, mismatchFailure, errorFailure } from "../../../../src/server/checker/expect/failure";
|
||||
import { truncateActual, mismatchFailure, errorFailure } from "../../../../../src/server/checker/runner/shared/failure";
|
||||
|
||||
describe("truncateActual", () => {
|
||||
test("短字符串不截断", () => {
|
||||
141
tests/server/checker/runner/shared/operator.test.ts
Normal file
141
tests/server/checker/runner/shared/operator.test.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { applyOperator, checkExpectValue, evaluateJsonPath } from "../../../../../src/server/checker/runner/shared/operator";
|
||||
|
||||
describe("evaluateJsonPath", () => {
|
||||
const obj = {
|
||||
status: "ok",
|
||||
code: 0,
|
||||
active: true,
|
||||
error: null,
|
||||
data: {
|
||||
count: 42,
|
||||
items: [{ name: "a" }, { name: "b" }],
|
||||
nested: { deep: "value" },
|
||||
},
|
||||
emptyObj: {},
|
||||
emptyArr: [],
|
||||
};
|
||||
|
||||
test("简单字段访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.status")).toBe("ok");
|
||||
expect(evaluateJsonPath(obj, "$.code")).toBe(0);
|
||||
expect(evaluateJsonPath(obj, "$.active")).toBe(true);
|
||||
expect(evaluateJsonPath(obj, "$.error")).toBeNull();
|
||||
});
|
||||
|
||||
test("嵌套对象访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.count")).toBe(42);
|
||||
expect(evaluateJsonPath(obj, "$.data.nested.deep")).toBe("value");
|
||||
});
|
||||
|
||||
test("数组索引访问", () => {
|
||||
expect(evaluateJsonPath(obj, "$.data.items[0].name")).toBe("a");
|
||||
expect(evaluateJsonPath(obj, "$.data.items[1].name")).toBe("b");
|
||||
});
|
||||
|
||||
test("路径不存在返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "$.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.notExist")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, "$.data.items[99]")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("空对象和空数组", () => {
|
||||
expect(evaluateJsonPath(obj, "$.emptyObj")).toEqual({});
|
||||
expect(evaluateJsonPath(obj, "$.emptyArr")).toEqual([]);
|
||||
});
|
||||
|
||||
test("非 $ 开头路径返回 undefined", () => {
|
||||
expect(evaluateJsonPath(obj, "status")).toBeUndefined();
|
||||
expect(evaluateJsonPath(obj, ".status")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("null 对象上访问", () => {
|
||||
expect(evaluateJsonPath(null, "$.any")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyOperator", () => {
|
||||
test("equals 操作符", () => {
|
||||
expect(applyOperator("ok", { equals: "ok" })).toBe(true);
|
||||
expect(applyOperator("ok", { equals: "error" })).toBe(false);
|
||||
expect(applyOperator(42, { equals: 42 })).toBe(true);
|
||||
expect(applyOperator(42, { equals: 41 })).toBe(false);
|
||||
expect(applyOperator(null, { equals: null })).toBe(true);
|
||||
expect(applyOperator(true, { equals: true })).toBe(true);
|
||||
});
|
||||
|
||||
test("contains 操作符", () => {
|
||||
expect(applyOperator("hello world", { contains: "hello" })).toBe(true);
|
||||
expect(applyOperator("hello world", { contains: "missing" })).toBe(false);
|
||||
expect(applyOperator(12345, { contains: "23" })).toBe(true);
|
||||
});
|
||||
|
||||
test("match 操作符", () => {
|
||||
expect(applyOperator("v2.1.0", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(true);
|
||||
expect(applyOperator("v2.1", { match: "\\d+\\.\\d+\\.\\d+" })).toBe(false);
|
||||
expect(applyOperator("abc123", { match: "^\\w+\\d+$" })).toBe(true);
|
||||
});
|
||||
|
||||
test("empty 操作符", () => {
|
||||
expect(applyOperator("", { empty: true })).toBe(true);
|
||||
expect(applyOperator(null, { empty: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { empty: true })).toBe(true);
|
||||
expect(applyOperator([], { empty: true })).toBe(true);
|
||||
expect(applyOperator({}, { empty: true })).toBe(true);
|
||||
expect(applyOperator("ok", { empty: true })).toBe(false);
|
||||
expect(applyOperator([1, 2], { empty: false })).toBe(true);
|
||||
expect(applyOperator([], { empty: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("exists 操作符", () => {
|
||||
expect(applyOperator("ok", { exists: true })).toBe(true);
|
||||
expect(applyOperator(null, { exists: true })).toBe(true);
|
||||
expect(applyOperator(undefined, { exists: true })).toBe(false);
|
||||
expect(applyOperator(undefined, { exists: false })).toBe(true);
|
||||
expect(applyOperator("ok", { exists: false })).toBe(false);
|
||||
});
|
||||
|
||||
test("gte 操作符", () => {
|
||||
expect(applyOperator(10, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gte: 5 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5 })).toBe(false);
|
||||
expect(applyOperator("10", { gte: 5 })).toBe(true);
|
||||
});
|
||||
|
||||
test("lte 操作符", () => {
|
||||
expect(applyOperator(3, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lte: 5 })).toBe(true);
|
||||
expect(applyOperator(10, { lte: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("gt 操作符", () => {
|
||||
expect(applyOperator(10, { gt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { gt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("lt 操作符", () => {
|
||||
expect(applyOperator(3, { lt: 5 })).toBe(true);
|
||||
expect(applyOperator(5, { lt: 5 })).toBe(false);
|
||||
});
|
||||
|
||||
test("多操作符 AND 组合", () => {
|
||||
expect(applyOperator(7, { gte: 5, lte: 10 })).toBe(true);
|
||||
expect(applyOperator(3, { gte: 5, lte: 10 })).toBe(false);
|
||||
expect(applyOperator(15, { gte: 5, lte: 10 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkExpectValue", () => {
|
||||
test("原始值直接比较", () => {
|
||||
expect(checkExpectValue("ok", "ok")).toBe(true);
|
||||
expect(checkExpectValue("ok", "error")).toBe(false);
|
||||
expect(checkExpectValue(42, 42)).toBe(true);
|
||||
expect(checkExpectValue(null, null)).toBe(true);
|
||||
});
|
||||
|
||||
test("对象作为操作符", () => {
|
||||
expect(checkExpectValue(42, { gte: 10 })).toBe(true);
|
||||
expect(checkExpectValue(42, { gte: 100 })).toBe(false);
|
||||
expect(checkExpectValue("hello", { contains: "ell" })).toBe(true);
|
||||
});
|
||||
});
|
||||
45
tests/server/checker/runner/shared/text.test.ts
Normal file
45
tests/server/checker/runner/shared/text.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { checkTextRules } from "../../../../../src/server/checker/runner/shared/text";
|
||||
|
||||
describe("checkTextRules", () => {
|
||||
test("无规则返回匹配成功", () => {
|
||||
const r = checkTextRules("hello", [], "stdout");
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("单条 contains 规则匹配成功", () => {
|
||||
const r = checkTextRules("build completed successfully", [{ contains: "completed" }], "stdout");
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("单条 contains 规则匹配失败", () => {
|
||||
const r = checkTextRules("build completed successfully", [{ contains: "failed" }], "stdout");
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
expect(r.failure!.path).toBe("stdout[0]");
|
||||
});
|
||||
|
||||
test("多条规则全部通过", () => {
|
||||
const r = checkTextRules("version: 3.2.1, build: ok", [{ contains: "version" }, { match: "\\d+\\.\\d+\\.\\d+" }], "stdout");
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
|
||||
test("第一条规则失败立即返回", () => {
|
||||
const r = checkTextRules("error occurred", [{ contains: "success" }, { contains: "error" }], "stdout");
|
||||
expect(r.matched).toBe(false);
|
||||
expect(r.failure!.phase).toBe("stdout");
|
||||
expect(r.failure!.path).toBe("stdout[0]");
|
||||
});
|
||||
|
||||
test("stderr phase", () => {
|
||||
const r = checkTextRules("warning: deprecated", [{ contains: "warning" }], "stderr");
|
||||
expect(r.matched).toBe(true);
|
||||
expect(r.failure).toBeNull();
|
||||
});
|
||||
|
||||
test("empty 操作符", () => {
|
||||
const r = checkTextRules("", [{ empty: true }], "stderr");
|
||||
expect(r.matched).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -4,6 +4,20 @@ import type { CheckFailure, ResolvedTarget } from "../../../src/server/checker/t
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { checkerRegistry } from "../../../src/server/checker/runner";
|
||||
import { HttpChecker } from "../../../src/server/checker/runner/http/runner";
|
||||
import { CommandChecker } from "../../../src/server/checker/runner/command/runner";
|
||||
|
||||
function ensureRegistered() {
|
||||
if (!checkerRegistry.supportedTypes.includes("http")) {
|
||||
checkerRegistry.register(new HttpChecker());
|
||||
checkerRegistry.register(new CommandChecker());
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(() => {
|
||||
ensureRegistered();
|
||||
});
|
||||
|
||||
const httpTarget: ResolvedTarget = {
|
||||
type: "http",
|
||||
|
||||
Reference in New Issue
Block a user