test: isolate latest main suite regressions (#427)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { afterEach, expect, test } from 'bun:test'
|
import { afterEach, expect, mock, test } from 'bun:test'
|
||||||
|
|
||||||
import { getModelOptions } from './modelOptions.js'
|
import { saveGlobalConfig } from '../config.js'
|
||||||
|
|
||||||
const originalEnv = {
|
const originalEnv = {
|
||||||
CLAUDE_CODE_USE_GITHUB: process.env.CLAUDE_CODE_USE_GITHUB,
|
CLAUDE_CODE_USE_GITHUB: process.env.CLAUDE_CODE_USE_GITHUB,
|
||||||
@@ -25,9 +25,27 @@ afterEach(() => {
|
|||||||
process.env.OPENAI_BASE_URL = originalEnv.OPENAI_BASE_URL
|
process.env.OPENAI_BASE_URL = originalEnv.OPENAI_BASE_URL
|
||||||
process.env.ANTHROPIC_CUSTOM_MODEL_OPTION =
|
process.env.ANTHROPIC_CUSTOM_MODEL_OPTION =
|
||||||
originalEnv.ANTHROPIC_CUSTOM_MODEL_OPTION
|
originalEnv.ANTHROPIC_CUSTOM_MODEL_OPTION
|
||||||
|
saveGlobalConfig(current => ({
|
||||||
|
...current,
|
||||||
|
additionalModelOptionsCache: [],
|
||||||
|
additionalModelOptionsCacheScope: undefined,
|
||||||
|
openaiAdditionalModelOptionsCache: [],
|
||||||
|
openaiAdditionalModelOptionsCacheByProfile: {},
|
||||||
|
providerProfiles: [],
|
||||||
|
activeProviderProfileId: undefined,
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
test('GitHub provider exposes only default + GitHub model in /model options', () => {
|
async function importFreshModelOptionsModule() {
|
||||||
|
mock.restore()
|
||||||
|
mock.module('./providers.js', () => ({
|
||||||
|
getAPIProvider: () => 'github',
|
||||||
|
}))
|
||||||
|
const nonce = `${Date.now()}-${Math.random()}`
|
||||||
|
return import(`./modelOptions.js?ts=${nonce}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
test('GitHub provider exposes only default + GitHub model in /model options', async () => {
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
delete process.env.CLAUDE_CODE_USE_OPENAI
|
delete process.env.CLAUDE_CODE_USE_OPENAI
|
||||||
delete process.env.CLAUDE_CODE_USE_GEMINI
|
delete process.env.CLAUDE_CODE_USE_GEMINI
|
||||||
@@ -38,6 +56,7 @@ test('GitHub provider exposes only default + GitHub model in /model options', ()
|
|||||||
process.env.OPENAI_MODEL = 'github:copilot'
|
process.env.OPENAI_MODEL = 'github:copilot'
|
||||||
delete process.env.ANTHROPIC_CUSTOM_MODEL_OPTION
|
delete process.env.ANTHROPIC_CUSTOM_MODEL_OPTION
|
||||||
|
|
||||||
|
const { getModelOptions } = await importFreshModelOptionsModule()
|
||||||
const options = getModelOptions(false)
|
const options = getModelOptions(false)
|
||||||
const nonDefault = options.filter(option => option.value !== null)
|
const nonDefault = options.filter(option => option.value !== null)
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,6 @@
|
|||||||
import { afterEach, describe, expect, test } from 'bun:test'
|
import { afterEach, describe, expect, mock, test } from 'bun:test'
|
||||||
|
|
||||||
import { saveGlobalConfig, type ProviderProfile } from './config.js'
|
import type { ProviderProfile } from './config.js'
|
||||||
import { getAPIProvider } from './model/providers.js'
|
|
||||||
import {
|
|
||||||
applyActiveProviderProfileFromConfig,
|
|
||||||
applyProviderProfileToProcessEnv,
|
|
||||||
deleteProviderProfile,
|
|
||||||
getProviderProfiles,
|
|
||||||
getProviderPresetDefaults,
|
|
||||||
persistActiveProviderProfileModel,
|
|
||||||
} from './providerProfiles.js'
|
|
||||||
|
|
||||||
const originalEnv = { ...process.env }
|
const originalEnv = { ...process.env }
|
||||||
|
|
||||||
@@ -31,6 +22,34 @@ const RESTORED_KEYS = [
|
|||||||
'ANTHROPIC_API_KEY',
|
'ANTHROPIC_API_KEY',
|
||||||
] as const
|
] as const
|
||||||
|
|
||||||
|
type MockConfigState = {
|
||||||
|
providerProfiles: ProviderProfile[]
|
||||||
|
activeProviderProfileId?: string
|
||||||
|
openaiAdditionalModelOptionsCache: unknown[]
|
||||||
|
openaiAdditionalModelOptionsCacheByProfile: Record<string, unknown[]>
|
||||||
|
additionalModelOptionsCache?: unknown[]
|
||||||
|
additionalModelOptionsCacheScope?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function createMockConfigState(): MockConfigState {
|
||||||
|
return {
|
||||||
|
providerProfiles: [],
|
||||||
|
activeProviderProfileId: undefined,
|
||||||
|
openaiAdditionalModelOptionsCache: [],
|
||||||
|
openaiAdditionalModelOptionsCacheByProfile: {},
|
||||||
|
additionalModelOptionsCache: [],
|
||||||
|
additionalModelOptionsCacheScope: undefined,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mockConfigState: MockConfigState = createMockConfigState()
|
||||||
|
|
||||||
|
function saveMockGlobalConfig(
|
||||||
|
updater: (current: MockConfigState) => MockConfigState,
|
||||||
|
): void {
|
||||||
|
mockConfigState = updater(mockConfigState)
|
||||||
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
for (const key of RESTORED_KEYS) {
|
for (const key of RESTORED_KEYS) {
|
||||||
if (originalEnv[key] === undefined) {
|
if (originalEnv[key] === undefined) {
|
||||||
@@ -40,15 +59,30 @@ afterEach(() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveGlobalConfig(current => ({
|
mock.restore()
|
||||||
...current,
|
mockConfigState = createMockConfigState()
|
||||||
providerProfiles: [],
|
|
||||||
activeProviderProfileId: undefined,
|
|
||||||
openaiAdditionalModelOptionsCache: [],
|
|
||||||
openaiAdditionalModelOptionsCacheByProfile: {},
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
async function importFreshProviderProfileModules() {
|
||||||
|
mock.restore()
|
||||||
|
mock.module('./config.js', () => ({
|
||||||
|
getGlobalConfig: () => mockConfigState,
|
||||||
|
saveGlobalConfig: (
|
||||||
|
updater: (current: MockConfigState) => MockConfigState,
|
||||||
|
) => {
|
||||||
|
mockConfigState = updater(mockConfigState)
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
const nonce = `${Date.now()}-${Math.random()}`
|
||||||
|
const providers = await import(`./model/providers.js?ts=${nonce}`)
|
||||||
|
const providerProfiles = await import(`./providerProfiles.js?ts=${nonce}`)
|
||||||
|
|
||||||
|
return {
|
||||||
|
...providers,
|
||||||
|
...providerProfiles,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function buildProfile(overrides: Partial<ProviderProfile> = {}): ProviderProfile {
|
function buildProfile(overrides: Partial<ProviderProfile> = {}): ProviderProfile {
|
||||||
return {
|
return {
|
||||||
id: 'provider_test',
|
id: 'provider_test',
|
||||||
@@ -61,7 +95,9 @@ function buildProfile(overrides: Partial<ProviderProfile> = {}): ProviderProfile
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('applyProviderProfileToProcessEnv', () => {
|
describe('applyProviderProfileToProcessEnv', () => {
|
||||||
test('openai profile clears competing gemini/github flags', () => {
|
test('openai profile clears competing gemini/github flags', async () => {
|
||||||
|
const { applyProviderProfileToProcessEnv, getAPIProvider } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
|
|
||||||
@@ -76,7 +112,9 @@ describe('applyProviderProfileToProcessEnv', () => {
|
|||||||
expect(getAPIProvider()).toBe('openai')
|
expect(getAPIProvider()).toBe('openai')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('anthropic profile clears competing gemini/github flags', () => {
|
test('anthropic profile clears competing gemini/github flags', async () => {
|
||||||
|
const { applyProviderProfileToProcessEnv, getAPIProvider } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
|
|
||||||
@@ -96,7 +134,9 @@ describe('applyProviderProfileToProcessEnv', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('applyActiveProviderProfileFromConfig', () => {
|
describe('applyActiveProviderProfileFromConfig', () => {
|
||||||
test('does not override explicit startup provider selection', () => {
|
test('does not override explicit startup provider selection', async () => {
|
||||||
|
const { applyActiveProviderProfileFromConfig } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
||||||
process.env.OPENAI_MODEL = 'qwen2.5:3b'
|
process.env.OPENAI_MODEL = 'qwen2.5:3b'
|
||||||
@@ -117,7 +157,9 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('does not override explicit startup selection when profile marker is stale', () => {
|
test('does not override explicit startup selection when profile marker is stale', async () => {
|
||||||
|
const { applyActiveProviderProfileFromConfig } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED = '1'
|
process.env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED = '1'
|
||||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
||||||
@@ -140,7 +182,9 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('re-applies active profile when profile-managed env drifts', () => {
|
test('re-applies active profile when profile-managed env drifts', async () => {
|
||||||
|
const { applyActiveProviderProfileFromConfig, applyProviderProfileToProcessEnv } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
applyProviderProfileToProcessEnv(
|
applyProviderProfileToProcessEnv(
|
||||||
buildProfile({
|
buildProfile({
|
||||||
id: 'saved_openai',
|
id: 'saved_openai',
|
||||||
@@ -168,7 +212,9 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
expect(process.env.OPENAI_BASE_URL).toBe('http://192.168.33.108:11434/v1')
|
expect(process.env.OPENAI_BASE_URL).toBe('http://192.168.33.108:11434/v1')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('does not re-apply active profile when flags conflict with current provider', () => {
|
test('does not re-apply active profile when flags conflict with current provider', async () => {
|
||||||
|
const { applyActiveProviderProfileFromConfig, applyProviderProfileToProcessEnv } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
applyProviderProfileToProcessEnv(
|
applyProviderProfileToProcessEnv(
|
||||||
buildProfile({
|
buildProfile({
|
||||||
id: 'saved_openai',
|
id: 'saved_openai',
|
||||||
@@ -196,7 +242,9 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
expect(process.env.OPENAI_MODEL).toBe('github:copilot')
|
expect(process.env.OPENAI_MODEL).toBe('github:copilot')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('applies active profile when no explicit provider is selected', () => {
|
test('applies active profile when no explicit provider is selected', async () => {
|
||||||
|
const { applyActiveProviderProfileFromConfig } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
delete process.env.CLAUDE_CODE_USE_OPENAI
|
delete process.env.CLAUDE_CODE_USE_OPENAI
|
||||||
delete process.env.CLAUDE_CODE_USE_GEMINI
|
delete process.env.CLAUDE_CODE_USE_GEMINI
|
||||||
delete process.env.CLAUDE_CODE_USE_GITHUB
|
delete process.env.CLAUDE_CODE_USE_GITHUB
|
||||||
@@ -226,14 +274,19 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('persistActiveProviderProfileModel', () => {
|
describe('persistActiveProviderProfileModel', () => {
|
||||||
test('updates active profile model and current env for profile-managed sessions', () => {
|
test('updates active profile model and current env for profile-managed sessions', async () => {
|
||||||
|
const {
|
||||||
|
applyProviderProfileToProcessEnv,
|
||||||
|
getProviderProfiles,
|
||||||
|
persistActiveProviderProfileModel,
|
||||||
|
} = await importFreshProviderProfileModules()
|
||||||
const activeProfile = buildProfile({
|
const activeProfile = buildProfile({
|
||||||
id: 'saved_openai',
|
id: 'saved_openai',
|
||||||
baseUrl: 'http://192.168.33.108:11434/v1',
|
baseUrl: 'http://192.168.33.108:11434/v1',
|
||||||
model: 'kimi-k2.5:cloud',
|
model: 'kimi-k2.5:cloud',
|
||||||
})
|
})
|
||||||
|
|
||||||
saveGlobalConfig(current => ({
|
saveMockGlobalConfig(current => ({
|
||||||
...current,
|
...current,
|
||||||
providerProfiles: [activeProfile],
|
providerProfiles: [activeProfile],
|
||||||
activeProviderProfileId: activeProfile.id,
|
activeProviderProfileId: activeProfile.id,
|
||||||
@@ -255,13 +308,17 @@ describe('persistActiveProviderProfileModel', () => {
|
|||||||
expect(saved?.model).toBe('minimax-m2.5:cloud')
|
expect(saved?.model).toBe('minimax-m2.5:cloud')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('does not mutate process env when session is not profile-managed', () => {
|
test('does not mutate process env when session is not profile-managed', async () => {
|
||||||
|
const {
|
||||||
|
getProviderProfiles,
|
||||||
|
persistActiveProviderProfileModel,
|
||||||
|
} = await importFreshProviderProfileModules()
|
||||||
const activeProfile = buildProfile({
|
const activeProfile = buildProfile({
|
||||||
id: 'saved_openai',
|
id: 'saved_openai',
|
||||||
model: 'kimi-k2.5:cloud',
|
model: 'kimi-k2.5:cloud',
|
||||||
})
|
})
|
||||||
|
|
||||||
saveGlobalConfig(current => ({
|
saveMockGlobalConfig(current => ({
|
||||||
...current,
|
...current,
|
||||||
providerProfiles: [activeProfile],
|
providerProfiles: [activeProfile],
|
||||||
activeProviderProfileId: activeProfile.id,
|
activeProviderProfileId: activeProfile.id,
|
||||||
@@ -283,7 +340,8 @@ describe('persistActiveProviderProfileModel', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('getProviderPresetDefaults', () => {
|
describe('getProviderPresetDefaults', () => {
|
||||||
test('ollama preset defaults to a local Ollama model', () => {
|
test('ollama preset defaults to a local Ollama model', async () => {
|
||||||
|
const { getProviderPresetDefaults } = await importFreshProviderProfileModules()
|
||||||
delete process.env.OPENAI_MODEL
|
delete process.env.OPENAI_MODEL
|
||||||
|
|
||||||
const defaults = getProviderPresetDefaults('ollama')
|
const defaults = getProviderPresetDefaults('ollama')
|
||||||
@@ -294,7 +352,11 @@ describe('getProviderPresetDefaults', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('deleteProviderProfile', () => {
|
describe('deleteProviderProfile', () => {
|
||||||
test('deleting final profile clears provider env when active profile applied it', () => {
|
test('deleting final profile clears provider env when active profile applied it', async () => {
|
||||||
|
const {
|
||||||
|
applyProviderProfileToProcessEnv,
|
||||||
|
deleteProviderProfile,
|
||||||
|
} = await importFreshProviderProfileModules()
|
||||||
applyProviderProfileToProcessEnv(
|
applyProviderProfileToProcessEnv(
|
||||||
buildProfile({
|
buildProfile({
|
||||||
id: 'only_profile',
|
id: 'only_profile',
|
||||||
@@ -304,7 +366,7 @@ describe('deleteProviderProfile', () => {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
saveGlobalConfig(current => ({
|
saveMockGlobalConfig(current => ({
|
||||||
...current,
|
...current,
|
||||||
providerProfiles: [buildProfile({ id: 'only_profile' })],
|
providerProfiles: [buildProfile({ id: 'only_profile' })],
|
||||||
activeProviderProfileId: 'only_profile',
|
activeProviderProfileId: 'only_profile',
|
||||||
@@ -334,12 +396,13 @@ describe('deleteProviderProfile', () => {
|
|||||||
expect(process.env.ANTHROPIC_API_KEY).toBeUndefined()
|
expect(process.env.ANTHROPIC_API_KEY).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('deleting final profile preserves explicit startup provider env', () => {
|
test('deleting final profile preserves explicit startup provider env', async () => {
|
||||||
|
const { deleteProviderProfile } = await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
process.env.OPENAI_BASE_URL = 'http://localhost:11434/v1'
|
||||||
process.env.OPENAI_MODEL = 'qwen2.5:3b'
|
process.env.OPENAI_MODEL = 'qwen2.5:3b'
|
||||||
|
|
||||||
saveGlobalConfig(current => ({
|
saveMockGlobalConfig(current => ({
|
||||||
...current,
|
...current,
|
||||||
providerProfiles: [buildProfile({ id: 'only_profile' })],
|
providerProfiles: [buildProfile({ id: 'only_profile' })],
|
||||||
activeProviderProfileId: 'only_profile',
|
activeProviderProfileId: 'only_profile',
|
||||||
|
|||||||
Reference in New Issue
Block a user