Files
Rune-Spec/tests/cli/output.test.ts

43 lines
1.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from "bun:test";
import { formatError } from "../../src/cli/output.ts";
import { UsageError, ConfigError, InternalError } from "../../src/cli/errors.ts";
describe("formatError", () => {
it("只输出错误行(无 hint/usage", () => {
const err = new ConfigError("未初始化");
const output = formatError(err);
expect(output).toBe("错误: 未初始化");
});
it("输出错误行 + 提示行", () => {
const err = new ConfigError("未初始化", { hint: "请先运行 rune init" });
const output = formatError(err);
expect(output).toBe("错误: 未初始化\n\n提示: 请先运行 rune init");
});
it("输出错误行 + 用法行", () => {
const err = new UsageError("缺少参数", {
usage: "rune plan <change-name>",
});
const output = formatError(err);
expect(output).toBe("错误: 缺少参数\n\n用法: rune plan <change-name>");
});
it("输出完整格式(错误 + 用法 + 提示)", () => {
const err = new UsageError("缺少必填参数 <change-name>", {
usage: "rune plan <change-name>",
hint: '请指定变更名称,如 "add-login"',
});
const output = formatError(err);
expect(output).toBe(
'错误: 缺少必填参数 <change-name>\n\n用法: rune plan <change-name>\n\n提示: 请指定变更名称,如 "add-login"',
);
});
it("InternalError 输出固定消息", () => {
const err = new InternalError();
const output = formatError(err);
expect(output).toBe("错误: 发生了未预期的错误");
});
});