refactor(test): dedupe provider env setup in model config tests

This commit is contained in:
Peter Steinberger
2026-02-21 18:18:29 +00:00
parent c41d1070b7
commit bc037dfe01
5 changed files with 43 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"; 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 { withFetchPreconnect } from "../test-utils/fetch-mock.js";
import { scanOpenRouterModels } from "./model-scan.js"; import { scanOpenRouterModels } from "./model-scan.js";
@@ -70,9 +70,7 @@ describe("scanOpenRouterModels", () => {
it("requires an API key when probing", async () => { it("requires an API key when probing", async () => {
const fetchImpl = createFetchFixture({ data: [] }); const fetchImpl = createFetchFixture({ data: [] });
const envSnapshot = captureEnv(["OPENROUTER_API_KEY"]); await withEnvAsync({ OPENROUTER_API_KEY: undefined }, async () => {
try {
delete process.env.OPENROUTER_API_KEY;
await expect( await expect(
scanOpenRouterModels({ scanOpenRouterModels({
fetchImpl, fetchImpl,
@@ -80,8 +78,6 @@ describe("scanOpenRouterModels", () => {
apiKey: "", apiKey: "",
}), }),
).rejects.toThrow(/Missing OpenRouter API key/); ).rejects.toThrow(/Missing OpenRouter API key/);
} finally { });
envSnapshot.restore();
}
}); });
}); });

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js"; import { withEnvAsync } from "../test-utils/env.js";
import { import {
installModelsConfigTestHooks, installModelsConfigTestHooks,
mockCopilotTokenExchangeSuccess, mockCopilotTokenExchangeSuccess,
@@ -32,21 +32,24 @@ describe("models-config", () => {
it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => { it("prefers COPILOT_GITHUB_TOKEN over GH_TOKEN and GITHUB_TOKEN", async () => {
await withTempHome(async () => { await withTempHome(async () => {
const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"]); await withEnvAsync(
process.env.COPILOT_GITHUB_TOKEN = "copilot-token"; {
process.env.GH_TOKEN = "gh-token"; COPILOT_GITHUB_TOKEN: "copilot-token",
process.env.GITHUB_TOKEN = "github-token"; GH_TOKEN: "gh-token",
GITHUB_TOKEN: "github-token",
},
async () => {
const fetchMock = mockCopilotTokenExchangeSuccess();
const fetchMock = mockCopilotTokenExchangeSuccess(); await ensureOpenClawModelsJson({ models: { providers: {} } });
try { const [, opts] = fetchMock.mock.calls[0] as [
await ensureOpenClawModelsJson({ models: { providers: {} } }); string,
{ headers?: Record<string, string> },
const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }]; ];
expect(opts?.headers?.Authorization).toBe("Bearer copilot-token"); expect(opts?.headers?.Authorization).toBe("Bearer copilot-token");
} finally { },
envSnapshot.restore(); );
}
}); });
}); });
}); });

View File

