import { describe, it, expect } from "bun:test"; import { UsageError, ConfigError, InternalError } from "../../src/cli/errors.ts"; import { mapError } from "../../src/cli.ts"; describe("mapError", () => { it("CliError 原样返回", () => { const err = new ConfigError("未初始化"); const result = mapError(err); expect(result).toBe(err); expect(result.message).toBe("未初始化"); }); it("Unknown option 转为 UsageError", () => { const err = new Error("Unknown option `--badflag`"); const result = mapError(err); expect(result).toBeInstanceOf(UsageError); expect(result.message).toBe("未知选项: --badflag"); expect(result.hint).toBe("运行 rune help 查看所有命令"); }); it("Unknown command 转为 UsageError", () => { const err = new Error("Unknown command `foo`"); const result = mapError(err); expect(result).toBeInstanceOf(UsageError); expect(result.message).toBe("未知命令: foo"); }); it("Unused args 转为 UsageError", () => { const err = new Error("Unused args: --extra"); const result = mapError(err); expect(result).toBeInstanceOf(UsageError); expect(result.message).toContain("未知命令"); expect(result.message).toContain("--extra"); }); it("missing required args 转为 UsageError", () => { const err = new Error("missing required args for command `plan`"); const result = mapError(err); expect(result).toBeInstanceOf(UsageError); expect(result.message).toBe("命令 'plan' 缺少必填参数"); expect(result.usage).toBe("rune plan "); expect(result.hint).toContain("rune help plan"); }); it("未知 Error 转为 InternalError", () => { const err = new Error("something unexpected"); const result = mapError(err); expect(result).toBeInstanceOf(InternalError); expect(result.message).toBe("发生了未预期的错误"); }); it("非 Error 类型转为 InternalError", () => { const result = mapError("字符串错误"); expect(result).toBeInstanceOf(InternalError); }); });