feat: add config schema and loader with env var expansion
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { loadConfig } from './loader.js';
|
||||
import { writeFileSync, mkdirSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
|
||||
describe('loadConfig', () => {
|
||||
const testDir = join(tmpdir(), 'flynn-test-config');
|
||||
|
||||
it('loads valid config from file', () => {
|
||||
mkdirSync(testDir, { recursive: true });
|
||||
const configPath = join(testDir, 'config.yaml');
|
||||
writeFileSync(configPath, `
|
||||
telegram:
|
||||
bot_token: "test-token"
|
||||
allowed_chat_ids: [123456789]
|
||||
server:
|
||||
port: 18800
|
||||
models:
|
||||
default:
|
||||
provider: anthropic
|
||||
model: claude-sonnet
|
||||
`);
|
||||
|
||||
const config = loadConfig(configPath);
|
||||
|
||||
expect(config.telegram.bot_token).toBe('test-token');
|
||||
expect(config.telegram.allowed_chat_ids).toContain(123456789);
|
||||
expect(config.server.port).toBe(18800);
|
||||
expect(config.models.default.provider).toBe('anthropic');
|
||||
|
||||
rmSync(testDir, { recursive: true });
|
||||
});
|
||||
|
||||
it('expands environment variables', () => {
|
||||
mkdirSync(testDir, { recursive: true });
|
||||
const configPath = join(testDir, 'config.yaml');
|
||||
process.env.TEST_BOT_TOKEN = 'env-token-value';
|
||||
writeFileSync(configPath, `
|
||||
telegram:
|
||||
bot_token: \${TEST_BOT_TOKEN}
|
||||
allowed_chat_ids: [123]
|
||||
server:
|
||||
port: 18800
|
||||
models:
|
||||
default:
|
||||
provider: anthropic
|
||||
model: claude-sonnet
|
||||
`);
|
||||
|
||||
const config = loadConfig(configPath);
|
||||
|
||||
expect(config.telegram.bot_token).toBe('env-token-value');
|
||||
|
||||
delete process.env.TEST_BOT_TOKEN;
|
||||
rmSync(testDir, { recursive: true });
|
||||
});
|
||||
|
||||
it('throws on invalid config', () => {
|
||||
mkdirSync(testDir, { recursive: true });
|
||||
const configPath = join(testDir, 'config.yaml');
|
||||
writeFileSync(configPath, `
|
||||
telegram:
|
||||
bot_token: ""
|
||||
`);
|
||||
|
||||
expect(() => loadConfig(configPath)).toThrow();
|
||||
|
||||
rmSync(testDir, { recursive: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user