@@ -2,7 +2,7 @@ import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { DEFAULT_COPILOT_API_BASE_URL } from "../providers/github-copilot-token.js"; 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 { import {
installModelsConfigTestHooks, installModelsConfigTestHooks,
mockCopilotTokenExchangeSuccess, mockCopilotTokenExchangeSuccess,
@@ -16,16 +16,14 @@ installModelsConfigTestHooks({ restoreFetch: true });
describe("models-config", () => { describe("models-config", () => {
it("falls back to default baseUrl when token exchange fails", async () => { it("falls back to default baseUrl when token exchange fails", async () => {
await withTempHome(async () => { await withTempHome(async () => {
const envSnapshot = captureEnv(["COPILOT_GITHUB_TOKEN"]); await withEnvAsync({ COPILOT_GITHUB_TOKEN: "gh-token" }, async () => {
process.env.COPILOT_GITHUB_TOKEN = "gh-token"; const fetchMock = vi.fn().mockResolvedValue({
const fetchMock = vi.fn().mockResolvedValue({ ok: false,
ok: false, status: 500,
status: 500, json: async () => ({ message: "boom" }),
json: async () => ({ message: "boom" }), });
}); globalThis.fetch = fetchMock as unknown as typeof fetch;
globalThis.fetch = fetchMock as unknown as typeof fetch;
try {
await ensureOpenClawModelsJson({ models: { providers: {} } }); await ensureOpenClawModelsJson({ models: { providers: {} } });
const agentDir = path.join(process.env.HOME ?? "", ".openclaw", "agents", "main", "agent"); 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); expect(parsed.providers["github-copilot"]?.baseUrl).toBe(DEFAULT_COPILOT_API_BASE_URL);
} finally { });
envSnapshot.restore();
}
}); });
}); });

View File

@@ -2,31 +2,23 @@ import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import { join } from "node:path"; import { join } from "node:path";
import { describe, expect, it } from "vitest"; 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 { resolveApiKeyForProvider } from "./model-auth.js";
import { buildNvidiaProvider, resolveImplicitProviders } from "./models-config.providers.js"; import { buildNvidiaProvider, resolveImplicitProviders } from "./models-config.providers.js";
describe("NVIDIA provider", () => { describe("NVIDIA provider", () => {
it("should include nvidia when NVIDIA_API_KEY is configured", async () => { it("should include nvidia when NVIDIA_API_KEY is configured", async () => {
const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-"));
const envSnapshot = captureEnv(["NVIDIA_API_KEY"]); await withEnvAsync({ NVIDIA_API_KEY: "test-key" }, async () => {
process.env.NVIDIA_API_KEY = "test-key";
try {
const providers = await resolveImplicitProviders({ agentDir }); const providers = await resolveImplicitProviders({ agentDir });
expect(providers?.nvidia).toBeDefined(); expect(providers?.nvidia).toBeDefined();
expect(providers?.nvidia?.models?.length).toBeGreaterThan(0); expect(providers?.nvidia?.models?.length).toBeGreaterThan(0);
} finally { });
envSnapshot.restore();
}
}); });
it("resolves the nvidia api key value from env", async () => { it("resolves the nvidia api key value from env", async () => {
const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-"));
const envSnapshot = captureEnv(["NVIDIA_API_KEY"]); await withEnvAsync({ NVIDIA_API_KEY: "nvidia-test-api-key" }, async () => {
process.env.NVIDIA_API_KEY = "nvidia-test-api-key";
try {
const auth = await resolveApiKeyForProvider({ const auth = await resolveApiKeyForProvider({
provider: "nvidia", provider: "nvidia",
agentDir, agentDir,
@@ -35,9 +27,7 @@ describe("NVIDIA provider", () => {
expect(auth.apiKey).toBe("nvidia-test-api-key"); expect(auth.apiKey).toBe("nvidia-test-api-key");
expect(auth.mode).toBe("api-key"); expect(auth.mode).toBe("api-key");
expect(auth.source).toContain("NVIDIA_API_KEY"); expect(auth.source).toContain("NVIDIA_API_KEY");
} finally { });
envSnapshot.restore();
}
}); });
it("should build nvidia provider with correct configuration", () => { it("should build nvidia provider with correct configuration", () => {
@@ -60,40 +50,27 @@ describe("NVIDIA provider", () => {
describe("MiniMax implicit provider (#15275)", () => { describe("MiniMax implicit provider (#15275)", () => {
it("should use anthropic-messages API for API-key provider", async () => { it("should use anthropic-messages API for API-key provider", async () => {
const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-"));
const envSnapshot = captureEnv(["MINIMAX_API_KEY"]); await withEnvAsync({ MINIMAX_API_KEY: "test-key" }, async () => {
process.env.MINIMAX_API_KEY = "test-key";
try {
const providers = await resolveImplicitProviders({ agentDir }); const providers = await resolveImplicitProviders({ agentDir });
expect(providers?.minimax).toBeDefined(); expect(providers?.minimax).toBeDefined();
expect(providers?.minimax?.api).toBe("anthropic-messages"); expect(providers?.minimax?.api).toBe("anthropic-messages");
expect(providers?.minimax?.baseUrl).toBe("https://api.minimax.io/anthropic"); expect(providers?.minimax?.baseUrl).toBe("https://api.minimax.io/anthropic");
} finally { });
envSnapshot.restore();
}
}); });
}); });
describe("vLLM provider", () => { describe("vLLM provider", () => {
it("should not include vllm when no API key is configured", async () => { it("should not include vllm when no API key is configured", async () => {
const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-"));
const envSnapshot = captureEnv(["VLLM_API_KEY"]); await withEnvAsync({ VLLM_API_KEY: undefined }, async () => {
delete process.env.VLLM_API_KEY;
try {
const providers = await resolveImplicitProviders({ agentDir }); const providers = await resolveImplicitProviders({ agentDir });
expect(providers?.vllm).toBeUndefined(); expect(providers?.vllm).toBeUndefined();
} finally { });
envSnapshot.restore();
}
}); });
it("should include vllm when VLLM_API_KEY is set", async () => { it("should include vllm when VLLM_API_KEY is set", async () => {
const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-"));
const envSnapshot = captureEnv(["VLLM_API_KEY"]); await withEnvAsync({ VLLM_API_KEY: "test-key" }, async () => {
process.env.VLLM_API_KEY = "test-key";
try {
const providers = await resolveImplicitProviders({ agentDir }); const providers = await resolveImplicitProviders({ agentDir });
expect(providers?.vllm).toBeDefined(); expect(providers?.vllm).toBeDefined();
@@ -103,8 +80,6 @@ describe("vLLM provider", () => {
// Note: discovery is disabled in test environments (VITEST check) // Note: discovery is disabled in test environments (VITEST check)
expect(providers?.vllm?.models).toEqual([]); expect(providers?.vllm?.models).toEqual([]);
} finally { });
envSnapshot.restore();
}
}); });
}); });

View File

@@ -2,21 +2,16 @@ import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import { join } from "node:path"; import { join } from "node:path";
import { describe, expect, it } from "vitest"; 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"; import { resolveImplicitProviders } from "./models-config.providers.js";
describe("Qianfan provider", () => { describe("Qianfan provider", () => {
it("should include qianfan when QIANFAN_API_KEY is configured", async () => { it("should include qianfan when QIANFAN_API_KEY is configured", async () => {
const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-")); const agentDir = mkdtempSync(join(tmpdir(), "openclaw-test-"));
const envSnapshot = captureEnv(["QIANFAN_API_KEY"]); await withEnvAsync({ QIANFAN_API_KEY: "test-key" }, async () => {
process.env.QIANFAN_API_KEY = "test-key";
try {
const providers = await resolveImplicitProviders({ agentDir }); const providers = await resolveImplicitProviders({ agentDir });
expect(providers?.qianfan).toBeDefined(); expect(providers?.qianfan).toBeDefined();
expect(providers?.qianfan?.apiKey).toBe("QIANFAN_API_KEY"); expect(providers?.qianfan?.apiKey).toBe("QIANFAN_API_KEY");
} finally { });
envSnapshot.restore();
}
}); });
}); });