85 lines
4.6 KiB
TypeScript
85 lines
4.6 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import {
|
|
CliError,
|
|
classifyCliError,
|
|
confirmationRequired,
|
|
PayloadTooLargeError,
|
|
safetyBlocked,
|
|
validationError
|
|
} from "./errors.js";
|
|
|
|
describe("CLI semantic errors", () => {
|
|
test("preserves explicit exit codes and next actions", () => {
|
|
const error = new CliError("missing sign key", 2, "seal-use deploy hosecloud get <sign-key>");
|
|
|
|
expect(classifyCliError(error)).toEqual({
|
|
exitCode: 2,
|
|
nextAction: "seal-use deploy hosecloud get <sign-key>"
|
|
});
|
|
});
|
|
|
|
test("classifies confirmation and output protection distinctly", () => {
|
|
expect(classifyCliError(confirmationRequired("Refusing to delete", "seal-use webhook delete <id> --yes"))).toMatchObject({
|
|
exitCode: 10,
|
|
nextAction: "seal-use webhook delete <id> --yes"
|
|
});
|
|
expect(classifyCliError(new PayloadTooLargeError("Output too large"))).toEqual({ exitCode: 12 });
|
|
});
|
|
|
|
test("classifies authentication failures from response status", () => {
|
|
expect(classifyCliError({ response: { status: 401 } })).toEqual({ exitCode: 11 });
|
|
expect(classifyCliError(new Error("bearer token expired"))).toEqual({ exitCode: 11 });
|
|
});
|
|
|
|
test("classifies unknown commands and subcommands across all domains as usage errors", () => {
|
|
expect(classifyCliError(new Error("Unknown command: not-a-command"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown contacts command: . Run 'seal-use help' for available commands."))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown data command: bad-subcommand"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown analytics command: invalid"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown approval-runs command: invalid"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown billing command: invalid"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown webhook command: invalid"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Usage: seal-use contacts children <departmentId>"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Invalid boolean: now"))).toEqual({ exitCode: 2 });
|
|
expect(classifyCliError(new Error("Unknown deploy hosecloud action: x"))).toEqual({ exitCode: 2 });
|
|
});
|
|
|
|
test("classifies structural input failures without matching their wording", () => {
|
|
expect(classifyCliError(validationError("请选择目标通道"))).toMatchObject({ exitCode: 13 });
|
|
expect(classifyCliError(new SyntaxError("JSON Parse error: Expected '}'"))).toEqual({ exitCode: 13 });
|
|
expect(classifyCliError({ issues: [{ path: ["environment"], message: "Invalid option" }] })).toEqual({ exitCode: 13 });
|
|
});
|
|
|
|
test("safety guards carry their code independently of user-facing text", () => {
|
|
expect(classifyCliError(safetyBlocked("blocked by environment guard", "seal-use webhook replay --force")))
|
|
.toEqual({ exitCode: 14, nextAction: "seal-use webhook replay --force" });
|
|
expect(classifyCliError(safetyBlocked("换一种中文解释"))).toMatchObject({ exitCode: 14 });
|
|
});
|
|
|
|
test("upstream messages containing validation or safety words stay domain failures", () => {
|
|
for (const message of ["上游格式错误,请提供工单", "No enterprise config found upstream", "File not found: remote object", "禁止向生产环境传输失败"]) {
|
|
expect(classifyCliError(new Error(message))).toEqual({ exitCode: 1 });
|
|
}
|
|
});
|
|
|
|
test("validationError helper carries exit 13 with next action", () => {
|
|
expect(classifyCliError(validationError("请选择目标通道", "seal-use webhook list"))).toEqual({
|
|
exitCode: 13,
|
|
nextAction: "seal-use webhook list"
|
|
});
|
|
});
|
|
|
|
test("keeps domain failures, post-mutation recovery errors, and quality gates at exit 1", () => {
|
|
expect(classifyCliError(new Error("未找到指定的规则版本: 99"))).toEqual({ exitCode: 1 });
|
|
expect(classifyCliError(new Error("[Linter 质量门禁未通过] 综合评分 80 低于要求的最低分 85 分"))).toEqual({
|
|
exitCode: 1
|
|
});
|
|
expect(classifyCliError(new Error("Webhook 单据推送失败 (500): internal"))).toEqual({ exitCode: 1 });
|
|
expect(classifyCliError(new Error("测试企业「T」AI 审批额度不足:当前剩余 200 点"))).toEqual({ exitCode: 1 });
|
|
expect(
|
|
classifyCliError(new Error("重排请求已发送,但回读规则与预览不一致。请先核对当前草稿,勿重复执行旧确认码。"))
|
|
).toEqual({ exitCode: 1 });
|
|
expect(classifyCliError(new Error("上游网关响应超时,请重试或检查网络连接。"))).toEqual({ exitCode: 1 });
|
|
});
|
|
});
|