1
0

chore: 强化代码质量与风格检查体系

ESLint 升级到 recommended-type-checked + stylistic-type-checked,
引入 perfectionist 导入排序和 import 插件导入验证。

Prettier 显式声明全部格式化参数,消除跨环境差异。
TypeScript 启用 noUnusedLocals 和 noPropertyAccessFromIndexSignature。
完善 ignore 列表,排除 .agents/、bun.lock、data/ 等。
引入 husky + lint-staged(pre-commit)+ commitlint(commit-msg)。
更新 DEVELOPMENT.md 代码质量章节。
修复所有新增规则检测到的类型和风格违规。
This commit is contained in:
2026-05-12 18:44:59 +08:00
parent ce8baae3d1
commit a5cf6065c2
83 changed files with 2654 additions and 1824 deletions

View File

@@ -1,10 +1,38 @@
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 { ProbeEngine } from "../../../src/server/checker/engine";
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";
import { HttpChecker } from "../../../src/server/checker/runner/http/runner";
function createMockStore(targetNames: string[]) {
let nextId = 1;
const targets = targetNames.map((name) => ({ id: nextId++, name }));
const results: Array<Record<string, unknown>> = [];
return {
_results: results,
getTargets() {
return targets.map(({ id, name }) => ({
config: "",
expect: null,
grp: "default",
id,
interval_ms: 60000,
name,
target: "",
timeout_ms: 5000,
type: "command" as const,
}));
},
insertCheckResult(result: Record<string, unknown>) {
results.push(result);
},
};
}
function ensureRegistered() {
if (!checkerRegistry.supportedTypes.includes("http")) {
@@ -13,46 +41,20 @@ function ensureRegistered() {
}
}
function createMockStore(targetNames: string[]) {
let nextId = 1;
const targets = targetNames.map((name) => ({ id: nextId++, name }));
const results: Array<Record<string, unknown>> = [];
return {
getTargets() {
return targets.map(({ id, name }) => ({
id,
name,
type: "command" as const,
target: "",
config: "",
interval_ms: 60000,
timeout_ms: 5000,
expect: null,
grp: "default",
}));
},
insertCheckResult(result: Record<string, unknown>) {
results.push(result);
},
_results: results,
};
}
function makeCommandTarget(name: string, overrides?: Partial<ResolvedCommandTarget>): ResolvedCommandTarget {
return {
type: "command",
name,
group: "default",
command: {
exec: "echo",
args: ["hello"],
cwd: "/tmp",
env: {},
exec: "echo",
maxOutputBytes: 1024 * 1024,
},
group: "default",
intervalMs: 60000,
name,
timeoutMs: 5000,
type: "command",
...overrides,
};
}
@@ -80,16 +82,16 @@ describe("ProbeEngine", () => {
const results = (mockStore as unknown as { _results: Array<Record<string, unknown>> })._results;
expect(results.length).toBe(1);
expect(results[0]!.matched).toBe(true);
expect(results[0]!.statusDetail).toBe("exitCode=0");
expect(results[0]!["matched"]).toBe(true);
expect(results[0]!["statusDetail"]).toBe("exitCode=0");
});
test("多个目标并发执行", async () => {
const targetA = makeCommandTarget("echo-a", {
command: { exec: "echo", args: ["a"], cwd: "/tmp", env: {}, maxOutputBytes: 1024 * 1024 },
command: { args: ["a"], cwd: "/tmp", env: {}, exec: "echo", maxOutputBytes: 1024 * 1024 },
});
const targetB = makeCommandTarget("echo-b", {
command: { exec: "echo", args: ["b"], cwd: "/tmp", env: {}, maxOutputBytes: 1024 * 1024 },
command: { args: ["b"], cwd: "/tmp", env: {}, exec: "echo", maxOutputBytes: 1024 * 1024 },
});
const mockStore = createMockStore(["echo-a", "echo-b"]) as unknown as ProbeStore;
@@ -106,7 +108,7 @@ describe("ProbeEngine", () => {
test("失败目标不阻塞其他目标", async () => {
const badTarget = makeCommandTarget("bad-cmd", {
command: { exec: "false", args: [], cwd: "/tmp", env: {}, maxOutputBytes: 1024 * 1024 },
command: { args: [], cwd: "/tmp", env: {}, exec: "false", maxOutputBytes: 1024 * 1024 },
});
const goodTarget = makeCommandTarget("good-cmd");
@@ -121,8 +123,8 @@ describe("ProbeEngine", () => {
const results = (mockStore as unknown as { _results: Array<Record<string, unknown>> })._results;
expect(results.length).toBe(2);
const badResult = results.find((r) => r.matched === false);
const goodResult = results.find((r) => r.matched === true);
const badResult = results.find((r) => r["matched"] === false);
const goodResult = results.find((r) => r["matched"] === true);
expect(badResult).toBeDefined();
expect(goodResult).toBeDefined();
});
@@ -130,7 +132,7 @@ describe("ProbeEngine", () => {
test("并发限制 maxConcurrentChecks", async () => {
const targets = Array.from({ length: 5 }, (_, i) =>
makeCommandTarget(`cmd-${i}`, {
command: { exec: "echo", args: [String(i)], cwd: "/tmp", env: {}, maxOutputBytes: 1024 * 1024 },
command: { args: [String(i)], cwd: "/tmp", env: {}, exec: "echo", maxOutputBytes: 1024 * 1024 },
}),
);
@@ -145,7 +147,7 @@ describe("ProbeEngine", () => {
const results = (mockStore as unknown as { _results: Array<Record<string, unknown>> })._results;
expect(results.length).toBe(5);
for (const r of results) {
expect(r.matched).toBe(true);
expect(r["matched"]).toBe(true);
}
});
@@ -177,25 +179,25 @@ describe("ProbeEngine", () => {
test("HTTP 目标运行", async () => {
const httpServer = Bun.serve({
port: 0,
fetch() {
return new Response("ok");
},
port: 0,
});
try {
const httpTarget: ResolvedHttpTarget = {
type: "http",
name: "http-test",
group: "default",
http: {
url: `http://localhost:${httpServer.port}/`,
method: "GET",
headers: {},
maxBodyBytes: 1024 * 1024,
method: "GET",
url: `http://localhost:${httpServer.port}/`,
},
intervalMs: 60000,
name: "http-test",
timeoutMs: 5000,
type: "http",
};
const mockStore = createMockStore(["http-test"]) as unknown as ProbeStore;
@@ -208,10 +210,10 @@ describe("ProbeEngine", () => {
const results = (mockStore as unknown as { _results: Array<Record<string, unknown>> })._results;
expect(results.length).toBe(1);
expect(results[0]!.matched).toBe(true);
expect(results[0]!.statusDetail).toBe("HTTP 200");
expect(results[0]!["matched"]).toBe(true);
expect(results[0]!["statusDetail"]).toBe("HTTP 200");
} finally {
httpServer.stop();
void httpServer.stop();
}
});
});