test: micro-optimize slow suites and CLI command setup

This commit is contained in:
Peter Steinberger
2026-03-02 23:00:42 +00:00
parent ba5ae5b4f1
commit 2287d1ec13
7 changed files with 259 additions and 164 deletions

View File

@@ -1,5 +1,5 @@
import { Command } from "commander";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { ConfigFileSnapshot, OpenClawConfig } from "../config/types.js";
/**
@@ -61,27 +61,24 @@ function setSnapshotOnce(snapshot: ConfigFileSnapshot) {
}
let registerConfigCli: typeof import("./config-cli.js").registerConfigCli;
let sharedProgram: Command;
async function runConfigCommand(args: string[]) {
const program = new Command();
program.exitOverride();
registerConfigCli(program);
await program.parseAsync(args, { from: "user" });
await sharedProgram.parseAsync(args, { from: "user" });
}
describe("config cli", () => {
beforeAll(async () => {
({ registerConfigCli } = await import("./config-cli.js"));
sharedProgram = new Command();
sharedProgram.exitOverride();
registerConfigCli(sharedProgram);
});
beforeEach(() => {
vi.clearAllMocks();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("config set - issue #6070", () => {
it("preserves existing config keys when setting a new value", async () => {
const resolved: OpenClawConfig = {

View File

@@ -1,6 +1,5 @@
import { Command } from "commander";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { runRegisteredCli } from "../../test-utils/command-runner.js";
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
const callGatewayCli = vi.fn(async (_method: string, _opts: unknown, _params?: unknown) => ({
@@ -113,9 +112,13 @@ vi.mock("./discover.js", () => ({
describe("gateway register option collisions", () => {
let registerGatewayCli: typeof import("./register.js").registerGatewayCli;
let sharedProgram: Command;
beforeAll(async () => {
({ registerGatewayCli } = await import("./register.js"));
sharedProgram = new Command();
sharedProgram.exitOverride();
registerGatewayCli(sharedProgram);
});
beforeEach(() => {
@@ -125,9 +128,8 @@ describe("gateway register option collisions", () => {
});
it("forwards --token to gateway call when parent and child option names collide", async () => {
await runRegisteredCli({
register: registerGatewayCli as (program: Command) => void,
argv: ["gateway", "call", "health", "--token", "tok_call", "--json"],
await sharedProgram.parseAsync(["gateway", "call", "health", "--token", "tok_call", "--json"], {
from: "user",
});
expect(callGatewayCli).toHaveBeenCalledWith(
@@ -140,9 +142,8 @@ describe("gateway register option collisions", () => {
});
it("forwards --token to gateway probe when parent and child option names collide", async () => {
await runRegisteredCli({
register: registerGatewayCli as (program: Command) => void,
argv: ["gateway", "probe", "--token", "tok_probe", "--json"],
await sharedProgram.parseAsync(["gateway", "probe", "--token", "tok_probe", "--json"], {
from: "user",
});
expect(gatewayStatusCommand).toHaveBeenCalledWith(

View File

@@ -1,6 +1,5 @@
import { Command } from "commander";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { runRegisteredCli } from "../../test-utils/command-runner.js";
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
const startGatewayServer = vi.fn(async (_port: number, _opts?: unknown) => ({
@@ -93,9 +92,14 @@ vi.mock("./run-loop.js", () => ({
describe("gateway run option collisions", () => {
let addGatewayRunCommand: typeof import("./run.js").addGatewayRunCommand;
let sharedProgram: Command;
beforeAll(async () => {
({ addGatewayRunCommand } = await import("./run.js"));
sharedProgram = new Command();
sharedProgram.exitOverride();
const gateway = addGatewayRunCommand(sharedProgram.command("gateway"));
addGatewayRunCommand(gateway.command("run"));
});
beforeEach(() => {
@@ -109,13 +113,7 @@ describe("gateway run option collisions", () => {
});
async function runGatewayCli(argv: string[]) {
await runRegisteredCli({
register: ((program: Command) => {
const gateway = addGatewayRunCommand(program.command("gateway"));
addGatewayRunCommand(gateway.command("run"));
}) as (program: Command) => void,
argv,
});
await sharedProgram.parseAsync(argv, { from: "user" });
}
function expectAuthOverrideMode(mode: string) {

View File

@@ -12,6 +12,9 @@ type NodeInvokeCall = {
};
};
let lastNodeInvokeCall: NodeInvokeCall | null = null;
let lastApprovalRequestCall: { params?: Record<string, unknown> } | null = null;
const callGateway = vi.fn(async (opts: NodeInvokeCall) => {
if (opts.method === "node.list") {
return {
@@ -28,6 +31,7 @@ const callGateway = vi.fn(async (opts: NodeInvokeCall) => {
};
}
if (opts.method === "node.invoke") {
lastNodeInvokeCall = opts;
const command = opts.params?.command;
if (command === "system.run.prepare") {
const params = (opts.params?.params ?? {}) as {
@@ -83,6 +87,7 @@ const callGateway = vi.fn(async (opts: NodeInvokeCall) => {
};
}
if (opts.method === "exec.approval.request") {
lastApprovalRequestCall = opts as { params?: Record<string, unknown> };
return { decision: "allow-once" };
}
return { ok: true };
@@ -107,44 +112,36 @@ vi.mock("../config/config.js", () => ({
describe("nodes-cli coverage", () => {
let registerNodesCli: (program: Command) => void;
let sharedProgram: Command;
const getNodeInvokeCall = () => {
const nodeInvokeCalls = callGateway.mock.calls
.map((call) => call[0])
.filter((entry): entry is NodeInvokeCall => entry?.method === "node.invoke");
const last = nodeInvokeCalls.at(-1);
const last = lastNodeInvokeCall;
if (!last) {
throw new Error("expected node.invoke call");
}
return last;
};
const getApprovalRequestCall = () =>
callGateway.mock.calls.find((call) => call[0]?.method === "exec.approval.request")?.[0] as {
params?: Record<string, unknown>;
};
const createNodesProgram = () => {
const program = new Command();
program.exitOverride();
registerNodesCli(program);
return program;
};
const getApprovalRequestCall = () => lastApprovalRequestCall;
const runNodesCommand = async (args: string[]) => {
const program = createNodesProgram();
await program.parseAsync(args, { from: "user" });
await sharedProgram.parseAsync(args, { from: "user" });
return getNodeInvokeCall();
};
beforeAll(async () => {
({ registerNodesCli } = await import("./nodes-cli.js"));
sharedProgram = new Command();
sharedProgram.exitOverride();
registerNodesCli(sharedProgram);
});
beforeEach(() => {
resetRuntimeCapture();
callGateway.mockClear();
randomIdempotencyKey.mockClear();
lastNodeInvokeCall = null;
lastApprovalRequestCall = null;
});
it("invokes system.run with parsed params", async () => {