Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(routing): clear every provider key env var in routing-rules tests

Jack Rudenko77ad1ebf325e
diff --git a/packages/cli/src/providers/routing-rules.test.ts b/packages/cli/src/providers/routing-rules.test.ts
index 36bd483b..3f8c4bc2 100644
--- a/packages/cli/src/providers/routing-rules.test.ts
+++ b/packages/cli/src/providers/routing-rules.test.ts
@@ -25,6 +25,7 @@ import { credentials } from "../auth/credentials/authority.js";
import { __resetSniffForTests } from "../auth/credentials/op-source.js";
import { getLogFilePath, initLogger, setDiagOutput } from "../logger.js";
import type { RoutingRules } from "../profile-config.js";
+import { clearProviderCredentialEnv } from "../test-helpers/provider-credential-env.js";
import { type DiskCacheV3, type SlimModelEntry, writeAllModelsCache } from "./all-models-cache.js";
import { DISPLAY_NAMES } from "./auto-route.js";
import { _resetCatalogClient, _setCatalogEntriesForTest } from "./catalog-client.js";
@@ -632,34 +633,10 @@ describe("normalizeGlmSlug", () => {
// route() — credential-aware single entry point
// ---------------------------------------------------------------------------
-const ENV_KEYS_TO_CLEAR = [
- "ANTHROPIC_API_KEY",
- "ANTHROPIC_AUTH_TOKEN",
- "OPENROUTER_API_KEY",
- "OPENAI_API_KEY",
- "OPENAI_CODEX_API_KEY",
- "GEMINI_API_KEY",
- "MOONSHOT_API_KEY",
- "KIMI_API_KEY",
- "KIMI_CODING_API_KEY",
- "QWEN_TOKEN_PLAN_API_KEY",
- "QWEN_CODING_PLAN_API_KEY",
- "DASHSCOPE_API_KEY",
- "MINIMAX_API_KEY",
- "MINIMAX_CODING_API_KEY",
- "ZHIPU_API_KEY",
- "GLM_API_KEY",
- "GLM_CODING_API_KEY",
- "DEEPSEEK_API_KEY",
- "XAI_API_KEY",
- "ZAI_API_KEY",
- "OLLAMA_API_KEY",
- "OPENCODE_API_KEY",
- "OPENCODE_GO_API_KEY",
- "WINDSURF_API_KEY",
-];
-
-const savedEnv: Record<string, string | undefined> = {};
+// Every provider credential and base-URL variable is cleared before each test, derived
+// from the provider definitions (`clearProviderCredentialEnv`). A hand list here missed
+// POE_API_KEY, and two tests routed to Poe on a machine that had it set (D4).
+let restoreProviderEnv: () => void = () => {};
/**
* Route `modelId` against a catalog that publishes it through `providers` only. Without a
@@ -696,11 +673,8 @@ describe("route()", () => {
process.env.CLAUDISH_DISABLE_OP = "1";
__resetSniffForTests();
credentials.invalidate();
- // Snapshot and clear credential env vars so each test starts clean.
- for (const key of ENV_KEYS_TO_CLEAR) {
- savedEnv[key] = process.env[key];
- delete process.env[key];
- }
+ // Snapshot and clear every provider credential and base-URL variable.
+ restoreProviderEnv = clearProviderCredentialEnv();
});
afterEach(() => {
@@ -711,14 +685,8 @@ describe("route()", () => {
}
delete process.env.CLAUDISH_DISABLE_OP;
__resetSniffForTests();
- // Restore env vars (preserves the host's actual config for other tests).
- for (const key of ENV_KEYS_TO_CLEAR) {
- if (savedEnv[key] === undefined) {
- delete process.env[key];
- } else {
- process.env[key] = savedEnv[key];
- }
- }
+ // Restore them (preserves the host environment for other tests).
+ restoreProviderEnv();
credentials.invalidate();
});
@@ -1107,10 +1075,7 @@ describe("route() with defaultProvider", () => {
process.env.CLAUDISH_DISABLE_OP = "1";
__resetSniffForTests();
credentials.invalidate();
- for (const key of ENV_KEYS_TO_CLEAR) {
- savedEnv[key] = process.env[key];
- delete process.env[key];
- }
+ restoreProviderEnv = clearProviderCredentialEnv();
});
afterEach(() => {
@@ -1121,13 +1086,7 @@ describe("route() with defaultProvider", () => {
}
delete process.env.CLAUDISH_DISABLE_OP;
__resetSniffForTests();
- for (const key of ENV_KEYS_TO_CLEAR) {
- if (savedEnv[key] === undefined) {
- delete process.env[key];
- } else {
- process.env[key] = savedEnv[key];
- }
- }
+ restoreProviderEnv();
credentials.invalidate();
});
diff --git a/packages/cli/src/test-helpers/provider-credential-env.ts b/packages/cli/src/test-helpers/provider-credential-env.ts
new file mode 100644
index 00000000..614876ae
--- /dev/null
+++ b/packages/cli/src/test-helpers/provider-credential-env.ts
@@ -0,0 +1,84 @@
+/**
+ * Every environment variable a provider credential (or a provider base URL) is read
+ * from, DERIVED from the provider definitions, never typed out.
+ *
+ * Why: `routing-rules.test.ts` cleared a hand list of 24 key names. `POE_API_KEY` was
+ * not on it, so with that key in the developer's shell two tests routed to Poe and
+ * failed (model-failures-2026-10-21.md, D4). A hand list reopens that hole every time a
+ * provider lands; this derivation covers it the day the definition does.
+ *
+ * Sources:
+ * - `providerKeyEnvVars()` (`dotenv-allowlist.ts`): every `apiKeyEnvVar` and
+ * `apiKeyAliases` in `BUILTIN_PROVIDERS` and `PREDEFINED_ENDPOINTS`;
+ * - the credential names no definition declares (`UNDECLARED_CREDENTIAL_VARS`, each
+ * with its reader), taken from the reader's own constant where one exists;
+ * - every `CUSTOM_<NAME>_KEY` present in the environment (`isCustomEndpointKeyEnvVar`):
+ * user endpoints live in the config file, so their names are matched by shape.
+ *
+ * Base URLs are not credentials, but `localBaseUrl` and the transports read them, so
+ * `clearProviderCredentialEnv` clears `providerBaseUrlEnvVars()` too.
+ */
+import { VERTEX_PROJECT_ENV_VARS } from "../auth/vertex-auth.js";
+import { providerKeyEnvVars } from "../dotenv-allowlist.js";
+import { DEVIN_API_KEY_ENV } from "../providers/devin/devin-credentials.js";
+import { isCustomEndpointKeyEnvVar } from "../providers/endpoint-key-env.js";
+import { PREDEFINED_ENDPOINTS } from "../providers/predefined-catalog.js";
+import { BUILTIN_PROVIDERS } from "../providers/provider-definitions.js";
+
+type Env = Record<string, string | undefined>;
+
+/** Credential variables that no provider definition declares (see the module comment). */
+const UNDECLARED_CREDENTIAL_VARS = [
+ // native-anthropic declares no apiKeyEnvVar (native-anthropic-credential.ts)
+ "ANTHROPIC_API_KEY",
+ "ANTHROPIC_AUTH_TOKEN",
+ // devin's apiKeyEnvVar MUST stay "" (CLAUDE.md); its source reads this name directly
+ DEVIN_API_KEY_ENV,
+ // read by the advisor's Gemini call (native-handler-advisor.ts)
+ "GOOGLE_API_KEY",
+ // vertex: ADC file pointer and project (vertex-auth.ts)
+ "GOOGLE_APPLICATION_CREDENTIALS",
+ ...VERTEX_PROJECT_ENV_VARS,
+];
+
+/** Every environment variable a credential source reads, derived; never hand-listed. */
+export function providerCredentialEnvVars(env: Env = process.env): string[] {
+ const names = new Set<string>(providerKeyEnvVars());
+ for (const name of UNDECLARED_CREDENTIAL_VARS) names.add(name);
+ for (const name of Object.keys(env)) {
+ if (isCustomEndpointKeyEnvVar(name)) names.add(name);
+ }
+ return [...names].sort();
+}
+
+/** Every base-URL override a builtin provider or bundled endpoint declares. */
+export function providerBaseUrlEnvVars(): string[] {
+ const names = new Set<string>();
+ const sources: readonly { baseUrlEnvVars?: readonly string[] }[] = [
+ ...BUILTIN_PROVIDERS,
+ ...PREDEFINED_ENDPOINTS,
+ ];
+ for (const def of sources) {
+ for (const name of def.baseUrlEnvVars ?? []) names.add(name);
+ }
+ return [...names].sort();
+}
+
+/**
+ * Snapshot and delete every provider credential and base-URL variable from
+ * `process.env`. Returns the function that restores the snapshot (call it in afterEach).
+ */
+export function clearProviderCredentialEnv(): () => void {
+ const names = [...new Set([...providerCredentialEnvVars(), ...providerBaseUrlEnvVars()])];
+ const saved = new Map<string, string | undefined>();
+ for (const name of names) {
+ saved.set(name, process.env[name]);
+ delete process.env[name];
+ }
+ return () => {
+ for (const [name, value] of saved) {
+ if (value === undefined) delete process.env[name];
+ else process.env[name] = value;
+ }
+ };
+}