1
0

refactor: 重命名 command checker 为 cmd checker 并适配跨平台测试

将 type/configKey 从 "command" 统一为 "cmd",源码目录 runner/command/ → runner/cmd/,
spec 目录 command-checker/ → cmd-checker/,测试全部改用 bun -e 替代 Unix 系统命令,
归档 cmd-checker-enhancement 变更并同步 delta spec 到主 spec。
This commit is contained in:
2026-05-14 09:23:10 +08:00
parent 0fa2c0c811
commit e983e5d75d
40 changed files with 522 additions and 773 deletions

View File

@@ -3,12 +3,12 @@ import { mkdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { ResolvedCommandTarget } from "../../../src/server/checker/runner/command/types";
import type { ResolvedCommandTarget } from "../../../src/server/checker/runner/cmd/types";
import type { ResolvedHttpTarget } from "../../../src/server/checker/runner/http/types";
import { loadConfig, parseDuration } from "../../../src/server/checker/config-loader";
import { checkerRegistry } from "../../../src/server/checker/runner";
import { CommandChecker } from "../../../src/server/checker/runner/command/execute";
import { CommandChecker } from "../../../src/server/checker/runner/cmd/execute";
import { HttpChecker } from "../../../src/server/checker/runner/http/execute";
import { readRuntimeConfig } from "../../../src/server/config";
@@ -132,7 +132,7 @@ describe("loadConfig", () => {
expect(t.timeoutMs).toBe(10000);
});
test("解析最简 command 配置", async () => {
test("解析最简 cmd 配置", async () => {
const subdir = join(tempDir, "subdir");
await mkdir(subdir, { recursive: true });
const configPath = join(subdir, "cmd.yaml");
@@ -140,8 +140,8 @@ describe("loadConfig", () => {
configPath,
`targets:
- name: "check-nginx"
type: command
command:
type: cmd
cmd:
exec: "pgrep"
args: ["nginx"]
`,
@@ -150,13 +150,13 @@ describe("loadConfig", () => {
const config = await loadConfig(configPath);
expect(config.targets).toHaveLength(1);
const t = config.targets[0]! as ResolvedCommandTarget;
expect(t.type).toBe("command");
expect(t.type).toBe("cmd");
expect(t.name).toBe("check-nginx");
expect(t.command.exec).toBe("pgrep");
expect(t.command.args).toEqual(["nginx"]);
expect(t.command.cwd).toBe(subdir);
expect(t.command.maxOutputBytes).toBe(104857600);
expect(t.command.env["PATH"]).toBeDefined();
expect(t.cmd.exec).toBe("pgrep");
expect(t.cmd.args).toEqual(["nginx"]);
expect(t.cmd.cwd).toBe(subdir);
expect(t.cmd.maxOutputBytes).toBe(104857600);
expect(t.cmd.env["PATH"]).toBeDefined();
});
test("解析完整配置", async () => {
@@ -177,7 +177,7 @@ defaults:
headers:
Authorization: "Bearer token"
maxBodyBytes: "50MB"
command:
cmd:
cwd: "/tmp"
maxOutputBytes: "10MB"
targets:
@@ -193,8 +193,8 @@ targets:
body:
- contains: "ok"
- name: "cmd-target"
type: command
command:
type: cmd
cmd:
exec: "ls"
args: ["/tmp"]
expect:
@@ -222,10 +222,10 @@ targets:
expect(http.timeoutMs).toBe(5000);
const cmd = config.targets[1]! as ResolvedCommandTarget;
expect(cmd.type).toBe("command");
expect(cmd.command.exec).toBe("ls");
expect(cmd.command.args).toEqual(["/tmp"]);
expect(cmd.command.maxOutputBytes).toBe(10485760);
expect(cmd.type).toBe("cmd");
expect(cmd.cmd.exec).toBe("ls");
expect(cmd.cmd.args).toEqual(["/tmp"]);
expect(cmd.cmd.maxOutputBytes).toBe(10485760);
});
test("绝对 dataDir 保持不变", async () => {
@@ -386,18 +386,18 @@ targets:
await expect(loadConfig(configPath)).rejects.toThrow("status 模式");
});
test("command target 缺少 exec 抛出错误", async () => {
test("cmd target 缺少 exec 抛出错误", async () => {
const configPath = join(tempDir, "no-exec.yaml");
await writeFile(
configPath,
`targets:
- name: "test"
type: command
command: {}
type: cmd
cmd: {}
`,
);
// eslint-disable-next-line @typescript-eslint/await-thenable
await expect(loadConfig(configPath)).rejects.toThrow("缺少 command.exec 字段");
await expect(loadConfig(configPath)).rejects.toThrow("缺少 cmd.exec 字段");
});
test("非法 target type 抛出错误", async () => {
@@ -538,14 +538,14 @@ targets:
}
});
test("解析 command expect 配置", async () => {
test("解析 cmd expect 配置", async () => {
const configPath = join(tempDir, "cmd-expect.yaml");
await writeFile(
configPath,
`targets:
- name: "cmd-with-expect"
type: command
command:
type: cmd
cmd:
exec: "mycheck"
expect:
exitCode: [0, 2]
@@ -560,7 +560,7 @@ targets:
const config = await loadConfig(configPath);
const t = config.targets[0]!;
if (t.type === "command") {
if (t.type === "cmd") {
expect(t.expect).toEqual({
exitCode: [0, 2],
maxDurationMs: 5000,
@@ -570,7 +570,7 @@ targets:
}
});
test("command cwd 相对配置文件目录", async () => {
test("cmd cwd 相对配置文件目录", async () => {
const subdir = join(tempDir, "cwd-test");
await mkdir(subdir, { recursive: true });
const configPath = join(subdir, "cwd.yaml");
@@ -578,8 +578,8 @@ targets:
configPath,
`targets:
- name: "cwd-test"
type: command
command:
type: cmd
cmd:
exec: "ls"
cwd: "scripts"
`,
@@ -587,17 +587,17 @@ targets:
const config = await loadConfig(configPath);
const t = config.targets[0] as ResolvedCommandTarget;
expect(t.command.cwd).toBe(join(subdir, "scripts"));
expect(t.cmd.cwd).toBe(join(subdir, "scripts"));
});
test("command env 覆盖", async () => {
test("cmd env 覆盖", async () => {
const configPath = join(tempDir, "env.yaml");
await writeFile(
configPath,
`targets:
- name: "env-test"
type: command
command:
type: cmd
cmd:
exec: "echo"
env:
LANG: "C"
@@ -607,9 +607,9 @@ targets:
const config = await loadConfig(configPath);
const t = config.targets[0] as ResolvedCommandTarget;
expect(t.command.env["LANG"]).toBe("C");
expect(t.command.env["CUSTOM_VAR"]).toBe("test");
expect(t.command.env["PATH"]).toBeDefined();
expect(t.cmd.env["LANG"]).toBe("C");
expect(t.cmd.env["CUSTOM_VAR"]).toBe("test");
expect(t.cmd.env["PATH"]).toBeDefined();
});
test("解析 group 字段", async () => {
@@ -1092,8 +1092,8 @@ targets:
X-Response-Header:
contains: "ok"
- name: "cmd-test"
type: command
command:
type: cmd
cmd:
exec: "true"
env:
CUSTOM_ENV_NAME: "custom"
@@ -1101,64 +1101,64 @@ targets:
);
const config = await loadConfig(configPath);
const http = config.targets[0] as ResolvedHttpTarget;
const command = config.targets[1] as ResolvedCommandTarget;
const cmdTarget = config.targets[1] as ResolvedCommandTarget;
expect(http.type).toBe("http");
expect(command.type).toBe("command");
expect(cmdTarget.type).toBe("cmd");
expect(http.http.headers["X-Default-Header"]).toBe("default");
expect(http.http.headers["X-Custom-Header"]).toBe("custom");
expect(command.command.env["CUSTOM_ENV_NAME"]).toBe("custom");
expect(cmdTarget.cmd.env["CUSTOM_ENV_NAME"]).toBe("custom");
});
test("command args 类型非法", async () => {
test("cmd args 类型非法", async () => {
await expectConfigError(
"bad-command-args.yaml",
"bad-cmd-args.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
args: "hello"
`,
"command.args 类型不合法",
"cmd.args 类型不合法",
);
});
test("command cwd 类型非法", async () => {
test("cmd cwd 类型非法", async () => {
await expectConfigError(
"bad-command-cwd.yaml",
"bad-cmd-cwd.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
cwd: 123
`,
"command.cwd 类型不合法",
"cmd.cwd 类型不合法",
);
});
test("command env 值类型非法", async () => {
test("cmd env 值类型非法", async () => {
await expectConfigError(
"bad-command-env.yaml",
"bad-cmd-env.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
env:
COUNT: 123
`,
"command.env.COUNT 类型不合法",
"cmd.env.COUNT 类型不合法",
);
});
test("command maxOutputBytes 非法", async () => {
test("cmd maxOutputBytes 非法", async () => {
await expectConfigError(
"bad-command-max-output.yaml",
"bad-cmd-max-output.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
maxOutputBytes: "1TB"
`,
@@ -1166,13 +1166,13 @@ targets:
);
});
test("command expect exitCode 类型非法", async () => {
test("cmd expect exitCode 类型非法", async () => {
await expectConfigError(
"bad-command-exit-code.yaml",
"bad-cmd-exit-code.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
expect:
exitCode: [1.5]
@@ -1181,13 +1181,13 @@ targets:
);
});
test("command stdout 空 text rule 非法", async () => {
test("cmd stdout 空 text rule 非法", async () => {
await expectConfigError(
"bad-command-stdout-empty.yaml",
"bad-cmd-stdout-empty.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
expect:
stdout:
@@ -1197,13 +1197,13 @@ targets:
);
});
test("command stderr 未知 operator 非法", async () => {
test("cmd stderr 未知 operator 非法", async () => {
await expectConfigError(
"bad-command-stderr-operator.yaml",
"bad-cmd-stderr-operator.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
expect:
stderr:
@@ -1213,13 +1213,13 @@ targets:
);
});
test("command stdout match 正则非法", async () => {
test("cmd stdout match 正则非法", async () => {
await expectConfigError(
"bad-command-stdout-regex.yaml",
"bad-cmd-stdout-regex.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
expect:
stdout:
@@ -1229,13 +1229,13 @@ targets:
);
});
test("command expect 未知字段失败", async () => {
test("cmd expect 未知字段失败", async () => {
await expectConfigError(
"bad-command-expect-unknown.yaml",
"bad-cmd-expect-unknown.yaml",
`targets:
- name: "cmd"
type: command
command:
type: cmd
cmd:
exec: "echo"
expect:
status: [200]