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,4 +1,5 @@
import { describe, expect, test } from "bun:test";
import { checkBodyExpect } from "../../../../../src/server/checker/runner/shared/body";
describe("checkBodyExpect (BodyRule[])", () => {
@@ -41,89 +42,89 @@ describe("checkBodyExpect (BodyRule[])", () => {
});
test("json 等值匹配成功", () => {
const body = JSON.stringify({ status: "ok", code: 0 });
const r = checkBodyExpect(body, [{ json: { path: "$.status", equals: "ok" } }]);
const body = JSON.stringify({ code: 0, status: "ok" });
const r = checkBodyExpect(body, [{ json: { equals: "ok", path: "$.status" } }]);
expect(r.matched).toBe(true);
});
test("json 等值匹配失败", () => {
const body = JSON.stringify({ status: "ok" });
const r = checkBodyExpect(body, [{ json: { path: "$.status", equals: "error" } }]);
const r = checkBodyExpect(body, [{ json: { equals: "error", path: "$.status" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.kind).toBe("mismatch");
});
test("json 操作符匹配", () => {
const body = JSON.stringify({ count: 42, version: "v2.1.0" });
expect(checkBodyExpect(body, [{ json: { path: "$.count", gte: 10 } }]).matched).toBe(true);
expect(checkBodyExpect(body, [{ json: { path: "$.version", match: "\\d+\\.\\d+\\.\\d+" } }]).matched).toBe(true);
expect(checkBodyExpect(body, [{ json: { path: "$.count", gte: 100 } }]).matched).toBe(false);
expect(checkBodyExpect(body, [{ json: { gte: 10, path: "$.count" } }]).matched).toBe(true);
expect(checkBodyExpect(body, [{ json: { match: "\\d+\\.\\d+\\.\\d+", path: "$.version" } }]).matched).toBe(true);
expect(checkBodyExpect(body, [{ json: { gte: 100, path: "$.count" } }]).matched).toBe(false);
});
test("json 路径不存在", () => {
const body = JSON.stringify({ status: "ok" });
const r = checkBodyExpect(body, [{ json: { path: "$.notExist", equals: "value" } }]);
const r = checkBodyExpect(body, [{ json: { equals: "value", path: "$.notExist" } }]);
expect(r.matched).toBe(false);
});
test("json 解析失败", () => {
const r = checkBodyExpect("not json", [{ json: { path: "$.status", equals: "ok" } }]);
const r = checkBodyExpect("not json", [{ json: { equals: "ok", path: "$.status" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.kind).toBe("error");
});
test("css 文本内容匹配", () => {
const html = "<div id='health'>OK</div><span class='ver'>1.0</span>";
expect(checkBodyExpect(html, [{ css: { selector: "div#health", equals: "OK" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "span.ver", equals: "1.0" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "div#health", equals: "ERROR" } }]).matched).toBe(false);
expect(checkBodyExpect(html, [{ css: { equals: "OK", selector: "div#health" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { equals: "1.0", selector: "span.ver" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { equals: "ERROR", selector: "div#health" } }]).matched).toBe(false);
});
test("css 选择器无匹配元素", () => {
const html = "<div>OK</div>";
const r = checkBodyExpect(html, [{ css: { selector: "span.missing", equals: "OK" } }]);
const r = checkBodyExpect(html, [{ css: { equals: "OK", selector: "span.missing" } }]);
expect(r.matched).toBe(false);
});
test("css attr 提取", () => {
const html = '<meta name="version" content="2.0.1">';
expect(
checkBodyExpect(html, [{ css: { selector: 'meta[name="version"]', attr: "content", equals: "2.0.1" } }]).matched,
checkBodyExpect(html, [{ css: { attr: "content", equals: "2.0.1", selector: 'meta[name="version"]' } }]).matched,
).toBe(true);
});
test("css exists 检查", () => {
const html = "<div id='test'>OK</div>";
expect(checkBodyExpect(html, [{ css: { selector: "div#test", exists: true } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "span#missing", exists: false } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { selector: "div#test", exists: false } }]).matched).toBe(false);
expect(checkBodyExpect(html, [{ css: { exists: true, selector: "div#test" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { exists: false, selector: "span#missing" } }]).matched).toBe(true);
expect(checkBodyExpect(html, [{ css: { exists: false, selector: "div#test" } }]).matched).toBe(false);
});
test("xpath 节点文本匹配", () => {
const xml = "<root><status>ok</status><code>200</code></root>";
expect(checkBodyExpect(xml, [{ xpath: { path: "/root/status/text()", equals: "ok" } }]).matched).toBe(true);
expect(checkBodyExpect(xml, [{ xpath: { path: "/root/status/text()", equals: "error" } }]).matched).toBe(false);
expect(checkBodyExpect(xml, [{ xpath: { equals: "ok", path: "/root/status/text()" } }]).matched).toBe(true);
expect(checkBodyExpect(xml, [{ xpath: { equals: "error", path: "/root/status/text()" } }]).matched).toBe(false);
});
test("xpath 无匹配节点", () => {
const xml = "<root><status>ok</status></root>";
const r = checkBodyExpect(xml, [{ xpath: { path: "/root/missing/text()", equals: "ok" } }]);
const r = checkBodyExpect(xml, [{ xpath: { equals: "ok", path: "/root/missing/text()" } }]);
expect(r.matched).toBe(false);
});
test("规则数组按顺序检查,第一条失败立即返回", () => {
const body = JSON.stringify({ status: "error" });
const r = checkBodyExpect(body, [{ contains: "healthy" }, { json: { path: "$.status", equals: "error" } }]);
const r = checkBodyExpect(body, [{ contains: "healthy" }, { json: { equals: "error", path: "$.status" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.path).toBe("body[0]");
});
test("多条规则全部通过", () => {
const body = JSON.stringify({ status: "healthy", count: 5 });
const body = JSON.stringify({ count: 5, status: "healthy" });
const r = checkBodyExpect(body, [
{ contains: "healthy" },
{ json: { path: "$.status", equals: "healthy" } },
{ json: { path: "$.count", gte: 1 } },
{ json: { equals: "healthy", path: "$.status" } },
{ json: { gte: 1, path: "$.count" } },
]);
expect(r.matched).toBe(true);
expect(r.failure).toBeNull();
@@ -131,7 +132,7 @@ describe("checkBodyExpect (BodyRule[])", () => {
test("第二条规则失败返回正确索引", () => {
const body = JSON.stringify({ status: "ok" });
const r = checkBodyExpect(body, [{ contains: "ok" }, { json: { path: "$.status", equals: "error" } }]);
const r = checkBodyExpect(body, [{ contains: "ok" }, { json: { equals: "error", path: "$.status" } }]);
expect(r.matched).toBe(false);
expect(r.failure!.path).toContain("body[1]");
});

View File

@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
import { checkDuration } from "../../../../../src/server/checker/runner/shared/duration";
describe("checkDuration", () => {

View File

@@ -1,5 +1,6 @@
import { describe, expect, test } from "bun:test";
import { truncateActual, mismatchFailure, errorFailure } from "../../../../../src/server/checker/runner/shared/failure";
import { errorFailure, mismatchFailure, truncateActual } from "../../../../../src/server/checker/runner/shared/failure";
describe("truncateActual", () => {
test("短字符串不截断", () => {
@@ -43,12 +44,12 @@ describe("mismatchFailure", () => {
test("返回正确的 mismatch 结构", () => {
const f = mismatchFailure("status", "status", [200], 500, "status mismatch");
expect(f).toEqual({
kind: "mismatch",
phase: "status",
path: "status",
expected: [200],
actual: 500,
expected: [200],
kind: "mismatch",
message: "status mismatch",
path: "status",
phase: "status",
});
});
@@ -65,9 +66,9 @@ describe("errorFailure", () => {
const f = errorFailure("body", "body[0].json($.x)", "body is not valid JSON");
expect(f).toEqual({
kind: "error",
phase: "body",
path: "body[0].json($.x)",
message: "body is not valid JSON",
path: "body[0].json($.x)",
phase: "body",
});
});

View File

@@ -1,19 +1,24 @@
import { describe, expect, test } from "bun:test";
import { applyOperator, checkExpectValue, evaluateJsonPath } from "../../../../../src/server/checker/runner/shared/operator";
import {
applyOperator,
checkExpectValue,
evaluateJsonPath,
} from "../../../../../src/server/checker/runner/shared/operator";
describe("evaluateJsonPath", () => {
const obj = {
status: "ok",
code: 0,
active: true,
error: null,
code: 0,
data: {
count: 42,
items: [{ name: "a" }, { name: "b" }],
nested: { deep: "value" },
},
emptyObj: {},
emptyArr: [],
emptyObj: {},
error: null,
status: "ok",
};
test("简单字段访问", () => {

View File

@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
import { checkTextRules } from "../../../../../src/server/checker/runner/shared/text";
describe("checkTextRules", () => {
@@ -21,7 +22,11 @@ describe("checkTextRules", () => {
});
test("多条规则全部通过", () => {
const r = checkTextRules("version: 3.2.1, build: ok", [{ contains: "version" }, { match: "\\d+\\.\\d+\\.\\d+" }], "stdout");
const r = checkTextRules(
"version: 3.2.1, build: ok",
[{ contains: "version" }, { match: "\\d+\\.\\d+\\.\\d+" }],
"stdout",
);
expect(r.matched).toBe(true);
});