import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs' import { join } from 'path' import { tmpdir } from 'os' import YAML from 'js-yaml' vi.mock('../../packages/server/src/services/hermes/hermes-cli', () => ({ restartGateway: vi.fn().mockResolvedValue(undefined), })) let hermesHome = '' async function loadProvidersController() { vi.resetModules() process.env.HERMES_HOME = hermesHome return import('../../packages/server/src/controllers/hermes/providers') } function makeCtx(poolKey: string, body: Record, profile = 'research') { return { params: { poolKey: encodeURIComponent(poolKey) }, request: { body }, state: { profile: { name: profile } }, status: 200, body: undefined as unknown, } } function readYaml(filePath: string) { return YAML.load(readFileSync(filePath, 'utf-8')) as any } describe('providers controller update', () => { beforeEach(() => { hermesHome = mkdtempSync(join(tmpdir(), 'hwui-provider-update-')) mkdirSync(join(hermesHome, 'profiles', 'research'), { recursive: true }) writeFileSync(join(hermesHome, 'config.yaml'), 'model:\n provider: deepseek\n default: keep-default-model\n') writeFileSync(join(hermesHome, '.env'), [ 'DEEPSEEK_API_KEY=keep-default-key', '', ].join('\n')) writeFileSync(join(hermesHome, 'profiles', 'research', 'config.yaml'), [ 'model:', ' provider: custom:research-proxy', ' default: research-model', 'custom_providers:', ' - name: research-proxy', ' base_url: https://research.invalid/v1', ' api_key: old-research-custom-key', ' model: research-model', '', ].join('\n')) writeFileSync(join(hermesHome, 'profiles', 'research', '.env'), [ 'DEEPSEEK_API_KEY=old-research-key', '', ].join('\n')) }) afterEach(() => { delete process.env.HERMES_HOME vi.doUnmock('../../packages/server/src/controllers/hermes/providers') vi.clearAllMocks() if (hermesHome) rmSync(hermesHome, { recursive: true, force: true }) hermesHome = '' }) it('updates built-in provider API keys in the request-scoped profile env only', async () => { const { update } = await loadProvidersController() const ctx = makeCtx('deepseek', { api_key: 'new-research-key' }) await update(ctx) expect(ctx.body).toEqual({ success: true }) expect(readFileSync(join(hermesHome, '.env'), 'utf-8')).toContain('DEEPSEEK_API_KEY=keep-default-key') expect(readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf-8')).toContain('DEEPSEEK_API_KEY=new-research-key') }) it('updates custom provider API keys in the request-scoped profile config only', async () => { const defaultConfigPath = join(hermesHome, 'config.yaml') writeFileSync(defaultConfigPath, [ 'model:', ' provider: custom:research-proxy', ' default: default-model', 'custom_providers:', ' - name: research-proxy', ' base_url: https://default.invalid/v1', ' api_key: keep-default-custom-key', ' model: default-model', '', ].join('\n')) const { update } = await loadProvidersController() const ctx = makeCtx('custom:research-proxy', { api_key: 'new-research-custom-key' }) await update(ctx) expect(ctx.body).toEqual({ success: true }) const defaultConfig = readYaml(defaultConfigPath) const researchConfig = readYaml(join(hermesHome, 'profiles', 'research', 'config.yaml')) expect(defaultConfig.custom_providers[0].api_key).toBe('keep-default-custom-key') expect(researchConfig.custom_providers[0].api_key).toBe('new-research-custom-key') }) it('updates custom provider api_mode in the request-scoped profile config only', async () => { const defaultConfigPath = join(hermesHome, 'config.yaml') writeFileSync(defaultConfigPath, [ 'model:', ' provider: custom:research-proxy', ' default: default-model', 'custom_providers:', ' - name: research-proxy', ' base_url: https://default.invalid/v1', ' api_key: keep-default-custom-key', ' model: default-model', ' api_mode: codex_responses', '', ].join('\n')) const { update } = await loadProvidersController() const ctx = makeCtx('custom:research-proxy', { api_mode: 'chat_completions' }) await update(ctx) expect(ctx.body).toEqual({ success: true }) const defaultConfig = readYaml(defaultConfigPath) const researchConfig = readYaml(join(hermesHome, 'profiles', 'research', 'config.yaml')) expect(defaultConfig.custom_providers[0].api_mode).toBe('codex_responses') expect(researchConfig.custom_providers[0].api_mode).toBe('chat_completions') }) it('updates a built-in Base URL and key without changing defaults or another profile', async () => { const { update } = await loadProvidersController() const before = readFileSync(join(hermesHome, 'profiles', 'research', 'config.yaml'), 'utf8') const ctx = makeCtx('minimax-cn', { base_url: 'https://proxy.invalid/anthropic/v1', api_key: 'new-key' }) await update(ctx) expect(ctx.body).toEqual({ success: true }) const env = readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8') expect(env).toContain('MINIMAX_CN_API_KEY=new-key') expect(env).toContain('MINIMAX_CN_BASE_URL=https://proxy.invalid/anthropic/v1') expect(readFileSync(join(hermesHome, '.env'), 'utf8')).not.toContain('new-key') expect(readFileSync(join(hermesHome, 'profiles', 'research', 'config.yaml'), 'utf8')).toBe(before) }) it('keeps an existing key when only the URL is edited', async () => { const { update } = await loadProvidersController() const ctx = makeCtx('deepseek', { base_url: 'https://new.invalid/v1' }) await update(ctx) const env = readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8') expect(env).toContain('DEEPSEEK_API_KEY=old-research-key') expect(env).toContain('DEEPSEEK_BASE_URL=https://new.invalid/v1') }) it('edits a dict-backed provider in place and preserves its models and extra configuration', async () => { const path = join(hermesHome, 'profiles', 'research', 'config.yaml') writeFileSync(path, YAML.dump({ model: { provider: 'custom:dict-proxy', default: 'selected-model' }, providers: { original_key: { name: 'dict-proxy', url: 'https://old.invalid/v1', api_key: 'old-key', models: { 'selected-model': { context_length: 12345 } }, extra_body: { keep: true }, } }, })) const { update } = await loadProvidersController() const ctx = makeCtx('custom:dict-proxy', { base_url: 'https://new.invalid/v1', api_key: 'new-key', source: 'providers', providerKey: 'original_key', }) await update(ctx) expect(ctx.body).toEqual({ success: true }) const config = readYaml(path) expect(config.custom_providers).toBeUndefined() expect(config.model).toEqual({ provider: 'custom:dict-proxy', default: 'selected-model' }) expect(config.providers.original_key).toMatchObject({ base_url: 'https://new.invalid/v1', api_key: 'new-key', models: { 'selected-model': { context_length: 12345 } }, extra_body: { keep: true }, }) }) it('updates an env-backed custom key in the same profile', async () => { const path = join(hermesHome, 'profiles', 'research', 'config.yaml') writeFileSync(path, YAML.dump({ providers: { proxy: { base_url: 'https://proxy.invalid/v1', key_env: 'PROXY_KEY', model: 'keep' } } })) const { update } = await loadProvidersController() await update(makeCtx('custom:proxy', { api_key: 'new-key', source: 'providers', providerKey: 'proxy' })) expect(readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8')).toContain('PROXY_KEY=new-key') expect(readYaml(path).providers.proxy.model).toBe('keep') }) it.each(['', 'bad-url', 'file:///tmp/model'])('rejects an invalid URL before writing the key: %s', async base_url => { const { update } = await loadProvidersController() const ctx = makeCtx('deepseek', { base_url, api_key: 'must-not-save' }) await update(ctx) expect(ctx.status).toBe(400) expect(readFileSync(join(hermesHome, 'profiles', 'research', '.env'), 'utf8')).toContain('DEEPSEEK_API_KEY=old-research-key') }) })