60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
||
|
||
// Helper pour recharger le module avec de nouvelles variables d'env
|
||
async function loadConfig() {
|
||
const modulePath = '../src/config';
|
||
// Vitest supporte l'invalidation via import dynamique après resetModules
|
||
const mod = await import(modulePath);
|
||
return (mod.loadConfig as typeof import('../src/config').loadConfig)();
|
||
}
|
||
|
||
describe('config', () => {
|
||
const envBackup = { ...process.env };
|
||
|
||
beforeEach(async () => {
|
||
process.env = { ...envBackup };
|
||
delete process.env.PORT;
|
||
delete process.env.API_KEY;
|
||
delete process.env.DATABASE_PATH;
|
||
delete process.env.RELAY_URLS;
|
||
delete process.env.AUTO_RESTART;
|
||
delete process.env.MAX_RESTARTS;
|
||
delete process.env.LOG_LEVEL;
|
||
// @ts-ignore: vitest injecte resetModules via globalThis
|
||
if (typeof vi !== 'undefined') vi.resetModules();
|
||
});
|
||
|
||
it('charge les valeurs par défaut', async () => {
|
||
const cfg = await loadConfig();
|
||
expect(cfg.port).toBe(9090);
|
||
expect(cfg.apiKey).toBe('your-api-key-change-this');
|
||
expect(cfg.databasePath).toBe('./data/server.db');
|
||
expect(cfg.relayUrls).toEqual(['ws://localhost:8090']);
|
||
expect(cfg.autoRestart).toBe(false);
|
||
expect(cfg.maxRestarts).toBe(10);
|
||
expect(cfg.logLevel).toBe('info');
|
||
});
|
||
|
||
it('lit les variables d’environnement', async () => {
|
||
process.env.PORT = '1234';
|
||
process.env.API_KEY = 'k';
|
||
process.env.DATABASE_PATH = '/x.db';
|
||
process.env.RELAY_URLS = 'ws://a:1,ws://b:2';
|
||
process.env.AUTO_RESTART = 'true';
|
||
process.env.MAX_RESTARTS = '7';
|
||
process.env.LOG_LEVEL = 'debug';
|
||
// @ts-ignore
|
||
if (typeof vi !== 'undefined') vi.resetModules();
|
||
const cfg = await loadConfig();
|
||
expect(cfg.port).toBe(1234);
|
||
expect(cfg.apiKey).toBe('k');
|
||
expect(cfg.databasePath).toBe('/x.db');
|
||
expect(cfg.relayUrls).toEqual(['ws://a:1', 'ws://b:2']);
|
||
expect(cfg.autoRestart).toBe(true);
|
||
expect(cfg.maxRestarts).toBe(7);
|
||
expect(cfg.logLevel).toBe('debug');
|
||
});
|
||
});
|
||
|
||
|