mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-30 19:32:27 +00:00
refactor: reuse shared cli runtime test mocks
This commit is contained in:
@@ -3,9 +3,9 @@ import path from "node:path";
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { makeTempWorkspace } from "../../test-helpers/workspace.js";
|
||||
import { captureEnv } from "../../test-utils/env.js";
|
||||
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const runtimeErrors: string[] = [];
|
||||
const { runtimeLogs, defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
|
||||
const serviceMock = vi.hoisted(() => ({
|
||||
label: "Gateway",
|
||||
@@ -24,24 +24,9 @@ vi.mock("../../daemon/service.js", () => ({
|
||||
resolveGatewayService: () => serviceMock,
|
||||
}));
|
||||
|
||||
vi.mock("../../runtime.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../../runtime.js")>();
|
||||
return {
|
||||
...actual,
|
||||
defaultRuntime: {
|
||||
...actual.defaultRuntime,
|
||||
log: (message: string) => runtimeLogs.push(message),
|
||||
error: (message: string) => runtimeErrors.push(message),
|
||||
writeStdout: (value: string) =>
|
||||
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value),
|
||||
writeJson: (value: unknown, space = 2) =>
|
||||
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined)),
|
||||
exit: (code: number) => {
|
||||
throw new Error(`__exit__:${code}`);
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
vi.mock("../../runtime.js", () => ({
|
||||
defaultRuntime,
|
||||
}));
|
||||
|
||||
const { runDaemonInstall } = await import("./install.js");
|
||||
const { clearConfigCache } = await import("../../config/config.js");
|
||||
@@ -78,9 +63,8 @@ describe("runDaemonInstall integration", () => {
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
runtimeLogs.length = 0;
|
||||
runtimeErrors.length = 0;
|
||||
vi.clearAllMocks();
|
||||
resetRuntimeCapture();
|
||||
// Keep these defined-but-empty so dotenv won't repopulate from local .env.
|
||||
process.env.OPENCLAW_GATEWAY_TOKEN = "";
|
||||
process.env.CLAWDBOT_GATEWAY_TOKEN = "";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { captureFullEnv } from "../../test-utils/env.js";
|
||||
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
|
||||
import type { DaemonActionResponse } from "./response.js";
|
||||
|
||||
const resolveNodeStartupTlsEnvironmentMock = vi.hoisted(() => vi.fn());
|
||||
@@ -125,19 +126,9 @@ vi.mock("./response.js", () => ({
|
||||
installDaemonServiceAndEmit: installDaemonServiceAndEmitMock,
|
||||
}));
|
||||
|
||||
const runtimeLogs: string[] = [];
|
||||
const { defaultRuntime, resetRuntimeCapture } = createCliRuntimeCapture();
|
||||
vi.mock("../../runtime.js", () => ({
|
||||
defaultRuntime: {
|
||||
log: (message: string) => runtimeLogs.push(message),
|
||||
writeStdout: (value: string) => {
|
||||
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
|
||||
},
|
||||
writeJson: (value: unknown, space = 2) => {
|
||||
runtimeLogs.push(JSON.stringify(value, null, space));
|
||||
},
|
||||
error: vi.fn(),
|
||||
exit: vi.fn(),
|
||||
},
|
||||
defaultRuntime,
|
||||
}));
|
||||
|
||||
function expectFirstInstallPlanCallOmitsToken() {
|
||||
@@ -176,7 +167,7 @@ describe("runDaemonInstall", () => {
|
||||
isGatewayDaemonRuntimeMock.mockReset();
|
||||
installDaemonServiceAndEmitMock.mockReset();
|
||||
service.isLoaded.mockReset();
|
||||
runtimeLogs.length = 0;
|
||||
resetRuntimeCapture();
|
||||
actionState.warnings.length = 0;
|
||||
actionState.emitted.length = 0;
|
||||
actionState.failed.length = 0;
|
||||
|
||||
Reference in New Issue
Block a user