From 5688fe10a777e7e04d98392c69451b60db69a69a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E9=98=BF=E6=B1=9F=28Relakkes?= =?UTF-8?q?=29?= Date: Thu, 4 Jun 2026 20:12:54 +0800 Subject: [PATCH] test(e2e): isolate ANTHROPIC_*MODEL env from the models-API fixtures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The models API derives its model list from ANTHROPIC_MODEL and the ANTHROPIC_DEFAULT_{HAIKU,SONNET,OPUS}_MODEL env vars. A developer who exports these for a custom provider (e.g. MiniMax) leaked them into the no-provider fixture: the four collapsed to one model, the default model became the custom one, and switched-model names fell back to the raw id — failing five "available models" / "default model" assertions on that machine while passing on clean CI. Clear those four vars in the e2e setup and restore them in teardown, matching the existing CLAUDE_CONFIG_DIR / CLAUDE_CLI_PATH isolation. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../__tests__/e2e/business-flow.test.ts | 19 +++++++++++++++++++ src/server/__tests__/e2e/full-flow.test.ts | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/server/__tests__/e2e/business-flow.test.ts b/src/server/__tests__/e2e/business-flow.test.ts index 636ac073..b26b6ffa 100644 --- a/src/server/__tests__/e2e/business-flow.test.ts +++ b/src/server/__tests__/e2e/business-flow.test.ts @@ -20,7 +20,25 @@ const originalCliPath = process.env.CLAUDE_CLI_PATH const originalDisableTerminalShellEnv = process.env.CC_HAHA_DISABLE_TERMINAL_SHELL_ENV const mockSdkCliPath = fileURLToPath(new URL('../fixtures/mock-sdk-cli.ts', import.meta.url)) +// The models API derives its model list from these env vars (see +// src/server/api/models.ts getEnvConfiguredAnthropicModels). A developer who +// exports them for a custom provider would otherwise leak them into the +// no-provider fixture and break the default-model assertions. Isolate them. +const MODEL_ENV_KEYS = [ + 'ANTHROPIC_MODEL', + 'ANTHROPIC_DEFAULT_HAIKU_MODEL', + 'ANTHROPIC_DEFAULT_SONNET_MODEL', + 'ANTHROPIC_DEFAULT_OPUS_MODEL', +] as const +const originalModelEnv = Object.fromEntries( + MODEL_ENV_KEYS.map((key) => [key, process.env[key]]), +) as Record<(typeof MODEL_ENV_KEYS)[number], string | undefined> + function restoreEnv() { + for (const key of MODEL_ENV_KEYS) { + if (originalModelEnv[key] !== undefined) process.env[key] = originalModelEnv[key] + else delete process.env[key] + } if (originalConfigDir !== undefined) { process.env.CLAUDE_CONFIG_DIR = originalConfigDir } else { @@ -47,6 +65,7 @@ async function startTestServer() { process.env.CLAUDE_CONFIG_DIR = tmpDir process.env.CLAUDE_CLI_PATH = mockSdkCliPath process.env.CC_HAHA_DISABLE_TERMINAL_SHELL_ENV = '1' + for (const key of MODEL_ENV_KEYS) delete process.env[key] await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true }) await fs.mkdir(path.join(tmpDir, 'agents'), { recursive: true }) diff --git a/src/server/__tests__/e2e/full-flow.test.ts b/src/server/__tests__/e2e/full-flow.test.ts index 89e4200e..7d48965a 100644 --- a/src/server/__tests__/e2e/full-flow.test.ts +++ b/src/server/__tests__/e2e/full-flow.test.ts @@ -18,7 +18,25 @@ const originalCliPath = process.env.CLAUDE_CLI_PATH const originalDisableTerminalShellEnv = process.env.CC_HAHA_DISABLE_TERMINAL_SHELL_ENV const mockSdkCliPath = fileURLToPath(new URL('../fixtures/mock-sdk-cli.ts', import.meta.url)) +// The models API derives its model list from these env vars (see +// src/server/api/models.ts getEnvConfiguredAnthropicModels). A developer who +// exports them for a custom provider would otherwise leak them into the +// no-provider fixture and break the default-model assertions. Isolate them. +const MODEL_ENV_KEYS = [ + 'ANTHROPIC_MODEL', + 'ANTHROPIC_DEFAULT_HAIKU_MODEL', + 'ANTHROPIC_DEFAULT_SONNET_MODEL', + 'ANTHROPIC_DEFAULT_OPUS_MODEL', +] as const +const originalModelEnv = Object.fromEntries( + MODEL_ENV_KEYS.map((key) => [key, process.env[key]]), +) as Record<(typeof MODEL_ENV_KEYS)[number], string | undefined> + function restoreEnv() { + for (const key of MODEL_ENV_KEYS) { + if (originalModelEnv[key] !== undefined) process.env[key] = originalModelEnv[key] + else delete process.env[key] + } if (originalConfigDir !== undefined) { process.env.CLAUDE_CONFIG_DIR = originalConfigDir } else { @@ -46,6 +64,7 @@ async function startTestServer() { process.env.CLAUDE_CONFIG_DIR = tmpDir process.env.CLAUDE_CLI_PATH = mockSdkCliPath process.env.CC_HAHA_DISABLE_TERMINAL_SHELL_ENV = '1' + for (const key of MODEL_ENV_KEYS) delete process.env[key] // Create required directories await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })