|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { getProviderConfig, loadConfig, saveConfig } from '../src/config.js'; |
| 6 | + |
| 7 | +describe('process provider selection', () => { |
| 8 | + let directory: string; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + directory = await fs.mkdtemp(path.join(os.tmpdir(), 'autohand-provider-env-')); |
| 12 | + vi.stubEnv('AUTOHAND_PROVIDER', undefined); |
| 13 | + vi.stubEnv('AUTOHAND_AI_API_KEY', 'fixture-inference-key'); |
| 14 | + vi.stubEnv('AUTOHAND_AI_BASE_URL', 'http://127.0.0.1:12345/v1'); |
| 15 | + vi.stubEnv('AUTOHAND_AI_PLAN', 'cloud'); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(async () => { |
| 19 | + vi.unstubAllEnvs(); |
| 20 | + await fs.remove(directory); |
| 21 | + }); |
| 22 | + |
| 23 | + const formats = [ |
| 24 | + ['json', '{"provider":"openrouter","auth":{"token":"fixture-account-token"}}'], |
| 25 | + ['yaml', 'provider: openrouter\nauth:\n token: fixture-account-token\n'], |
| 26 | + ['toml', 'provider = "openrouter"\n[auth]\ntoken = "fixture-account-token"\n'], |
| 27 | + ] as const; |
| 28 | + |
| 29 | + it.each(formats)('selects Autohand AI over global and workspace %s settings without rewriting them', async (format, content) => { |
| 30 | + const configPath = path.join(directory, `config.${format}`); |
| 31 | + const workspace = path.join(directory, 'project'); |
| 32 | + const localPath = path.join(workspace, '.autohand', 'settings.local.json'); |
| 33 | + const localContent = '{"provider":"openai","permissions":{"mode":"restricted"}}'; |
| 34 | + await fs.writeFile(configPath, content); |
| 35 | + await fs.outputFile(localPath, localContent); |
| 36 | + vi.stubEnv('AUTOHAND_PROVIDER', 'autohandai'); |
| 37 | + |
| 38 | + const config = await loadConfig(configPath, workspace, { initializeTheme: false }); |
| 39 | + |
| 40 | + expect(config.provider).toBe('autohandai'); |
| 41 | + expect(config.auth?.token).toBe('fixture-account-token'); |
| 42 | + expect(config.permissions?.mode).toBe('restricted'); |
| 43 | + expect(getProviderConfig(config)).toMatchObject({ |
| 44 | + apiKey: 'fixture-inference-key', baseUrl: 'http://127.0.0.1:12345/v1', model: 'fantail', |
| 45 | + }); |
| 46 | + expect(await fs.readFile(configPath, 'utf8')).toBe(content); |
| 47 | + expect(await fs.readFile(localPath, 'utf8')).toBe(localContent); |
| 48 | + }); |
| 49 | + |
| 50 | + it.each(['anthropic', 'custom:acme', 'extension:company-provider', 'vertex'])('accepts the existing provider contract for %s', async provider => { |
| 51 | + const configPath = path.join(directory, 'config.json'); |
| 52 | + await fs.writeJson(configPath, { provider: 'openrouter' }); |
| 53 | + vi.stubEnv('AUTOHAND_PROVIDER', provider); |
| 54 | + |
| 55 | + const config = await loadConfig(configPath, undefined, { initializeTheme: false }); |
| 56 | + |
| 57 | + expect(config.provider).toBe(provider === 'vertex' ? 'vertexai' : provider); |
| 58 | + }); |
| 59 | + |
| 60 | + it.each(['', ' ', 'unknown-provider', 'extension:'])('rejects invalid explicit selection %j instead of silently using a saved provider', async provider => { |
| 61 | + const configPath = path.join(directory, 'config.json'); |
| 62 | + await fs.writeJson(configPath, { provider: 'openrouter' }); |
| 63 | + vi.stubEnv('AUTOHAND_PROVIDER', provider); |
| 64 | + |
| 65 | + await expect(loadConfig(configPath, undefined, { initializeTheme: false })) |
| 66 | + .rejects.toThrow('AUTOHAND_PROVIDER'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('keeps the saved provider when only inference credentials are supplied', async () => { |
| 70 | + const configPath = path.join(directory, 'config.json'); |
| 71 | + await fs.writeJson(configPath, { provider: 'openrouter' }); |
| 72 | + |
| 73 | + const config = await loadConfig(configPath, undefined, { initializeTheme: false }); |
| 74 | + |
| 75 | + expect(config.provider).toBe('openrouter'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('preserves an explicitly disabled inference feature', async () => { |
| 79 | + const configPath = path.join(directory, 'config.json'); |
| 80 | + await fs.writeJson(configPath, { provider: 'openrouter', features: { autohand_inference: false } }); |
| 81 | + vi.stubEnv('AUTOHAND_PROVIDER', 'autohandai'); |
| 82 | + |
| 83 | + const config = await loadConfig(configPath, undefined, { initializeTheme: false }); |
| 84 | + |
| 85 | + expect(config.provider).toBe('autohandai'); |
| 86 | + expect(config.features?.autohand_inference).toBe(false); |
| 87 | + expect(getProviderConfig(config)).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it.each(formats)('does not persist process provider settings during an unrelated %s save', async (format, content) => { |
| 91 | + const configPath = path.join(directory, `config.${format}`); |
| 92 | + await fs.writeFile(configPath, content); |
| 93 | + vi.stubEnv('AUTOHAND_PROVIDER', 'autohandai'); |
| 94 | + const config = await loadConfig(configPath, undefined, { initializeTheme: false }); |
| 95 | + config.ui = { completionReportEnabled: false }; |
| 96 | + |
| 97 | + await saveConfig(config); |
| 98 | + vi.stubEnv('AUTOHAND_PROVIDER', undefined); |
| 99 | + vi.stubEnv('AUTOHAND_AI_API_KEY', undefined); |
| 100 | + vi.stubEnv('AUTOHAND_AI_BASE_URL', undefined); |
| 101 | + vi.stubEnv('AUTOHAND_AI_PLAN', undefined); |
| 102 | + const saved = await loadConfig(configPath, undefined, { initializeTheme: false }); |
| 103 | + |
| 104 | + expect(saved.provider).toBe('openrouter'); |
| 105 | + expect(saved.autohandai).toBeUndefined(); |
| 106 | + expect(saved.ui?.completionReportEnabled).toBe(false); |
| 107 | + expect(saved.auth?.token).toBe('fixture-account-token'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('keeps the latest saved provider credentials when another process updates them', async () => { |
| 111 | + const configPath = path.join(directory, 'config.json'); |
| 112 | + const original = { |
| 113 | + provider: 'openrouter', |
| 114 | + autohandai: { plan: 'cloud', authMode: 'api-key', apiKey: 'saved-key', model: 'moa' }, |
| 115 | + }; |
| 116 | + await fs.writeJson(configPath, original); |
| 117 | + vi.stubEnv('AUTOHAND_PROVIDER', 'autohandai'); |
| 118 | + const config = await loadConfig(configPath, undefined, { initializeTheme: false }); |
| 119 | + const newer = { ...original, provider: 'ollama', autohandai: { ...original.autohandai, apiKey: 'newer-key' } }; |
| 120 | + await fs.writeJson(configPath, newer); |
| 121 | + |
| 122 | + await saveConfig(config); |
| 123 | + |
| 124 | + const saved = await fs.readJson(configPath); |
| 125 | + expect(saved.provider).toBe('ollama'); |
| 126 | + expect(saved.autohandai).toEqual(newer.autohandai); |
| 127 | + expect(config.autohandai?.apiKey).toBe('fixture-inference-key'); |
| 128 | + }); |
| 129 | +}); |
0 commit comments