The classifier only matched English usage wording, so every Chinese-language
usage error, every enterprise-selection failure, and every Zod/SyntaxError
fell through to exit 1 ("upstream failure"), while environment safety guards
were reported as exit 2 or 1. Add two semantic codes:
- 13 (validationError): well-formed command, unusable value — missing --corp,
unparseable enterprise JSON, missing required parameter, ambiguous webhook
channel selection.
- 14 (safetyBlocked): the target environment forbids the action — replaying
into production, regression outside a test tenant.
classifyCliError now recognises Chinese usage wording, enterprise-selection
phrasing, local file problems, Zod issue lists (structural, not textual) and
SyntaxError; English Usage:/Unknown-command stays 2, domain failures and
quality gates stay 1. Docs (AGENTS, cli-protocol, skill cli.md) and the exit
code assertions follow.
103 lines
5.3 KiB
TypeScript
103 lines
5.3 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 local config and input-data failures as validation errors (13)", () => {
|
|
// Chinese-language usage wording: the message names a flag/parameter to supply.
|
|
expect(
|
|
classifyCliError(new Error("当前租户存在多个 Webhook 通道 [\"a\" (1)],请通过 --webhook <idOrName> 指定目标通道。"))
|
|
).toEqual({ exitCode: 13 });
|
|
expect(classifyCliError(new Error("必须提供 target、recordId 或 sourceDocumentSN 参数。"))).toEqual({ exitCode: 13 });
|
|
|
|
// Enterprise selection: well-formed command, incomplete local config.
|
|
expect(
|
|
classifyCliError(new Error("Stateless mode is enabled. Every enterprise command requires --corp <corpId>."))
|
|
).toEqual({ exitCode: 13 });
|
|
expect(classifyCliError(new Error("No enterprise config found for nope"))).toEqual({ exitCode: 13 });
|
|
expect(
|
|
classifyCliError(new Error("rules lint requires an authenticated enterprise. Run 'seal-use corps list' to select one."))
|
|
).toEqual({ exitCode: 13 });
|
|
|
|
// Local file input the caller must fix.
|
|
expect(classifyCliError(new Error("File not found: /tmp/missing.json"))).toEqual({ exitCode: 13 });
|
|
expect(
|
|
classifyCliError(new Error("本地规则文件格式错误: 顶层必须为规则数组或包含 'rules' 数组的对象"))
|
|
).toEqual({ exitCode: 13 });
|
|
|
|
// Structural signals that carry no message prefix.
|
|
expect(classifyCliError(new SyntaxError("JSON Parse error: Expected '}'"))).toEqual({ exitCode: 13 });
|
|
expect(classifyCliError({ issues: [{ path: ["environment"], message: "Invalid option" }] })).toEqual({ exitCode: 13 });
|
|
});
|
|
|
|
test("classifies safety-guard blocks distinctly (14)", () => {
|
|
expect(
|
|
classifyCliError(new Error("目标企业「生产目标企业」(prod) 标记为生产正式环境 (production)。"))
|
|
).toEqual({ exitCode: 14 });
|
|
expect(classifyCliError(new Error("重放目标企业「T」(t) 的环境为「production」,非测试环境!"))).toEqual({
|
|
exitCode: 14
|
|
});
|
|
expect(classifyCliError(safetyBlocked("blocked by environment guard", "seal-use webhook replay --force"))).toEqual({
|
|
exitCode: 14,
|
|
nextAction: "seal-use webhook replay --force"
|
|
});
|
|
});
|
|
|
|
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 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 });
|
|
});
|
|
});
|