From bc037dfe011291556cedd1867778b0bda054ceec Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 18:18:29 +0000 Subject: [PATCH] refactor(test): dedupe provider env setup in model config tests --- src/agents/model-scan.e2e.test.ts | 10 ++-- ...thub-copilot-provider-token-is.e2e.test.ts | 31 ++++++------ ...t-baseurl-token-exchange-fails.e2e.test.ts | 22 ++++----- .../models-config.providers.nvidia.test.ts | 47 +++++-------------- ...odels-config.providers.qianfan.e2e.test.ts | 11 ++--- 5 files changed, 43 insertions(+), 78 deletions(-) diff --git a/src/agents/model-scan.e2e.test.ts b/src/agents/model-scan.e2e.test.ts index 87c457445..d037e8023 100644 --- a/src/agents/model-scan.e2e.test.ts +++ b/src/agents/model-scan.e2e.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { captureEnv } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { withFetchPreconnect } from "../test-utils/fetch-mock.js"; import { scanOpenRouterModels } from "./model-scan.js"; @@ -70,9 +70,7 @@ describe("scanOpenRouterModels", () => { it("requires an API key when probing", async () => { const fetchImpl = createFetchFixture({ data: [] }); - const envSnapshot = captureEnv(["OPENROUTER_API_KEY"]); - try { - delete process.env.OPENROUTER_API_KEY; + await withEnvAsync({ OPENROUTER_API_KEY: undefined }, async () => { await expect( scanOpenRouterModels({ fetchImpl, @@ -80,8 +78,6 @@ describe("scanOpenRouterModels", () => { apiKey: "", }), ).rejects.toThrow(/Missing OpenRouter API key/); - } finally { - envSnapshot.restore(); - } + }); }); }); diff --git a/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts b/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts index 77b4c63e9..a710d3ad9 100644 --- a/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts +++ b/src/agents/models-config.auto-injects-github-copilot-provider-token-is.e2e.test.ts @@ -1,7 +1,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import { describe, expect, it } from "vitest"; -import { captureEnv } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { installModelsConfigTestHooks, mockCopilotTokenExchangeSuccess, @@ -32,21 +32,24 @@ describe("models-config", () => { it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => { await withTempHome(async () => { - const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"]); - process.env.COPILOT_GITHUB_TOKEN = "copilot-token"; - process.env.GH_TOKEN = "gh-token"; - process.env.GITHUB_TOKEN = "github-token"; + await withEnvAsync( + { + COPILOT_GITHUB_TOKEN: "copilot-token", + GH_TOKEN: "gh-token", + GITHUB_TOKEN: "github-token", + }, + async () => { + const fetchMock = mockCopilotTokenExchangeSuccess(); - const fetchMock = mockCopilotTokenExchangeSuccess(); + await ensureOpenClawModelsJson({ models: { providers: {} } }); - try { - await ensureOpenClawModelsJson({ models: { providers: {} } }); - - const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record }]; - expect(opts?.headers?.Authorization).toBe("Bearer copilot-token"); - } finally { - envSnapshot.restore(); - } + const [, opts] = fetchMock.mock.calls[0] as [ + string, + { headers?: Record }, + ]; + expect(opts?.headers?.Authorization).toBe("Bearer copilot-token"); + }, + ); }); }); }); diff --git a/src/agents/models-config.falls-back-default-baseurl-token-exchange-fails.e2e.test.ts b/src/agents/models-config.falls-back-default-baseurl-token-exchange-fails.e2e.test.ts index a7b123de1..f0c7493fe 100644 --- a/src/agents/models-config.falls-back-default-baseurl-token-exchange-fails.e2e.test.ts +++ b/src/agents/models-config.falls-back-default-baseurl-token-exchange-fails.e2e.test.ts @@ -2,7 +2,7 @@ import fs from "node:fs/promises"; import path from "node:path"; import { describe, expect, it, vi } from "vitest"; import { DEFAULT_COPILOT_API_BASE_URL } from "../providers/github-copilot-token.js"; -import { captureEnv } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { installModelsConfigTestHooks, mockCopilotTokenExchangeSuccess, @@ -16,16 +16,14 @@ installModelsConfigTestHooks({ restoreFetch: true }); describe("models-config", () => { it("falls back to default baseUrl when token exchange fails", async () => { await withTempHome(async () => { - const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]); - process.env.COPILOT_GITHUB_TOKEN = "gh-token"; - const fetchMock = vi.fn().mockResolvedValue({ - ok: false, - status: 500, - json: async () => ({ message: "boom" }), - }); - globalThis.fetch = fetchMock as unknown as typeof fetch; + await withEnvAsync({ COPILOT_GITHUB_TOKEN: "gh-token" }, async () => { + const fetchMock = vi.fn().mockResolvedValue({ + ok: false, + status: 500, + json: async () => ({ message: "boom" }), + }); + globalThis.fetch = fetchMock as unknown as typeof fetch; - try { await ensureOpenClawModelsJson({ models: { providers: {} } }); const agentDir = path.join(process.env.HOME ?? "", ".openclaw", "agents", "main", "agent"); @@ -35,9 +33,7 @@ describe("models-config", () => { }; expect(parsed.providers["github-copilot"]?.baseUrl).toBe(DEFAULT_COPILOT_API_BASE_URL); - } finally { - envSnapshot.restore(); - } + }); }); }); diff --git a/src/agents/models-config.providers.nvidia.test.ts b/src/agents/models-config.providers.nvidia.test.ts index 3a2f86e98..17025cb86 100644 --- a/src/agents/models-config.providers.nvidia.test.ts +++ b/src/agents/models-config.providers.nvidia.test.ts @@ -2,31 +2,23 @@ import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { captureEnv } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { resolveApiKeyForProvider } from "./model-auth.js"; import { buildNvidiaProvider, resolveImplicitProviders } from "./models-config.providers.js"; describe("NVIDIA provider", () => { it("should include nvidia when NVIDIA_API_KEY is configured", async () => { const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); - const envSnapshot = captureEnv(["NVIDIA_API_KEY"]); - process.env.NVIDIA_API_KEY = "test-key"; - - try { + await withEnvAsync({ NVIDIA_API_KEY: "test-key" }, async () => { const providers = await resolveImplicitProviders({ agentDir }); expect(providers?.nvidia).toBeDefined(); expect(providers?.nvidia?.models?.length).toBeGreaterThan(0); - } finally { - envSnapshot.restore(); - } + }); }); it("resolves the nvidia api key value from env", async () => { const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); - const envSnapshot = captureEnv(["NVIDIA_API_KEY"]); - process.env.NVIDIA_API_KEY = "nvidia-test-api-key"; - - try { + await withEnvAsync({ NVIDIA_API_KEY: "nvidia-test-api-key" }, async () => { const auth = await resolveApiKeyForProvider({ provider: "nvidia", agentDir, @@ -35,9 +27,7 @@ describe("NVIDIA provider", () => { expect(auth.apiKey).toBe("nvidia-test-api-key"); expect(auth.mode).toBe("api-key"); expect(auth.source).toContain("NVIDIA_API_KEY"); - } finally { - envSnapshot.restore(); - } + }); }); it("should build nvidia provider with correct configuration", () => { @@ -60,40 +50,27 @@ describe("NVIDIA provider", () => { describe("MiniMax implicit provider (#15275)", () => { it("should use anthropic-messages API for API-key provider", async () => { const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); - const envSnapshot = captureEnv(["MINIMAX_API_KEY"]); - process.env.MINIMAX_API_KEY = "test-key"; - - try { + await withEnvAsync({ MINIMAX_API_KEY: "test-key" }, async () => { const providers = await resolveImplicitProviders({ agentDir }); expect(providers?.minimax).toBeDefined(); expect(providers?.minimax?.api).toBe("anthropic-messages"); expect(providers?.minimax?.baseUrl).toBe("https://api.minimax.io/anthropic"); - } finally { - envSnapshot.restore(); - } + }); }); }); describe("vLLM provider", () => { it("should not include vllm when no API key is configured", async () => { const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); - const envSnapshot = captureEnv(["VLLM_API_KEY"]); - delete process.env.VLLM_API_KEY; - - try { + await withEnvAsync({ VLLM_API_KEY: undefined }, async () => { const providers = await resolveImplicitProviders({ agentDir }); expect(providers?.vllm).toBeUndefined(); - } finally { - envSnapshot.restore(); - } + }); }); it("should include vllm when VLLM_API_KEY is set", async () => { const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); - const envSnapshot = captureEnv(["VLLM_API_KEY"]); - process.env.VLLM_API_KEY = "test-key"; - - try { + await withEnvAsync({ VLLM_API_KEY: "test-key" }, async () => { const providers = await resolveImplicitProviders({ agentDir }); expect(providers?.vllm).toBeDefined(); @@ -103,8 +80,6 @@ describe("vLLM provider", () => { // Note: discovery is disabled in test environments (VITEST check) expect(providers?.vllm?.models).toEqual([]); - } finally { - envSnapshot.restore(); - } + }); }); }); diff --git a/src/agents/models-config.providers.qianfan.e2e.test.ts b/src/agents/models-config.providers.qianfan.e2e.test.ts index 06f477874..081b0aeb7 100644 --- a/src/agents/models-config.providers.qianfan.e2e.test.ts +++ b/src/agents/models-config.providers.qianfan.e2e.test.ts @@ -2,21 +2,16 @@ import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; -import { captureEnv } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { resolveImplicitProviders } from "./models-config.providers.js"; describe("Qianfan provider", () => { it("should include qianfan when QIANFAN_API_KEY is configured", async () => { const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); - const envSnapshot = captureEnv(["QIANFAN_API_KEY"]); - process.env.QIANFAN_API_KEY = "test-key"; - - try { + await withEnvAsync({ QIANFAN_API_KEY: "test-key" }, async () => { const providers = await resolveImplicitProviders({ agentDir }); expect(providers?.qianfan).toBeDefined(); expect(providers?.qianfan?.apiKey).toBe("QIANFAN_API_KEY"); - } finally { - envSnapshot.restore(); - } + }); }); });