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 { 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();
}
});
});
});

View File

@@ -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<string, string> }];
expect(opts?.headers?.Authorization).toBe("Bearer copilot-token");
} finally {
envSnapshot.restore();
}
const [, opts] = fetchMock.mock.calls[0] as [
string,
{ headers?: Record<string, string> },
];
expect(opts?.headers?.Authorization).toBe("Bearer copilot-token");
},
);
});
});
});

View File

@@ -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();
}
});
});
});

View File

@@ -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();
}
});
});
});

View File

@@ -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();
}
});
});
});