diff --git a/src/cli/program/preaction.test.ts b/src/cli/program/preaction.test.ts index 3d629f422..9afed73c0 100644 --- a/src/cli/program/preaction.test.ts +++ b/src/cli/program/preaction.test.ts @@ -72,66 +72,34 @@ afterEach(() => { }); describe("registerPreActionHooks", () => { - type CommandKey = - | "status" - | "doctor" - | "completion" - | "secrets" - | "update-status" - | "config-set" - | "agents" - | "configure" - | "onboard" - | "message-send"; + let program: Command; - function buildProgram(keys: readonly CommandKey[]) { - const enabled = new Set(keys); - const has = (key: CommandKey) => enabled.has(key); + function buildProgram() { const program = new Command().name("openclaw"); - if (has("status")) { - program.command("status").action(() => {}); - } - if (has("doctor")) { - program.command("doctor").action(() => {}); - } - if (has("completion")) { - program.command("completion").action(() => {}); - } - if (has("secrets")) { - program.command("secrets").action(() => {}); - } - if (has("update-status")) { - program - .command("update") - .command("status") - .option("--json") - .action(() => {}); - } - if (has("config-set")) { - const config = program.command("config"); - config - .command("set") - .argument("") - .argument("") - .option("--json") - .action(() => {}); - } - if (has("agents")) { - program.command("agents").action(() => {}); - } - if (has("configure")) { - program.command("configure").action(() => {}); - } - if (has("onboard")) { - program.command("onboard").action(() => {}); - } - if (has("message-send")) { - program - .command("message") - .command("send") - .option("--json") - .action(() => {}); - } + program.command("status").action(() => {}); + program.command("doctor").action(() => {}); + program.command("completion").action(() => {}); + program.command("secrets").action(() => {}); + program.command("agents").action(() => {}); + program.command("configure").action(() => {}); + program.command("onboard").action(() => {}); + program + .command("update") + .command("status") + .option("--json") + .action(() => {}); + program + .command("message") + .command("send") + .option("--json") + .action(() => {}); + const config = program.command("config"); + config + .command("set") + .argument("") + .argument("") + .option("--json") + .action(() => {}); registerPreActionHooks(program, "9.9.9-test"); return program; } @@ -145,7 +113,6 @@ describe("registerPreActionHooks", () => { } it("emits banner, resolves config, and enables verbose from --debug", async () => { - const program = buildProgram(["status"]); await runCommand( { parseArgv: ["status"], @@ -165,7 +132,6 @@ describe("registerPreActionHooks", () => { }); it("loads plugin registry for plugin-required commands", async () => { - const program = buildProgram(["message-send"]); await runCommand( { parseArgv: ["message", "send"], @@ -184,7 +150,6 @@ describe("registerPreActionHooks", () => { }); it("loads plugin registry for configure command", async () => { - const program = buildProgram(["configure"]); await runCommand( { parseArgv: ["configure"], @@ -197,7 +162,6 @@ describe("registerPreActionHooks", () => { }); it("skips config guard for doctor command", async () => { - const program = buildProgram(["doctor"]); await runCommand( { parseArgv: ["doctor"], @@ -210,7 +174,6 @@ describe("registerPreActionHooks", () => { }); it("skips preaction work when argv indicates help/version", async () => { - const program = buildProgram(["status"]); await runCommand( { parseArgv: ["status"], @@ -226,7 +189,6 @@ describe("registerPreActionHooks", () => { it("hides banner when OPENCLAW_HIDE_BANNER is truthy", async () => { process.env.OPENCLAW_HIDE_BANNER = "1"; - const program = buildProgram(["status"]); await runCommand( { parseArgv: ["status"], @@ -240,7 +202,6 @@ describe("registerPreActionHooks", () => { }); it("suppresses doctor stdout for any --json output command", async () => { - const program = buildProgram(["message-send", "update-status"]); await runCommand( { parseArgv: ["message", "send", "--json"], @@ -273,7 +234,6 @@ describe("registerPreActionHooks", () => { }); it("does not treat config set --json (strict-parse alias) as json output mode", async () => { - const program = buildProgram(["config-set"]); await runCommand( { parseArgv: ["config", "set", "gateway.auth.mode", "{bad", "--json"], @@ -287,4 +247,8 @@ describe("registerPreActionHooks", () => { commandPath: ["config", "set"], }); }); + + beforeAll(() => { + program = buildProgram(); + }); });