|
| 1 | +import { |
| 2 | + existsSync, |
| 3 | + mkdirSync, |
| 4 | + readFileSync, |
| 5 | + rmSync, |
| 6 | + writeFileSync, |
| 7 | +} from 'node:fs'; |
| 8 | +import { homedir } from 'node:os'; |
| 9 | +import { join } from 'node:path'; |
| 10 | + |
| 11 | +const CONFIG_DIR = join(homedir(), '.code-ollama'); |
| 12 | +const CONFIG_PATH = join(CONFIG_DIR, 'config.json'); |
| 13 | + |
| 14 | +function writeConfig(data: object) { |
| 15 | + mkdirSync(CONFIG_DIR, { recursive: true }); |
| 16 | + writeFileSync(CONFIG_PATH, JSON.stringify(data), 'utf8'); |
| 17 | +} |
| 18 | + |
| 19 | +function removeConfig() { |
| 20 | + if (existsSync(CONFIG_PATH)) { |
| 21 | + rmSync(CONFIG_PATH); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('config', () => { |
| 26 | + const originalEnv = { ...process.env }; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + delete process.env.OLLAMA_HOST; |
| 30 | + delete process.env.OLLAMA_MODEL; |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + process.env.OLLAMA_HOST = originalEnv.OLLAMA_HOST; |
| 35 | + process.env.OLLAMA_MODEL = originalEnv.OLLAMA_MODEL; |
| 36 | + removeConfig(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('loadConfig', () => { |
| 40 | + it('returns hardcoded defaults when no file and no env vars', async () => { |
| 41 | + removeConfig(); |
| 42 | + const { loadConfig } = await import('./config'); |
| 43 | + const cfg = loadConfig(); |
| 44 | + expect(cfg.host).toBe('http://localhost:11434'); |
| 45 | + expect(cfg.model).toBe('gemma4'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('reads host and model from config file', async () => { |
| 49 | + writeConfig({ host: 'http://remote:11434', model: 'llama3' }); |
| 50 | + const { loadConfig } = await import('./config'); |
| 51 | + const cfg = loadConfig(); |
| 52 | + expect(cfg.host).toBe('http://remote:11434'); |
| 53 | + expect(cfg.model).toBe('llama3'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('env vars override config file values', async () => { |
| 57 | + writeConfig({ host: 'http://remote:11434', model: 'llama3' }); |
| 58 | + process.env.OLLAMA_HOST = 'http://env-host:11434'; |
| 59 | + process.env.OLLAMA_MODEL = 'codellama'; |
| 60 | + const { loadConfig } = await import('./config'); |
| 61 | + const cfg = loadConfig(); |
| 62 | + expect(cfg.host).toBe('http://env-host:11434'); |
| 63 | + expect(cfg.model).toBe('codellama'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns defaults for missing keys in config file', async () => { |
| 67 | + writeConfig({ model: 'llama3' }); |
| 68 | + const { loadConfig } = await import('./config'); |
| 69 | + const cfg = loadConfig(); |
| 70 | + expect(cfg.host).toBe('http://localhost:11434'); |
| 71 | + expect(cfg.model).toBe('llama3'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns defaults when config file is malformed JSON', async () => { |
| 75 | + mkdirSync(CONFIG_DIR, { recursive: true }); |
| 76 | + writeFileSync(CONFIG_PATH, 'not json', 'utf8'); |
| 77 | + const { loadConfig } = await import('./config'); |
| 78 | + const cfg = loadConfig(); |
| 79 | + expect(cfg.host).toBe('http://localhost:11434'); |
| 80 | + expect(cfg.model).toBe('gemma4'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('saveConfig', () => { |
| 85 | + it('creates the config file with given values', async () => { |
| 86 | + removeConfig(); |
| 87 | + const { saveConfig } = await import('./config'); |
| 88 | + saveConfig({ model: 'mistral' }); |
| 89 | + const saved = JSON.parse(readFileSync(CONFIG_PATH, 'utf8')) as { |
| 90 | + model: string; |
| 91 | + }; |
| 92 | + expect(saved.model).toBe('mistral'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('merges patch into existing config', async () => { |
| 96 | + writeConfig({ host: 'http://remote:11434', model: 'llama3' }); |
| 97 | + const { saveConfig } = await import('./config'); |
| 98 | + saveConfig({ model: 'mistral' }); |
| 99 | + const saved = JSON.parse(readFileSync(CONFIG_PATH, 'utf8')) as { |
| 100 | + host: string; |
| 101 | + model: string; |
| 102 | + }; |
| 103 | + expect(saved.host).toBe('http://remote:11434'); |
| 104 | + expect(saved.model).toBe('mistral'); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments