mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 03:37:27 +00:00
test(cli): dedupe runtime capture fixtures across command specs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
|
||||
|
||||
const callGateway = vi.fn(async (..._args: unknown[]) => ({ ok: true }));
|
||||
const resolveGatewayProgramArguments = vi.fn(async (_opts?: unknown) => ({
|
||||
@@ -20,15 +21,7 @@ const inspectPortUsage = vi.fn(async (port: number) => ({
|
||||
hints: [],
|
||||
}));
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const defaultRuntime = {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
};
|
||||
const { runtimeLogs, defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
vi.mock("../gateway/call.js", () => ({
|
||||
callGateway: (opts: unknown) => callGateway(opts),
|
||||
@@ -122,8 +115,7 @@ describe("daemon-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("probes gateway status by default", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGateway.mockClear();
|
||||
|
||||
const { registerDaemonCli } = await import("./daemon-cli.js");
|
||||
@@ -140,8 +132,7 @@ describe("daemon-cli coverage", () => {
|
||||
}, 20_000);
|
||||
|
||||
it("derives probe URL from service args + env (json)", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGateway.mockClear();
|
||||
inspectPortUsage.mockClear();
|
||||
|
||||
@@ -218,8 +209,7 @@ describe("daemon-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("installs the daemon with json output", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
serviceIsLoaded.mockResolvedValueOnce(false);
|
||||
serviceInstall.mockClear();
|
||||
|
||||
@@ -261,8 +251,7 @@ describe("daemon-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("emits json for daemon start/stop", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
serviceRestart.mockClear();
|
||||
serviceStop.mockClear();
|
||||
serviceIsLoaded.mockResolvedValue(true);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
|
||||
|
||||
const callGatewayFromCli = vi.fn(async (method: string, _opts: unknown, params?: unknown) => {
|
||||
if (method.endsWith(".get")) {
|
||||
@@ -13,15 +14,7 @@ const callGatewayFromCli = vi.fn(async (method: string, _opts: unknown, params?:
|
||||
return { method, params };
|
||||
});
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const defaultRuntime = {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
};
|
||||
const { runtimeErrors, defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
const localSnapshot = {
|
||||
path: "/tmp/local-exec-approvals.json",
|
||||
@@ -71,8 +64,7 @@ describe("exec approvals CLI", () => {
|
||||
};
|
||||
|
||||
it("routes get command to local, gateway, and node modes", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGatewayFromCli.mockClear();
|
||||
|
||||
const localProgram = createProgram();
|
||||
@@ -99,8 +91,7 @@ describe("exec approvals CLI", () => {
|
||||
});
|
||||
|
||||
it("defaults allowlist add to wildcard agent", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGatewayFromCli.mockClear();
|
||||
|
||||
const saveExecApprovals = vi.mocked(execApprovals.saveExecApprovals);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Command } from "commander";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { withEnvOverride } from "../config/test-helpers.js";
|
||||
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
|
||||
|
||||
type DiscoveredBeacon = Awaited<
|
||||
ReturnType<typeof import("../infra/bonjour-discovery.js").discoverGatewayBeacons>
|
||||
@@ -26,15 +27,8 @@ const discoverGatewayBeacons = vi.fn<(opts: unknown) => Promise<DiscoveredBeacon
|
||||
);
|
||||
const gatewayStatusCommand = vi.fn<(opts: unknown) => Promise<void>>(async () => {});
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const defaultRuntime = {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
};
|
||||
const { runtimeLogs, runtimeErrors, defaultRuntime, resetRuntimeCapture } =
|
||||
createCliRuntimeCapture();
|
||||
|
||||
vi.mock(
|
||||
new URL("../../gateway/call.ts", new URL("./gateway-cli/call.ts", import.meta.url)).href,
|
||||
@@ -93,8 +87,7 @@ vi.mock("../commands/gateway-status.js", () => ({
|
||||
|
||||
describe("gateway-cli coverage", () => {
|
||||
it("registers call/health commands and routes to callGateway", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGateway.mockClear();
|
||||
|
||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
||||
@@ -111,8 +104,7 @@ describe("gateway-cli coverage", () => {
|
||||
}, 60_000);
|
||||
|
||||
it("registers gateway probe and routes to gatewayStatusCommand", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
gatewayStatusCommand.mockClear();
|
||||
|
||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
||||
@@ -126,8 +118,7 @@ describe("gateway-cli coverage", () => {
|
||||
}, 60_000);
|
||||
|
||||
it("registers gateway discover and prints JSON", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
discoverGatewayBeacons.mockReset();
|
||||
discoverGatewayBeacons.mockResolvedValueOnce([
|
||||
{
|
||||
@@ -158,8 +149,7 @@ describe("gateway-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("registers gateway discover and prints human output with details on new lines", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
discoverGatewayBeacons.mockReset();
|
||||
discoverGatewayBeacons.mockResolvedValueOnce([
|
||||
{
|
||||
@@ -193,8 +183,7 @@ describe("gateway-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("validates gateway discover timeout", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
discoverGatewayBeacons.mockReset();
|
||||
|
||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
||||
@@ -213,8 +202,7 @@ describe("gateway-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("fails gateway call on invalid params JSON", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGateway.mockClear();
|
||||
|
||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
||||
@@ -231,8 +219,7 @@ describe("gateway-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("validates gateway ports and handles force/start errors", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
|
||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
||||
|
||||
@@ -288,8 +275,7 @@ describe("gateway-cli coverage", () => {
|
||||
});
|
||||
|
||||
it("prints stop hints on GatewayLockError when service is loaded", async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
serviceIsLoaded.mockResolvedValue(true);
|
||||
|
||||
const { GatewayLockError } = await import("../infra/gateway-lock.js");
|
||||
@@ -315,8 +301,7 @@ describe("gateway-cli coverage", () => {
|
||||
|
||||
it("uses env/config port when --port is omitted", async () => {
|
||||
await withEnvOverride({ OPENCLAW_GATEWAY_PORT: "19001" }, async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
startGatewayServer.mockClear();
|
||||
|
||||
const { registerGatewayCli } = await import("./gateway-cli.js");
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
|
||||
|
||||
const callGatewayCli = vi.fn(async (_method: string, _opts: unknown, _params?: unknown) => ({
|
||||
ok: true,
|
||||
}));
|
||||
const gatewayStatusCommand = vi.fn(async (_opts: unknown, _runtime: unknown) => {});
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const defaultRuntime = {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
};
|
||||
const { defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
vi.mock("../cli-utils.js", () => ({
|
||||
runCommandWithRuntime: async (
|
||||
@@ -119,8 +112,7 @@ vi.mock("./discover.js", () => ({
|
||||
|
||||
describe("gateway register option collisions", () => {
|
||||
beforeEach(() => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGatewayCli.mockClear();
|
||||
gatewayStatusCommand.mockClear();
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
|
||||
|
||||
const startGatewayServer = vi.fn(async (_port: number, _opts?: unknown) => ({
|
||||
close: vi.fn(async () => {}),
|
||||
@@ -16,15 +17,7 @@ const runGatewayLoop = vi.fn(async ({ start }: { start: () => Promise<unknown> }
|
||||
await start();
|
||||
});
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const defaultRuntime = {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
};
|
||||
const { defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
vi.mock("../../config/config.js", () => ({
|
||||
getConfigPath: () => "/tmp/openclaw-test-missing-config.json",
|
||||
@@ -99,8 +92,7 @@ vi.mock("./run-loop.js", () => ({
|
||||
|
||||
describe("gateway run option collisions", () => {
|
||||
beforeEach(() => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
startGatewayServer.mockClear();
|
||||
setGatewayWsLogStyle.mockClear();
|
||||
setVerbose.mockClear();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
|
||||
|
||||
type NodeInvokeCall = {
|
||||
method?: string;
|
||||
@@ -61,15 +62,7 @@ const callGateway = vi.fn(async (opts: NodeInvokeCall) => {
|
||||
|
||||
const randomIdempotencyKey = vi.fn(() => "rk_test");
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const defaultRuntime = {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
};
|
||||
const { defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
vi.mock("../gateway/call.js", () => ({
|
||||
callGateway: (opts: unknown) => callGateway(opts as NodeInvokeCall),
|
||||
@@ -95,8 +88,7 @@ describe("nodes-cli coverage", () => {
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
resetRuntimeCapture();
|
||||
callGateway.mockClear();
|
||||
randomIdempotencyKey.mockClear();
|
||||
});
|
||||
|
||||
28
src/cli/test-runtime-capture.ts
Normal file
28
src/cli/test-runtime-capture.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
|
||||
export type CliRuntimeCapture = {
|
||||
runtimeLogs: string[];
|
||||
runtimeErrors: string[];
|
||||
defaultRuntime: Pick<RuntimeEnv, "log" | "error" | "exit">;
|
||||
resetRuntimeCapture: () => void;
|
||||
};
|
||||
|
||||
export function createCliRuntimeCapture(): CliRuntimeCapture {
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
return {
|
||||
runtimeLogs,
|
||||
runtimeErrors,
|
||||
defaultRuntime: {
|
||||
log: (msg: string) => runtimeLogs.push(msg),
|
||||
error: (msg: string) => runtimeErrors.push(msg),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
},
|
||||
resetRuntimeCapture: () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user