mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-10 16:51:13 +00:00
test: speed up cli and command suites
This commit is contained in:
@@ -1,9 +1,29 @@
|
||||
import { Command } from "commander";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
|
||||
import { registerCronCli } from "./cron-cli.js";
|
||||
|
||||
const CRON_CLI_TEST_TIMEOUT_MS = 15_000;
|
||||
const { defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
const mocks = vi.hoisted(() => {
|
||||
const defaultRuntime = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn(),
|
||||
writeStdout: vi.fn((value: string) => {
|
||||
defaultRuntime.log(value.endsWith("\n") ? value.slice(0, -1) : value);
|
||||
}),
|
||||
writeJson: vi.fn((value: unknown, space = 2) => {
|
||||
defaultRuntime.log(JSON.stringify(value, null, space > 0 ? space : undefined));
|
||||
}),
|
||||
exit: vi.fn((code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
}),
|
||||
};
|
||||
return {
|
||||
defaultRuntime,
|
||||
callGatewayFromCli: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
const { defaultRuntime, callGatewayFromCli } = mocks;
|
||||
|
||||
const defaultGatewayMock = async (
|
||||
method: string,
|
||||
@@ -16,23 +36,21 @@ const defaultGatewayMock = async (
|
||||
}
|
||||
return { ok: true, params };
|
||||
};
|
||||
const callGatewayFromCli = vi.fn(defaultGatewayMock);
|
||||
callGatewayFromCli.mockImplementation(defaultGatewayMock);
|
||||
|
||||
vi.mock("./gateway-rpc.js", async () => {
|
||||
const actual = await vi.importActual<typeof import("./gateway-rpc.js")>("./gateway-rpc.js");
|
||||
return {
|
||||
...actual,
|
||||
callGatewayFromCli: (method: string, opts: unknown, params?: unknown, extra?: unknown) =>
|
||||
callGatewayFromCli(method, opts, params, extra as number | undefined),
|
||||
mocks.callGatewayFromCli(method, opts, params, extra as number | undefined),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../runtime.js", () => ({
|
||||
defaultRuntime,
|
||||
defaultRuntime: mocks.defaultRuntime,
|
||||
}));
|
||||
|
||||
const { registerCronCli } = await import("./cron-cli.js");
|
||||
|
||||
type CronUpdatePatch = {
|
||||
patch?: {
|
||||
schedule?: { kind?: string; expr?: string; tz?: string; staggerMs?: number };
|
||||
@@ -72,7 +90,11 @@ function buildProgram() {
|
||||
function resetGatewayMock() {
|
||||
callGatewayFromCli.mockClear();
|
||||
callGatewayFromCli.mockImplementation(defaultGatewayMock);
|
||||
resetRuntimeCapture();
|
||||
defaultRuntime.log.mockClear();
|
||||
defaultRuntime.error.mockClear();
|
||||
defaultRuntime.writeStdout.mockClear();
|
||||
defaultRuntime.writeJson.mockClear();
|
||||
defaultRuntime.exit.mockClear();
|
||||
}
|
||||
|
||||
async function runCronCommand(args: string[]): Promise<void> {
|
||||
@@ -176,8 +198,7 @@ async function runCronRunAndCaptureExit(params: {
|
||||
},
|
||||
);
|
||||
|
||||
const runtimeModule = await import("../runtime.js");
|
||||
const runtime = runtimeModule.defaultRuntime as { exit: (code: number) => void };
|
||||
const runtime = defaultRuntime as { exit: (code: number) => void };
|
||||
const originalExit = runtime.exit;
|
||||
const exitSpy = vi.fn();
|
||||
runtime.exit = exitSpy;
|
||||
|
||||
Reference in New Issue
Block a user