From c733d5040d1cb33090fc2efb2792b362ae08ebec Mon Sep 17 00:00:00 2001 From: Josh Lehman Date: Mon, 23 Mar 2026 10:59:21 -0700 Subject: [PATCH] Tests: reset provider auth module cache --- ...rovider-usage.auth.normalizes-keys.test.ts | 67 ++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/src/infra/provider-usage.auth.normalizes-keys.test.ts b/src/infra/provider-usage.auth.normalizes-keys.test.ts index 05f3c6fbd8e..f91a986432d 100644 --- a/src/infra/provider-usage.auth.normalizes-keys.test.ts +++ b/src/infra/provider-usage.auth.normalizes-keys.test.ts @@ -7,18 +7,72 @@ import { NON_ENV_SECRETREF_MARKER } from "../agents/model-auth-markers.js"; import type { OpenClawConfig } from "../config/config.js"; import type { ModelDefinitionConfig } from "../config/types.models.js"; +vi.mock("../agents/auth-profiles.js", async () => { + const profiles = await vi.importActual( + "../agents/auth-profiles/profiles.js", + ); + const order = await vi.importActual( + "../agents/auth-profiles/order.js", + ); + const oauth = await vi.importActual( + "../agents/auth-profiles/oauth.js", + ); + + const readStore = (agentDir?: string) => { + if (!agentDir) { + return { version: 1, profiles: {} }; + } + const authPath = path.join(agentDir, "auth-profiles.json"); + try { + const parsed = JSON.parse(nodeFs.readFileSync(authPath, "utf8")) as { + version?: number; + profiles?: Record; + order?: Record; + lastGood?: Record; + usageStats?: Record; + }; + return { + version: parsed.version ?? 1, + profiles: parsed.profiles ?? {}, + ...(parsed.order ? { order: parsed.order } : {}), + ...(parsed.lastGood ? { lastGood: parsed.lastGood } : {}), + ...(parsed.usageStats ? { usageStats: parsed.usageStats } : {}), + }; + } catch { + return { version: 1, profiles: {} }; + } + }; + + return { + clearRuntimeAuthProfileStoreSnapshots: () => {}, + ensureAuthProfileStore: (agentDir?: string) => readStore(agentDir), + dedupeProfileIds: profiles.dedupeProfileIds, + listProfilesForProvider: profiles.listProfilesForProvider, + resolveApiKeyForProfile: oauth.resolveApiKeyForProfile, + resolveAuthProfileOrder: order.resolveAuthProfileOrder, + }; +}); + const resolveProviderUsageAuthWithPluginMock = vi.fn(async (..._args: unknown[]) => null); vi.mock("../plugins/provider-runtime.js", () => ({ resolveProviderUsageAuthWithPlugin: resolveProviderUsageAuthWithPluginMock, })); +vi.mock("../plugins/provider-runtime.ts", () => ({ + resolveProviderUsageAuthWithPlugin: resolveProviderUsageAuthWithPluginMock, +})); + vi.mock("../agents/cli-credentials.js", () => ({ readCodexCliCredentialsCached: () => null, readMiniMaxCliCredentialsCached: () => null, readQwenCliCredentialsCached: () => null, })); +vi.mock("../agents/auth-profiles/external-cli-sync.js", () => ({ + syncExternalCliCredentials: () => false, +})); + let resolveProviderAuths: typeof import("./provider-usage.auth.js").resolveProviderAuths; let clearRuntimeAuthProfileStoreSnapshots: typeof import("../agents/auth-profiles.js").clearRuntimeAuthProfileStoreSnapshots; let clearConfigCache: typeof import("../config/config.js").clearConfigCache; @@ -44,6 +98,7 @@ describe("resolveProviderAuths key normalization", () => { suiteCase = 0; }); +<<<<<<< HEAD beforeEach(async () => { vi.resetModules(); ({ resolveProviderAuths } = await import("./provider-usage.auth.js")); @@ -64,9 +119,15 @@ describe("resolveProviderAuths key normalization", () => { async function withSuiteHome(fn: (home: string) => Promise): Promise { const base = path.join(suiteRoot, `case-${++suiteCase}`); nodeFs.mkdirSync(base, { recursive: true }); - nodeFs.mkdirSync(path.join(base, ".openclaw", "agents", "main", "sessions"), { - recursive: true, - }); + const stateDir = path.join(base, ".openclaw"); + const agentDir = path.join(stateDir, "agents", "main", "agent"); + nodeFs.mkdirSync(path.join(stateDir, "agents", "main", "sessions"), { recursive: true }); + nodeFs.mkdirSync(agentDir, { recursive: true }); + nodeFs.writeFileSync( + path.join(agentDir, "auth-profiles.json"), + `${JSON.stringify({ version: 1, profiles: {} }, null, 2)}\n`, + "utf8", + ); return await fn(base); }