118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { runCli } from "./runner.js";
|
|
import { parseArgs, booleanOption } from "./parser.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("");
|
|
});
|
|
|
|
it("does not swallow positional arguments after subcommand boolean flags", () => {
|
|
const parsed1 = parseArgs(["feetype", "get", "--all", "FT001"]);
|
|
expect(booleanOption(parsed1.options, ["all"])).toBe(true);
|
|
expect(parsed1.args).toEqual(["FT001"]);
|
|
|
|
const parsed2 = parseArgs(["workflow", "patch", "--in-place", "--value-json", "PIPE_123"]);
|
|
expect(booleanOption(parsed2.options, ["in-place"])).toBe(true);
|
|
expect(booleanOption(parsed2.options, ["value-json"])).toBe(true);
|
|
expect(parsed2.args).toEqual(["PIPE_123"]);
|
|
|
|
const parsed3 = parseArgs(["platform", "record", "toggle", "--active", "false", "REC_01"]);
|
|
expect(booleanOption(parsed3.options, ["active"], true)).toBe(false);
|
|
expect(parsed3.args).toEqual(["toggle", "REC_01"]);
|
|
});
|
|
});
|