File size: 2,530 Bytes
eb3f11e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | // Parent default help tests cover parent command help fallback behavior.
import { Command } from "commander";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { applyParentDefaultHelpAction, isParentDefaultHelpAction } from "./parent-default-help.js";
describe("applyParentDefaultHelpAction (#73077)", () => {
let originalExitCode: NodeJS.Process["exitCode"];
let originalSuppressHelpBanner: string | undefined;
beforeEach(() => {
originalExitCode = process.exitCode;
originalSuppressHelpBanner = process.env.OPENCLAW_SUPPRESS_HELP_BANNER;
process.exitCode = undefined;
});
afterEach(() => {
process.exitCode = originalExitCode;
if (originalSuppressHelpBanner === undefined) {
delete process.env.OPENCLAW_SUPPRESS_HELP_BANNER;
} else {
process.env.OPENCLAW_SUPPRESS_HELP_BANNER = originalSuppressHelpBanner;
}
});
function buildParent(): Command {
const program = new Command();
program.exitOverride();
const parent = program.command("parent").description("test parent");
parent.exitOverride();
parent.command("list").action(() => {});
parent.command("status").action(() => {});
return parent;
}
it("invokes parent help and exits 0 when invoked without subcommand", async () => {
const parent = buildParent();
const suppressHelpBannerValues: Array<string | undefined> = [];
const helpSpy = vi.spyOn(parent, "outputHelp").mockImplementation(() => {
suppressHelpBannerValues.push(process.env.OPENCLAW_SUPPRESS_HELP_BANNER);
});
expect(isParentDefaultHelpAction(parent)).toBe(false);
applyParentDefaultHelpAction(parent);
expect(isParentDefaultHelpAction(parent)).toBe(true);
await parent.parent!.parseAsync(["node", "test", "parent"]);
expect(helpSpy).toHaveBeenCalledTimes(1);
expect(suppressHelpBannerValues).toEqual(["1"]);
expect(process.env.OPENCLAW_SUPPRESS_HELP_BANNER).toBeUndefined();
expect(process.exitCode).toBe(0);
});
it("still routes through subcommand actions when one is invoked", async () => {
const parent = buildParent();
const listAction = vi.fn();
parent.commands.find((c) => c.name() === "list")!.action(listAction);
const helpSpy = vi.spyOn(parent, "outputHelp").mockImplementation(() => {});
applyParentDefaultHelpAction(parent);
await parent.parent!.parseAsync(["node", "test", "parent", "list"]);
expect(listAction).toHaveBeenCalledTimes(1);
expect(helpSpy).not.toHaveBeenCalled();
});
});
|