mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-04 13:51:30 +00:00
test: speed up cli and command suites
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createCliRuntimeCapture } from "./test-runtime-capture.js";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { registerNodesCli } from "./nodes-cli.js";
|
||||
|
||||
type NodeInvokeCall = {
|
||||
method?: string;
|
||||
@@ -46,7 +46,31 @@ const callGateway = vi.fn(async (opts: NodeInvokeCall) => {
|
||||
|
||||
const randomIdempotencyKey = vi.fn(() => "rk_test");
|
||||
|
||||
const { runtimeErrors, defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
const mocks = vi.hoisted(() => {
|
||||
const runtimeErrors: string[] = [];
|
||||
const stringifyArgs = (args: unknown[]) => args.map((value) => String(value)).join(" ");
|
||||
const defaultRuntime = {
|
||||
log: vi.fn(),
|
||||
error: vi.fn((...args: unknown[]) => {
|
||||
runtimeErrors.push(stringifyArgs(args));
|
||||
}),
|
||||
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 {
|
||||
runtimeErrors,
|
||||
defaultRuntime,
|
||||
};
|
||||
});
|
||||
|
||||
const { runtimeErrors, defaultRuntime } = mocks;
|
||||
|
||||
vi.mock("../gateway/call.js", () => ({
|
||||
callGateway: (opts: unknown) => callGateway(opts as NodeInvokeCall),
|
||||
@@ -55,12 +79,11 @@ vi.mock("../gateway/call.js", () => ({
|
||||
|
||||
vi.mock("../runtime.js", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("../runtime.js")>()),
|
||||
defaultRuntime,
|
||||
defaultRuntime: mocks.defaultRuntime,
|
||||
}));
|
||||
|
||||
describe("nodes-cli coverage", () => {
|
||||
let registerNodesCli: (program: Command) => void;
|
||||
let sharedProgram: Command;
|
||||
let sharedProgram: Command = new Command();
|
||||
|
||||
const getNodeInvokeCall = () => {
|
||||
const last = lastNodeInvokeCall;
|
||||
@@ -75,17 +98,20 @@ describe("nodes-cli coverage", () => {
|
||||
return getNodeInvokeCall();
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
({ registerNodesCli } = await import("./nodes-cli.js"));
|
||||
sharedProgram = new Command();
|
||||
if (sharedProgram.commands.length === 0) {
|
||||
sharedProgram.exitOverride();
|
||||
registerNodesCli(sharedProgram);
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetRuntimeCapture();
|
||||
runtimeErrors.length = 0;
|
||||
callGateway.mockClear();
|
||||
randomIdempotencyKey.mockClear();
|
||||
defaultRuntime.log.mockClear();
|
||||
defaultRuntime.error.mockClear();
|
||||
defaultRuntime.writeStdout.mockClear();
|
||||
defaultRuntime.writeJson.mockClear();
|
||||
defaultRuntime.exit.mockClear();
|
||||
lastNodeInvokeCall = null;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user