Skip to content

Commit 2a1f569

Browse files
fix: 增强 providers 测试的环境变量隔离,防止 mock 污染
1 parent 6942f53 commit 2a1f569

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/utils/model/__tests__/providers.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, test, beforeEach, afterEach } from 'bun:test'
2+
import { isEnvTruthy } from '../../envUtils.js'
23

34
const { getAPIProvider, isFirstPartyAnthropicBaseUrl } = await import(
45
'../providers'
@@ -12,19 +13,19 @@ describe('getAPIProvider', () => {
1213
'CLAUDE_CODE_USE_FOUNDRY',
1314
'CLAUDE_CODE_USE_OPENAI',
1415
'CLAUDE_CODE_USE_GROK',
16+
'OPENAI_BASE_URL',
17+
'GEMINI_BASE_URL',
1518
] as const
1619
const savedEnv: Record<string, string | undefined> = {}
1720

1821
beforeEach(() => {
19-
// Save and clear environment variables
2022
for (const key of envKeys) {
2123
savedEnv[key] = process.env[key]
2224
delete process.env[key]
2325
}
2426
})
2527

2628
afterEach(() => {
27-
// Restore environment variables
2829
for (const key of envKeys) {
2930
if (savedEnv[key] !== undefined) {
3031
process.env[key] = savedEnv[key]
@@ -67,6 +68,16 @@ describe('getAPIProvider', () => {
6768
expect(getAPIProvider({})).toBe('foundry')
6869
})
6970

71+
test('returns "openai" when CLAUDE_CODE_USE_OPENAI is set', () => {
72+
process.env.CLAUDE_CODE_USE_OPENAI = '1'
73+
expect(getAPIProvider({})).toBe('openai')
74+
})
75+
76+
test('returns "grok" when CLAUDE_CODE_USE_GROK is set', () => {
77+
process.env.CLAUDE_CODE_USE_GROK = '1'
78+
expect(getAPIProvider({})).toBe('grok')
79+
})
80+
7081
test('bedrock takes precedence over gemini', () => {
7182
process.env.CLAUDE_CODE_USE_BEDROCK = '1'
7283
process.env.CLAUDE_CODE_USE_GEMINI = '1'
@@ -88,16 +99,19 @@ describe('getAPIProvider', () => {
8899

89100
test('"true" is truthy', () => {
90101
process.env.CLAUDE_CODE_USE_BEDROCK = 'true'
102+
expect(isEnvTruthy(process.env.CLAUDE_CODE_USE_BEDROCK)).toBe(true)
91103
expect(getAPIProvider({})).toBe('bedrock')
92104
})
93105

94106
test('"0" is not truthy', () => {
95107
process.env.CLAUDE_CODE_USE_BEDROCK = '0'
108+
expect(isEnvTruthy(process.env.CLAUDE_CODE_USE_BEDROCK)).toBe(false)
96109
expect(getAPIProvider({})).toBe('firstParty')
97110
})
98111

99112
test('empty string is not truthy', () => {
100113
process.env.CLAUDE_CODE_USE_BEDROCK = ''
114+
expect(isEnvTruthy(process.env.CLAUDE_CODE_USE_BEDROCK)).toBe(false)
101115
expect(getAPIProvider({})).toBe('firstParty')
102116
})
103117
})

0 commit comments

Comments
 (0)