From a07b9fc8408a92db39e4e2c0fa942844bc5fad50 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 18 Apr 2026 19:51:08 +0100 Subject: [PATCH] test: trim runtime import surfaces --- .../bash-tools.exec.approval-id.test.ts | 17 ++++++++++++-- src/agents/cli-runner/bundle-mcp.test.ts | 2 +- ...els-config.runtime-source-snapshot.test.ts | 22 +++++++------------ src/agents/pi-bundle-mcp-materialize.ts | 2 +- .../pi-bundle-mcp-tools.materialize.test.ts | 8 +++---- src/agents/session-write-lock.test.ts | 4 ++-- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/agents/bash-tools.exec.approval-id.test.ts b/src/agents/bash-tools.exec.approval-id.test.ts index c9eb7148167..3d5a8134a33 100644 --- a/src/agents/bash-tools.exec.approval-id.test.ts +++ b/src/agents/bash-tools.exec.approval-id.test.ts @@ -2,7 +2,7 @@ import crypto from "node:crypto"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { clearConfigCache, clearRuntimeConfigSnapshot } from "../config/io.js"; import { sendMessage } from "../infra/outbound/message.js"; import { buildSystemRunPreparePayload } from "../test-utils/system-run-prepare-payload.js"; @@ -221,13 +221,20 @@ describe("exec approvals", () => { let previousUserProfile: string | undefined; let previousBundledPluginsDir: string | undefined; let previousDisableBundledPlugins: string | undefined; + let tempRoot = ""; + let tempCaseIndex = 0; + + beforeAll(async () => { + tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-exec-approvals-")); + }); beforeEach(async () => { previousHome = process.env.HOME; previousUserProfile = process.env.USERPROFILE; previousBundledPluginsDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; previousDisableBundledPlugins = process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS; - const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-")); + const tempDir = path.join(tempRoot, `case-${++tempCaseIndex}`); + await fs.mkdir(tempDir, { recursive: true }); process.env.HOME = tempDir; // Windows uses USERPROFILE for os.homedir() process.env.USERPROFILE = tempDir; @@ -263,6 +270,12 @@ describe("exec approvals", () => { } }); + afterAll(async () => { + if (tempRoot) { + await fs.rm(tempRoot, { recursive: true, force: true }); + } + }); + it("reuses approval id as the node runId", async () => { let invokeParams: unknown; let agentParams: unknown; diff --git a/src/agents/cli-runner/bundle-mcp.test.ts b/src/agents/cli-runner/bundle-mcp.test.ts index 86f74ec67f5..c71041129e0 100644 --- a/src/agents/cli-runner/bundle-mcp.test.ts +++ b/src/agents/cli-runner/bundle-mcp.test.ts @@ -1,7 +1,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import { afterAll, describe, expect, it } from "vitest"; -import type { OpenClawConfig } from "../../config/config.js"; +import type { OpenClawConfig } from "../../config/types.openclaw.js"; import { createBundleMcpTempHarness, createBundleProbePlugin, diff --git a/src/agents/models-config.runtime-source-snapshot.test.ts b/src/agents/models-config.runtime-source-snapshot.test.ts index 64e2bd403f8..14dadca95e1 100644 --- a/src/agents/models-config.runtime-source-snapshot.test.ts +++ b/src/agents/models-config.runtime-source-snapshot.test.ts @@ -9,20 +9,14 @@ import { withTempEnv, } from "./models-config.e2e-harness.js"; -vi.mock("../plugins/provider-runtime.js", async () => { - const actual = await vi.importActual( - "../plugins/provider-runtime.js", - ); - return { - ...actual, - applyProviderConfigDefaultsWithPlugin: (config: OpenClawConfig) => config, - applyProviderNativeStreamingUsageCompatWithPlugin: () => undefined, - normalizeProviderConfigWithPlugin: () => undefined, - resetProviderRuntimeHookCacheForTest: () => undefined, - resolveProviderConfigApiKeyWithPlugin: () => undefined, - resolveProviderSyntheticAuthWithPlugin: () => undefined, - }; -}); +vi.mock("../plugins/provider-runtime.js", () => ({ + applyProviderConfigDefaultsWithPlugin: (config: OpenClawConfig) => config, + applyProviderNativeStreamingUsageCompatWithPlugin: () => undefined, + normalizeProviderConfigWithPlugin: () => undefined, + resetProviderRuntimeHookCacheForTest: () => undefined, + resolveProviderConfigApiKeyWithPlugin: () => undefined, + resolveProviderSyntheticAuthWithPlugin: () => undefined, +})); vi.mock("./models-config.providers.js", async () => { const actual = await vi.importActual( diff --git a/src/agents/pi-bundle-mcp-materialize.ts b/src/agents/pi-bundle-mcp-materialize.ts index 8cf6eccf077..ab381ef1ca3 100644 --- a/src/agents/pi-bundle-mcp-materialize.ts +++ b/src/agents/pi-bundle-mcp-materialize.ts @@ -9,7 +9,6 @@ import { normalizeReservedToolNames, TOOL_NAME_SEPARATOR, } from "./pi-bundle-mcp-names.js"; -import { createSessionMcpRuntime } from "./pi-bundle-mcp-runtime.js"; import type { BundleMcpToolRuntime, SessionMcpRuntime } from "./pi-bundle-mcp-types.js"; function toAgentToolResult(params: { @@ -131,6 +130,7 @@ export async function createBundleMcpToolRuntime(params: { cfg?: OpenClawConfig; reservedToolNames?: Iterable; }): Promise { + const { createSessionMcpRuntime } = await import("./pi-bundle-mcp-runtime.js"); const runtime = createSessionMcpRuntime({ sessionId: `bundle-mcp:${crypto.randomUUID()}`, workspaceDir: params.workspaceDir, diff --git a/src/agents/pi-bundle-mcp-tools.materialize.test.ts b/src/agents/pi-bundle-mcp-tools.materialize.test.ts index e40d9b35ca5..855167daa7d 100644 --- a/src/agents/pi-bundle-mcp-tools.materialize.test.ts +++ b/src/agents/pi-bundle-mcp-tools.materialize.test.ts @@ -1,15 +1,15 @@ import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { + createBundleMcpToolRuntime, + materializeBundleMcpToolsForRun, +} from "./pi-bundle-mcp-materialize.js"; import { cleanupBundleMcpHarness, makeTempDir, startSseProbeServer, writeBundleProbeMcpServer, } from "./pi-bundle-mcp-test-harness.js"; -import { - createBundleMcpToolRuntime, - materializeBundleMcpToolsForRun, -} from "./pi-bundle-mcp-tools.js"; import type { McpCatalogTool } from "./pi-bundle-mcp-types.js"; import type { SessionMcpRuntime } from "./pi-bundle-mcp-types.js"; diff --git a/src/agents/session-write-lock.test.ts b/src/agents/session-write-lock.test.ts index c7addeb2db7..f42c6b97034 100644 --- a/src/agents/session-write-lock.test.ts +++ b/src/agents/session-write-lock.test.ts @@ -205,7 +205,7 @@ describe("acquireSessionWriteLock", () => { it("watchdog releases stale in-process locks", async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-lock-")); - const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + const stderrSpy = vi.spyOn(process.stderr, "write").mockImplementation(() => true); try { const sessionFile = path.join(root, "session.jsonl"); const lockPath = `${sessionFile}.lock`; @@ -229,7 +229,7 @@ describe("acquireSessionWriteLock", () => { secondLock: lockB, }); } finally { - warnSpy.mockRestore(); + stderrSpy.mockRestore(); await fs.rm(root, { recursive: true, force: true }); } });