mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 10:10:45 +00:00
test: share CLI runtime mock helper
This commit is contained in:
@@ -2,28 +2,9 @@ import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { registerDirectoryCli } from "./directory-cli.js";
|
||||
|
||||
const runtimeState = vi.hoisted(() => {
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const stringifyArgs = (args: unknown[]) => args.map((value) => String(value)).join(" ");
|
||||
const defaultRuntime = {
|
||||
log: vi.fn((...args: unknown[]) => {
|
||||
runtimeLogs.push(stringifyArgs(args));
|
||||
}),
|
||||
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 { defaultRuntime, runtimeLogs, runtimeErrors };
|
||||
const runtimeState = await vi.hoisted(async () => {
|
||||
const { createCliRuntimeMock } = await import("./test-runtime-mock.js");
|
||||
return createCliRuntimeMock(vi, { exitPrefix: "exit" });
|
||||
});
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
|
||||
@@ -5,27 +5,9 @@ import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { registerSecretsCli } from "./secrets-cli.js";
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const stringifyArgs = (args: unknown[]) => args.map((value) => String(value)).join(" ");
|
||||
const defaultRuntime = {
|
||||
log: vi.fn((...args: unknown[]) => {
|
||||
runtimeLogs.push(stringifyArgs(args));
|
||||
}),
|
||||
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}`);
|
||||
}),
|
||||
};
|
||||
const mocks = await vi.hoisted(async () => {
|
||||
const { createCliRuntimeMock } = await import("./test-runtime-mock.js");
|
||||
const runtime = createCliRuntimeMock(vi);
|
||||
return {
|
||||
callGatewayFromCli: vi.fn(),
|
||||
runSecretsAudit: vi.fn(),
|
||||
@@ -33,9 +15,7 @@ const mocks = vi.hoisted(() => {
|
||||
runSecretsConfigureInteractive: vi.fn(),
|
||||
runSecretsApply: vi.fn(),
|
||||
confirm: vi.fn(),
|
||||
defaultRuntime,
|
||||
runtimeLogs,
|
||||
runtimeErrors,
|
||||
...runtime,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
36
src/cli/test-runtime-mock.ts
Normal file
36
src/cli/test-runtime-mock.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { vi } from "vitest";
|
||||
|
||||
type ViLike = Pick<typeof vi, "fn">;
|
||||
|
||||
export function createCliRuntimeMock(
|
||||
viInstance: ViLike,
|
||||
options: {
|
||||
exitPrefix?: string;
|
||||
} = {},
|
||||
) {
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const stringifyArgs = (args: unknown[]) => args.map((value) => String(value)).join(" ");
|
||||
const defaultRuntime = {
|
||||
log: viInstance.fn((...args: unknown[]) => {
|
||||
runtimeLogs.push(stringifyArgs(args));
|
||||
}),
|
||||
error: viInstance.fn((...args: unknown[]) => {
|
||||
runtimeErrors.push(stringifyArgs(args));
|
||||
}),
|
||||
writeStdout: viInstance.fn((value: string) => {
|
||||
defaultRuntime.log(value.endsWith("\n") ? value.slice(0, -1) : value);
|
||||
}),
|
||||
writeJson: viInstance.fn((value: unknown, space = 2) => {
|
||||
defaultRuntime.log(JSON.stringify(value, null, space > 0 ? space : undefined));
|
||||
}),
|
||||
exit: viInstance.fn((code: number) => {
|
||||
throw new Error(`${options.exitPrefix ?? "__exit__"}:${code}`);
|
||||
}),
|
||||
};
|
||||
return {
|
||||
defaultRuntime,
|
||||
runtimeLogs,
|
||||
runtimeErrors,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user