openclaw/src/cli/daemon-cli/register-service-commands.test.ts
Gustavo Madeira Santana 985ec71c55
CLI: resolve parent/subcommand option collisions (#18725)
Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: b7e51cf90950cdd3049ac3c7a3a949717b8ba261
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
2026-02-17 20:57:09 -05:00

73 lines
2.5 KiB
TypeScript

import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { addGatewayServiceCommands } from "./register-service-commands.js";
const runDaemonInstall = vi.fn(async () => {});
const runDaemonRestart = vi.fn(async () => {});
const runDaemonStart = vi.fn(async () => {});
const runDaemonStatus = vi.fn(async () => {});
const runDaemonStop = vi.fn(async () => {});
const runDaemonUninstall = vi.fn(async () => {});
vi.mock("./runners.js", () => ({
runDaemonInstall: (opts: unknown) => runDaemonInstall(opts),
runDaemonRestart: (opts: unknown) => runDaemonRestart(opts),
runDaemonStart: (opts: unknown) => runDaemonStart(opts),
runDaemonStatus: (opts: unknown) => runDaemonStatus(opts),
runDaemonStop: (opts: unknown) => runDaemonStop(opts),
runDaemonUninstall: (opts: unknown) => runDaemonUninstall(opts),
}));
function createGatewayParentLikeCommand() {
const gateway = new Command().name("gateway");
// Mirror overlapping root gateway options that conflict with service subcommand options.
gateway.option("--port <port>", "Port for the gateway WebSocket");
gateway.option("--token <token>", "Gateway token");
gateway.option("--password <password>", "Gateway password");
gateway.option("--force", "Gateway run --force", false);
addGatewayServiceCommands(gateway);
return gateway;
}
describe("addGatewayServiceCommands", () => {
beforeEach(() => {
runDaemonInstall.mockClear();
runDaemonRestart.mockClear();
runDaemonStart.mockClear();
runDaemonStatus.mockClear();
runDaemonStop.mockClear();
runDaemonUninstall.mockClear();
});
it("forwards install option collisions from parent gateway command", async () => {
const gateway = createGatewayParentLikeCommand();
await gateway.parseAsync(["install", "--force", "--port", "19000", "--token", "tok_test"], {
from: "user",
});
expect(runDaemonInstall).toHaveBeenCalledWith(
expect.objectContaining({
force: true,
port: "19000",
token: "tok_test",
}),
);
});
it("forwards status auth collisions from parent gateway command", async () => {
const gateway = createGatewayParentLikeCommand();
await gateway.parseAsync(["status", "--token", "tok_status", "--password", "pw_status"], {
from: "user",
});
expect(runDaemonStatus).toHaveBeenCalledWith(
expect.objectContaining({
rpc: expect.objectContaining({
token: "tok_status",
password: "pw_status",
}),
}),
);
});
});