mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-17 21:10:54 +00:00
fix: harden gateway status rpc smoke
This commit is contained in:
@@ -67,6 +67,17 @@ describe("addGatewayServiceCommands", () => {
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "forwards require-rpc for status",
|
||||
argv: ["status", "--require-rpc"],
|
||||
assert: () => {
|
||||
expect(runDaemonStatus).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
requireRpc: true,
|
||||
}),
|
||||
);
|
||||
},
|
||||
},
|
||||
])("$name", async ({ argv, assert }) => {
|
||||
const gateway = createGatewayParentLikeCommand();
|
||||
await gateway.parseAsync(argv, { from: "user" });
|
||||
|
||||
@@ -44,12 +44,14 @@ export function addGatewayServiceCommands(parent: Command, opts?: { statusDescri
|
||||
.option("--password <password>", "Gateway password (password auth)")
|
||||
.option("--timeout <ms>", "Timeout in ms", "10000")
|
||||
.option("--no-probe", "Skip RPC probe")
|
||||
.option("--require-rpc", "Exit non-zero when the RPC probe fails", false)
|
||||
.option("--deep", "Scan system-level services", false)
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (cmdOpts, command) => {
|
||||
await runDaemonStatus({
|
||||
rpc: resolveRpcOptions(cmdOpts, command),
|
||||
probe: Boolean(cmdOpts.probe),
|
||||
requireRpc: Boolean(cmdOpts.requireRpc),
|
||||
deep: Boolean(cmdOpts.deep),
|
||||
json: Boolean(cmdOpts.json),
|
||||
});
|
||||
|
||||
89
src/cli/daemon-cli/status.test.ts
Normal file
89
src/cli/daemon-cli/status.test.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
|
||||
|
||||
const gatherDaemonStatus = vi.fn(async (_opts?: unknown) => ({
|
||||
service: {
|
||||
label: "LaunchAgent",
|
||||
loaded: true,
|
||||
loadedText: "loaded",
|
||||
notLoadedText: "not loaded",
|
||||
},
|
||||
rpc: {
|
||||
ok: true,
|
||||
url: "ws://127.0.0.1:18789",
|
||||
},
|
||||
extraServices: [],
|
||||
}));
|
||||
const printDaemonStatus = vi.fn();
|
||||
|
||||
const { runtimeErrors, defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
vi.mock("../../runtime.js", () => ({
|
||||
defaultRuntime,
|
||||
}));
|
||||
|
||||
vi.mock("../../terminal/theme.js", () => ({
|
||||
colorize: (_rich: boolean, _color: unknown, text: string) => text,
|
||||
isRich: () => false,
|
||||
theme: { error: "error" },
|
||||
}));
|
||||
|
||||
vi.mock("./status.gather.js", () => ({
|
||||
gatherDaemonStatus: (opts: unknown) => gatherDaemonStatus(opts),
|
||||
}));
|
||||
|
||||
vi.mock("./status.print.js", () => ({
|
||||
printDaemonStatus: (...args: unknown[]) => printDaemonStatus(...args),
|
||||
}));
|
||||
|
||||
const { runDaemonStatus } = await import("./status.js");
|
||||
|
||||
describe("runDaemonStatus", () => {
|
||||
beforeEach(() => {
|
||||
gatherDaemonStatus.mockClear();
|
||||
printDaemonStatus.mockClear();
|
||||
resetRuntimeCapture();
|
||||
});
|
||||
|
||||
it("exits when require-rpc is set and the probe fails", async () => {
|
||||
gatherDaemonStatus.mockResolvedValueOnce({
|
||||
service: {
|
||||
label: "LaunchAgent",
|
||||
loaded: true,
|
||||
loadedText: "loaded",
|
||||
notLoadedText: "not loaded",
|
||||
},
|
||||
rpc: {
|
||||
ok: false,
|
||||
url: "ws://127.0.0.1:18789",
|
||||
error: "gateway closed",
|
||||
},
|
||||
extraServices: [],
|
||||
});
|
||||
|
||||
await expect(
|
||||
runDaemonStatus({
|
||||
rpc: {},
|
||||
probe: true,
|
||||
requireRpc: true,
|
||||
json: false,
|
||||
}),
|
||||
).rejects.toThrow("__exit__:1");
|
||||
|
||||
expect(printDaemonStatus).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("rejects require-rpc when probing is disabled", async () => {
|
||||
await expect(
|
||||
runDaemonStatus({
|
||||
rpc: {},
|
||||
probe: false,
|
||||
requireRpc: true,
|
||||
json: false,
|
||||
}),
|
||||
).rejects.toThrow("__exit__:1");
|
||||
|
||||
expect(gatherDaemonStatus).not.toHaveBeenCalled();
|
||||
expect(runtimeErrors.join("\n")).toContain("--require-rpc cannot be used with --no-probe");
|
||||
});
|
||||
});
|
||||
@@ -6,12 +6,20 @@ import type { DaemonStatusOptions } from "./types.js";
|
||||
|
||||
export async function runDaemonStatus(opts: DaemonStatusOptions) {
|
||||
try {
|
||||
if (opts.requireRpc && !opts.probe) {
|
||||
defaultRuntime.error("Gateway status failed: --require-rpc cannot be used with --no-probe.");
|
||||
defaultRuntime.exit(1);
|
||||
return;
|
||||
}
|
||||
const status = await gatherDaemonStatus({
|
||||
rpc: opts.rpc,
|
||||
probe: Boolean(opts.probe),
|
||||
deep: Boolean(opts.deep),
|
||||
});
|
||||
printDaemonStatus(status, { json: Boolean(opts.json) });
|
||||
if (opts.requireRpc && !status.rpc?.ok) {
|
||||
defaultRuntime.exit(1);
|
||||
}
|
||||
} catch (err) {
|
||||
const rich = isRich();
|
||||
defaultRuntime.error(colorize(rich, theme.error, `Gateway status failed: ${String(err)}`));
|
||||
|
||||
@@ -11,6 +11,7 @@ export type GatewayRpcOpts = {
|
||||
export type DaemonStatusOptions = {
|
||||
rpc: GatewayRpcOpts;
|
||||
probe: boolean;
|
||||
requireRpc: boolean;
|
||||
json: boolean;
|
||||
} & FindExtraGatewayServicesOptions;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user