test(cli): add root-v alias regression coverage

This commit is contained in:
Gustavo Madeira Santana
2026-02-19 20:33:33 -05:00
parent 07904cde16
commit 20d058dcf5
2 changed files with 49 additions and 0 deletions

View File

@@ -44,6 +44,16 @@ describe("argv helpers", () => {
argv: ["node", "openclaw", "acp", "-v"],
expected: false,
},
{
name: "root -v alias with equals profile",
argv: ["node", "openclaw", "--profile=work", "-v"],
expected: true,
},
{
name: "subcommand path after global root flags should not be treated as version",
argv: ["node", "openclaw", "--dev", "skills", "list", "-v"],
expected: false,
},
])("detects help/version flags: $name", ({ argv, expected }) => {
expect(hasHelpOrVersion(argv)).toBe(expected);
});

View File

@@ -0,0 +1,39 @@
import process from "node:process";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const { buildProgram } = await import("./build-program.js");
describe("buildProgram version alias handling", () => {
let originalArgv: string[];
beforeEach(() => {
originalArgv = [...process.argv];
});
afterEach(() => {
process.argv = originalArgv;
vi.restoreAllMocks();
});
it("exits with version output for root -v", () => {
process.argv = ["node", "openclaw", "-v"];
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`process.exit:${String(code)}`);
}) as typeof process.exit);
expect(() => buildProgram()).toThrow("process.exit:0");
expect(logSpy).toHaveBeenCalledTimes(1);
expect(exitSpy).toHaveBeenCalledWith(0);
});
it("does not treat subcommand -v as root version alias", () => {
process.argv = ["node", "openclaw", "acp", "-v"];
const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => {
throw new Error(`unexpected process.exit:${String(code)}`);
}) as typeof process.exit);
expect(() => buildProgram()).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
});
});