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 代码质量章节。 修复所有新增规则检测到的类型和风格违规。
81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
|
|
import { errorFailure, mismatchFailure, truncateActual } from "../../../../../src/server/checker/runner/shared/failure";
|
|
|
|
describe("truncateActual", () => {
|
|
test("短字符串不截断", () => {
|
|
expect(truncateActual("hello")).toBe("hello");
|
|
});
|
|
|
|
test("恰好等于限制长度不截断", () => {
|
|
const str = "a".repeat(200);
|
|
expect(truncateActual(str)).toBe(str);
|
|
});
|
|
|
|
test("超过限制长度截断并加省略号", () => {
|
|
const str = "a".repeat(300);
|
|
const result = truncateActual(str) as string;
|
|
expect(result.length).toBe(203);
|
|
expect(result.endsWith("...")).toBe(true);
|
|
expect(result.startsWith("a".repeat(200))).toBe(true);
|
|
});
|
|
|
|
test("自定义最大长度", () => {
|
|
const str = "abcdefghij";
|
|
const result = truncateActual(str, 5) as string;
|
|
expect(result).toBe("abcde...");
|
|
});
|
|
|
|
test("null 不截断", () => {
|
|
expect(truncateActual(null)).toBe(null);
|
|
});
|
|
|
|
test("undefined 不截断", () => {
|
|
expect(truncateActual(undefined)).toBe(undefined);
|
|
});
|
|
|
|
test("数字转换为字符串后判断", () => {
|
|
expect(truncateActual(42)).toBe(42);
|
|
expect(truncateActual(123456789, 3) as string).toBe("123...");
|
|
});
|
|
});
|
|
|
|
describe("mismatchFailure", () => {
|
|
test("返回正确的 mismatch 结构", () => {
|
|
const f = mismatchFailure("status", "status", [200], 500, "status mismatch");
|
|
expect(f).toEqual({
|
|
actual: 500,
|
|
expected: [200],
|
|
kind: "mismatch",
|
|
message: "status mismatch",
|
|
path: "status",
|
|
phase: "status",
|
|
});
|
|
});
|
|
|
|
test("自动截断过长的 actual", () => {
|
|
const longStr = "x".repeat(300);
|
|
const f = mismatchFailure("body", "body[0]", "short", longStr, "too long");
|
|
expect((f.actual as string).endsWith("...")).toBe(true);
|
|
expect((f.actual as string).length).toBe(203);
|
|
});
|
|
});
|
|
|
|
describe("errorFailure", () => {
|
|
test("返回正确的 error 结构", () => {
|
|
const f = errorFailure("body", "body[0].json($.x)", "body is not valid JSON");
|
|
expect(f).toEqual({
|
|
kind: "error",
|
|
message: "body is not valid JSON",
|
|
path: "body[0].json($.x)",
|
|
phase: "body",
|
|
});
|
|
});
|
|
|
|
test("不含 expected 和 actual 字段", () => {
|
|
const f = errorFailure("headers", "headers.x", "header missing");
|
|
expect(f).not.toHaveProperty("expected");
|
|
expect(f).not.toHaveProperty("actual");
|
|
});
|
|
});
|