1
0

refactor: checker 模块内聚化 — 每个 checker 自包含于独立目录

将 checker 架构重构为完全内聚模式:每个 checker 目录包含自身的
types、schema、validate、execute、expect 和 index,新增 checker
只需创建一个目录并在 runner/index.ts 添加一行注册。

主要变更:
- runner/shared/ 拆分:断言基础设施迁入 checker/expect/,
  body.ts 迁入 http/,text.ts 迁入 command/
- config-contract/ 重命名为 schema/,schema.ts → builder.ts
- size.ts + parseDuration 合并为 utils.ts
- 顶层 types.ts 改为 base interface + index signature,
  checker 专属类型下沉到各自 types.ts
- runner/index.ts 改为显式数组注册模式
- 更新 DEVELOPMENT.md 项目结构和开发新 Checker 指南
This commit is contained in:
2026-05-13 14:38:21 +08:00
parent c396c29402
commit bb6b2bc20b
52 changed files with 789 additions and 820 deletions

View File

@@ -3,10 +3,13 @@ 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 { 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/runner";
import { HttpChecker } from "../../../src/server/checker/runner/http/runner";
import { CommandChecker } from "../../../src/server/checker/runner/command/execute";
import { HttpChecker } from "../../../src/server/checker/runner/http/execute";
import { readRuntimeConfig } from "../../../src/server/config";
function ensureRegistered() {
@@ -106,19 +109,17 @@ describe("loadConfig", () => {
expect(config.dataDir).toBe("./data");
expect(config.maxConcurrentChecks).toBe(20);
expect(config.targets).toHaveLength(1);
const t = config.targets[0]!;
const t = config.targets[0]! as ResolvedHttpTarget;
expect(t.type).toBe("http");
if (t.type === "http") {
expect(t.name).toBe("test");
expect(t.http.url).toBe("http://example.com");
expect(t.http.method).toBe("GET");
expect(t.http.headers).toEqual({});
expect(t.http.ignoreSSL).toBe(false);
expect(t.http.maxBodyBytes).toBe(104857600);
expect(t.http.maxRedirects).toBe(0);
expect(t.intervalMs).toBe(30000);
expect(t.timeoutMs).toBe(10000);
}
expect(t.name).toBe("test");
expect(t.http.url).toBe("http://example.com");
expect(t.http.method).toBe("GET");
expect(t.http.headers).toEqual({});
expect(t.http.ignoreSSL).toBe(false);
expect(t.http.maxBodyBytes).toBe(104857600);
expect(t.http.maxRedirects).toBe(0);
expect(t.intervalMs).toBe(30000);
expect(t.timeoutMs).toBe(10000);
});
test("解析最简 command 配置", async () => {
@@ -138,16 +139,14 @@ describe("loadConfig", () => {
const config = await loadConfig(configPath);
expect(config.targets).toHaveLength(1);
const t = config.targets[0]!;
const t = config.targets[0]! as ResolvedCommandTarget;
expect(t.type).toBe("command");
if (t.type === "command") {
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.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();
});
test("解析完整配置", async () => {
@@ -200,27 +199,23 @@ targets:
expect(config.maxConcurrentChecks).toBe(5);
expect(config.targets).toHaveLength(2);
const http = config.targets[0]!;
const http = config.targets[0]! as ResolvedHttpTarget;
expect(http.type).toBe("http");
if (http.type === "http") {
expect(http.http.url).toBe("http://example.com");
expect(http.http.method).toBe("POST");
expect(http.http.headers).toEqual({ Authorization: "Bearer token" });
expect(http.http.ignoreSSL).toBe(true);
expect(http.http.maxBodyBytes).toBe(52428800);
expect(http.http.maxRedirects).toBe(5);
expect(http.expect?.status).toEqual(["2xx", 301]);
expect(http.intervalMs).toBe(60000);
expect(http.timeoutMs).toBe(5000);
}
expect(http.http.url).toBe("http://example.com");
expect(http.http.method).toBe("POST");
expect(http.http.headers).toEqual({ Authorization: "Bearer token" });
expect(http.http.ignoreSSL).toBe(true);
expect(http.http.maxBodyBytes).toBe(52428800);
expect(http.http.maxRedirects).toBe(5);
expect(http.expect?.status).toEqual(["2xx", 301]);
expect(http.intervalMs).toBe(60000);
expect(http.timeoutMs).toBe(5000);
const cmd = config.targets[1]!;
const cmd = config.targets[1]! as ResolvedCommandTarget;
expect(cmd.type).toBe("command");
if (cmd.type === "command") {
expect(cmd.command.exec).toBe("ls");
expect(cmd.command.args).toEqual(["/tmp"]);
expect(cmd.command.maxOutputBytes).toBe(10485760);
}
expect(cmd.command.exec).toBe("ls");
expect(cmd.command.args).toEqual(["/tmp"]);
expect(cmd.command.maxOutputBytes).toBe(10485760);
});
test("per-target 覆盖 defaults", async () => {
@@ -246,13 +241,11 @@ targets:
);
const config = await loadConfig(configPath);
const t = config.targets[0]!;
if (t.type === "http") {
expect(t.http.method).toBe("POST");
expect(t.intervalMs).toBe(300000);
expect(t.timeoutMs).toBe(30000);
expect(t.http.maxBodyBytes).toBe(1048576);
}
const t = config.targets[0]! as ResolvedHttpTarget;
expect(t.http.method).toBe("POST");
expect(t.intervalMs).toBe(300000);
expect(t.timeoutMs).toBe(30000);
expect(t.http.maxBodyBytes).toBe(1048576);
});
test("配置文件不存在抛出错误", async () => {
@@ -564,10 +557,8 @@ targets:
);
const config = await loadConfig(configPath);
const t = config.targets[0]!;
if (t.type === "command") {
expect(t.command.cwd).toBe(join(subdir, "scripts"));
}
const t = config.targets[0] as ResolvedCommandTarget;
expect(t.command.cwd).toBe(join(subdir, "scripts"));
});
test("command env 覆盖", async () => {
@@ -586,12 +577,10 @@ targets:
);
const config = await loadConfig(configPath);
const t = config.targets[0]!;
if (t.type === "command") {
expect(t.command.env["LANG"]).toBe("C");
expect(t.command.env["CUSTOM_VAR"]).toBe("test");
expect(t.command.env["PATH"]).toBeDefined();
}
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();
});
test("解析 group 字段", async () => {
@@ -1049,9 +1038,9 @@ targets:
`,
);
const config = await loadConfig(configPath);
const target = config.targets[0]!;
const target = config.targets[0] as ResolvedHttpTarget;
expect(target.type).toBe("http");
if (target.type === "http") expect(target.http.method).toBe("POST");
expect(target.http.method).toBe("POST");
});
test("动态 headers 和 env 允许任意键名", async () => {
@@ -1082,15 +1071,13 @@ targets:
`,
);
const config = await loadConfig(configPath);
const http = config.targets[0]!;
const command = config.targets[1]!;
const http = config.targets[0] as ResolvedHttpTarget;
const command = config.targets[1] as ResolvedCommandTarget;
expect(http.type).toBe("http");
expect(command.type).toBe("command");
if (http.type === "http") {
expect(http.http.headers["X-Default-Header"]).toBe("default");
expect(http.http.headers["X-Custom-Header"]).toBe("custom");
}
if (command.type === "command") expect(command.command.env["CUSTOM_ENV_NAME"]).toBe("custom");
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");
});
test("command args 类型非法", async () => {