test(refactor): dedupe cli and ios script scenarios

This commit is contained in:
Peter Steinberger
2026-03-02 11:16:33 +00:00
parent 1b98879295
commit 0c2d85529a
2 changed files with 121 additions and 82 deletions

View File

@@ -72,49 +72,87 @@ afterEach(() => {
});
describe("registerPreActionHooks", () => {
function buildProgram() {
type CommandKey =
| "status"
| "doctor"
| "completion"
| "secrets"
| "update-status"
| "config-set"
| "agents"
| "configure"
| "onboard"
| "message-send";
function buildProgram(keys: readonly CommandKey[]) {
const enabled = new Set<CommandKey>(keys);
const has = (key: CommandKey) => enabled.has(key);
const program = new Command().name("openclaw");
program.command("status").action(() => {});
program.command("doctor").action(() => {});
program.command("completion").action(() => {});
program.command("secrets").action(() => {});
program
.command("update")
.command("status")
.option("--json")
.action(() => {});
const config = program.command("config");
config
.command("set")
.argument("<path>")
.argument("<value>")
.option("--json")
.action(() => {});
program.command("agents").action(() => {});
program.command("configure").action(() => {});
program.command("onboard").action(() => {});
program
.command("message")
.command("send")
.option("--json")
.action(() => {});
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("<path>")
.argument("<value>")
.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(() => {});
}
registerPreActionHooks(program, "9.9.9-test");
return program;
}
async function runCommand(
params: { parseArgv: string[]; processArgv?: string[] },
program = buildProgram(),
program: Command,
) {
process.argv = params.processArgv ?? [...params.parseArgv];
await program.parseAsync(params.parseArgv, { from: "user" });
}
it("emits banner, resolves config, and enables verbose from --debug", async () => {
await runCommand({
parseArgv: ["status"],
processArgv: ["node", "openclaw", "status", "--debug"],
});
const program = buildProgram(["status"]);
await runCommand(
{
parseArgv: ["status"],
processArgv: ["node", "openclaw", "status", "--debug"],
},
program,
);
expect(emitCliBannerMock).toHaveBeenCalledWith("9.9.9-test");
expect(setVerboseMock).toHaveBeenCalledWith(true);
@@ -127,10 +165,14 @@ describe("registerPreActionHooks", () => {
});
it("loads plugin registry for plugin-required commands", async () => {
await runCommand({
parseArgv: ["message", "send"],
processArgv: ["node", "openclaw", "message", "send"],
});
const program = buildProgram(["message-send"]);
await runCommand(
{
parseArgv: ["message", "send"],
processArgv: ["node", "openclaw", "message", "send"],
},
program,
);
expect(setVerboseMock).toHaveBeenCalledWith(false);
expect(process.env.NODE_NO_WARNINGS).toBe("1");
@@ -143,7 +185,7 @@ describe("registerPreActionHooks", () => {
it("loads plugin registry for configure/onboard/agents commands", async () => {
const commands = ["configure", "onboard", "agents"] as const;
const program = buildProgram();
const program = buildProgram(["configure", "onboard", "agents"]);
for (const command of commands) {
vi.clearAllMocks();
await runCommand(
@@ -158,7 +200,7 @@ describe("registerPreActionHooks", () => {
});
it("skips config guard for doctor, completion, and secrets commands", async () => {
const program = buildProgram();
const program = buildProgram(["doctor", "completion", "secrets"]);
await runCommand(
{
parseArgv: ["doctor"],
@@ -185,10 +227,14 @@ describe("registerPreActionHooks", () => {
});
it("skips preaction work when argv indicates help/version", async () => {
await runCommand({
parseArgv: ["status"],
processArgv: ["node", "openclaw", "--version"],
});
const program = buildProgram(["status"]);
await runCommand(
{
parseArgv: ["status"],
processArgv: ["node", "openclaw", "--version"],
},
program,
);
expect(emitCliBannerMock).not.toHaveBeenCalled();
expect(setVerboseMock).not.toHaveBeenCalled();
@@ -197,17 +243,21 @@ describe("registerPreActionHooks", () => {
it("hides banner when OPENCLAW_HIDE_BANNER is truthy", async () => {
process.env.OPENCLAW_HIDE_BANNER = "1";
await runCommand({
parseArgv: ["status"],
processArgv: ["node", "openclaw", "status"],
});
const program = buildProgram(["status"]);
await runCommand(
{
parseArgv: ["status"],
processArgv: ["node", "openclaw", "status"],
},
program,
);
expect(emitCliBannerMock).not.toHaveBeenCalled();
expect(ensureConfigReadyMock).toHaveBeenCalledTimes(1);
});
it("suppresses doctor stdout for any --json output command", async () => {
const program = buildProgram();
const program = buildProgram(["message-send", "update-status"]);
await runCommand(
{
parseArgv: ["message", "send", "--json"],
@@ -240,10 +290,14 @@ describe("registerPreActionHooks", () => {
});
it("does not treat config set --json (strict-parse alias) as json output mode", async () => {
await runCommand({
parseArgv: ["config", "set", "gateway.auth.mode", "{bad", "--json"],
processArgv: ["node", "openclaw", "config", "set", "gateway.auth.mode", "{bad", "--json"],
});
const program = buildProgram(["config-set"]);
await runCommand(
{
parseArgv: ["config", "set", "gateway.auth.mode", "{bad", "--json"],
processArgv: ["node", "openclaw", "config", "set", "gateway.auth.mode", "{bad", "--json"],
},
program,
);
expect(ensureConfigReadyMock).toHaveBeenCalledWith({
runtime: runtimeMock,