将 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 指南
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/expect/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");
|
|
});
|
|
});
|