Files
hose-use/src/cli/runner.test.ts
T

102 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { runCli } from "./runner.js";
import { VERSION_INFO, formatVersionLabel } from "../version.js";
describe("CLI runner - version and help aliases", () => {
const versionVariants = [
["version"],
["verSion"],
["versioN"],
["VERSION"],
["Version"],
["v"],
["V"],
["-v"],
["-V"],
["--version"],
["--verSion"],
["--VERSION"],
["--v"],
["--V"],
];
for (const args of versionVariants) {
it(`correctly outputs version for "${args.join(" ")}"`, async () => {
const result = await runCli(args);
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe("");
expect(result.stdout).toBe(`${formatVersionLabel("hose-use")}\n`);
});
}
const helpVariants = [
[],
["help"],
["HeLp"],
["HELP"],
["h"],
["H"],
["-h"],
["-H"],
["--help"],
["--HeLp"],
["--HELP"],
["?"],
["-?"],
];
for (const args of helpVariants) {
it(`correctly outputs help for "${args.join(" ") || "<empty>"}"`, async () => {
const result = await runCli(args);
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe("");
expect(result.stdout).toContain(`合思/易快报企业级 CLI 工具 (${formatVersionLabel("hose-use")})`);
expect(result.stdout).toContain("用法:");
});
}
it("handles unknown commands gracefully", async () => {
const result = await runCli(["unknown-command-xyz"]);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain("未知命令域: unknown-command-xyz");
});
it("returns semantic confirmation exit code and nextAction", async () => {
const result = await runCli([
"rule",
"add",
"办公采购",
"采购时,[必须附采购合同]{硬性}。",
"--json",
]);
expect(result.exitCode).toBe(10);
const data = JSON.parse(result.stdout);
expect(data.exitCode).toBe(10);
expect(data.nextAction).toContain("--yes");
});
it("supports layered help by command domain and subcommand", async () => {
const domain = await runCli(["help", "workflow"]);
expect(domain.exitCode).toBe(0);
expect(domain.stdout).toContain("workflow -");
expect(domain.stdout).toContain("patch");
expect(domain.stdout).toContain("--dry-run");
const command = await runCli(["workflow", "publish", "--help"]);
expect(command.exitCode).toBe(0);
expect(command.stdout).toContain("publish");
expect(command.stdout).toContain("发布并启用工作流");
});
it("keeps help and version machine-readable when --json is requested", async () => {
const help = await runCli(["help", "workflow", "--json"]);
const version = await runCli(["version", "--json"]);
expect(JSON.parse(help.stdout).help).toContain("workflow -");
expect(JSON.parse(version.stdout)).toEqual(VERSION_INFO);
expect(help.stderr).toBe("");
expect(version.stderr).toBe("");
});
});