diff --git a/src/secrets/runtime.auth.integration.test.ts b/src/secrets/runtime.auth.integration.test.ts index 922eaa7b02e..67cfb38fb1b 100644 --- a/src/secrets/runtime.auth.integration.test.ts +++ b/src/secrets/runtime.auth.integration.test.ts @@ -3,27 +3,24 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ensureAuthProfileStore } from "../agents/auth-profiles.js"; -import { - clearConfigCache, - clearRuntimeConfigSnapshot, - loadConfig, - writeConfigFile, -} from "../config/config.js"; +import { loadConfig, writeConfigFile } from "../config/config.js"; import { withTempHome } from "../config/home-env.test-harness.js"; -import { captureEnv, withEnvAsync } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { asConfig, + beginSecretsRuntimeIsolationForTest, createOpenAIFileRuntimeConfig, createOpenAIFileRuntimeFixture, EMPTY_LOADABLE_PLUGIN_ORIGINS, + endSecretsRuntimeIsolationForTest, expectResolvedOpenAIRuntime, loadAuthStoreWithProfiles, OPENAI_ENV_KEY_REF, OPENAI_FILE_KEY_REF, + type SecretsRuntimeEnvSnapshot, } from "./runtime.integration.test-helpers.js"; import { activateSecretsRuntimeSnapshot, - clearSecretsRuntimeSnapshot, getActiveSecretsRuntimeSnapshot, prepareSecretsRuntimeSnapshot, } from "./runtime.js"; @@ -31,25 +28,14 @@ import { vi.unmock("../version.js"); describe("secrets runtime snapshot auth integration", () => { - let envSnapshot: ReturnType; + let envSnapshot: SecretsRuntimeEnvSnapshot; beforeEach(() => { - envSnapshot = captureEnv([ - "OPENCLAW_BUNDLED_PLUGINS_DIR", - "OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE", - "OPENCLAW_VERSION", - ]); - delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; - process.env.OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE = "1"; - delete process.env.OPENCLAW_VERSION; + envSnapshot = beginSecretsRuntimeIsolationForTest(); }); afterEach(() => { - vi.restoreAllMocks(); - envSnapshot.restore(); - clearSecretsRuntimeSnapshot(); - clearRuntimeConfigSnapshot(); - clearConfigCache(); + endSecretsRuntimeIsolationForTest(envSnapshot); }); it("activates runtime snapshots for loadConfig and ensureAuthProfileStore", async () => { diff --git a/src/secrets/runtime.gateway-auth.integration.test.ts b/src/secrets/runtime.gateway-auth.integration.test.ts index b8a959e2c2c..13366511c98 100644 --- a/src/secrets/runtime.gateway-auth.integration.test.ts +++ b/src/secrets/runtime.gateway-auth.integration.test.ts @@ -2,23 +2,20 @@ import fs from "node:fs/promises"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../config/config.js"; -import { - clearConfigCache, - clearRuntimeConfigSnapshot, - loadConfig, - writeConfigFile, -} from "../config/config.js"; +import { loadConfig, writeConfigFile } from "../config/config.js"; import { withTempHome } from "../config/home-env.test-harness.js"; -import { captureEnv, withEnvAsync } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { asConfig, + beginSecretsRuntimeIsolationForTest, EMPTY_LOADABLE_PLUGIN_ORIGINS, + endSecretsRuntimeIsolationForTest, loadAuthStoreWithProfiles, SECRETS_RUNTIME_INTEGRATION_TIMEOUT_MS, + type SecretsRuntimeEnvSnapshot, } from "./runtime.integration.test-helpers.js"; import { activateSecretsRuntimeSnapshot, - clearSecretsRuntimeSnapshot, getActiveSecretsRuntimeSnapshot, prepareSecretsRuntimeSnapshot, } from "./runtime.js"; @@ -26,25 +23,14 @@ import { vi.unmock("../version.js"); describe("secrets runtime snapshot gateway-auth integration", () => { - let envSnapshot: ReturnType; + let envSnapshot: SecretsRuntimeEnvSnapshot; beforeEach(() => { - envSnapshot = captureEnv([ - "OPENCLAW_BUNDLED_PLUGINS_DIR", - "OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE", - "OPENCLAW_VERSION", - ]); - delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; - process.env.OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE = "1"; - delete process.env.OPENCLAW_VERSION; + envSnapshot = beginSecretsRuntimeIsolationForTest(); }); afterEach(() => { - vi.restoreAllMocks(); - envSnapshot.restore(); - clearSecretsRuntimeSnapshot(); - clearRuntimeConfigSnapshot(); - clearConfigCache(); + endSecretsRuntimeIsolationForTest(envSnapshot); }); it("fails fast at startup when gateway auth SecretRef is active and unresolved", async () => { diff --git a/src/secrets/runtime.integration.test-helpers.ts b/src/secrets/runtime.integration.test-helpers.ts index 911003d7807..ee2dd88bcd7 100644 --- a/src/secrets/runtime.integration.test-helpers.ts +++ b/src/secrets/runtime.integration.test-helpers.ts @@ -1,10 +1,17 @@ import fs from "node:fs/promises"; import path from "node:path"; -import { expect } from "vitest"; +import { expect, vi } from "vitest"; import { ensureAuthProfileStore, type AuthProfileStore } from "../agents/auth-profiles.js"; import type { OpenClawConfig } from "../config/config.js"; -import { loadConfig } from "../config/config.js"; +import { clearConfigCache, clearRuntimeConfigSnapshot, loadConfig } from "../config/config.js"; +import { clearPluginDiscoveryCache } from "../plugins/discovery.js"; +import { clearPluginLoaderCache } from "../plugins/loader.js"; +import { clearPluginManifestRegistryCache } from "../plugins/manifest-registry.js"; import type { PluginOrigin } from "../plugins/types.js"; +import { __testing as webFetchProvidersTesting } from "../plugins/web-fetch-providers.runtime.js"; +import { __testing as webSearchProvidersTesting } from "../plugins/web-search-providers.runtime.js"; +import { captureEnv } from "../test-utils/env.js"; +import { clearSecretsRuntimeSnapshot } from "./runtime.js"; export const OPENAI_ENV_KEY_REF = { source: "env", @@ -20,6 +27,7 @@ export const OPENAI_FILE_KEY_REF = { export const SECRETS_RUNTIME_INTEGRATION_TIMEOUT_MS = 300_000; export const EMPTY_LOADABLE_PLUGIN_ORIGINS: ReadonlyMap = new Map(); +export type SecretsRuntimeEnvSnapshot = ReturnType; const allowInsecureTempSecretFile = process.platform === "win32"; @@ -106,3 +114,29 @@ export function expectResolvedOpenAIRuntime(agentDir: string) { key: "sk-file-runtime", }); } + +export function beginSecretsRuntimeIsolationForTest(): SecretsRuntimeEnvSnapshot { + const envSnapshot = captureEnv([ + "OPENCLAW_BUNDLED_PLUGINS_DIR", + "OPENCLAW_DISABLE_BUNDLED_PLUGINS", + "OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE", + "OPENCLAW_VERSION", + ]); + delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR; + process.env.OPENCLAW_DISABLE_PLUGIN_DISCOVERY_CACHE = "1"; + delete process.env.OPENCLAW_VERSION; + return envSnapshot; +} + +export function endSecretsRuntimeIsolationForTest(envSnapshot: SecretsRuntimeEnvSnapshot) { + vi.restoreAllMocks(); + envSnapshot.restore(); + clearSecretsRuntimeSnapshot(); + clearRuntimeConfigSnapshot(); + clearConfigCache(); + clearPluginLoaderCache(); + clearPluginDiscoveryCache(); + clearPluginManifestRegistryCache(); + webSearchProvidersTesting.resetWebSearchProviderSnapshotCacheForTests(); + webFetchProvidersTesting.resetWebFetchProviderSnapshotCacheForTests(); +}