import { describe, it, expect } from 'vitest';
import { loadConfig, validateConfig } from './config.js';
import type { AppConfig } from './config.js';

describe('config', () => {
  describe('loadConfig', () => {
    it('should return default values when env vars are not set', () => {
      const cfg = loadConfig();
      expect(cfg.port).toBe(3001);
      expect(typeof cfg.nodeEnv).toBe('string');
      expect(cfg.nodeEnv.length).toBeGreaterThan(0);
      expect(cfg.dataDir).toContain('data');
    });
  });

  describe('validateConfig', () => {
    it('should return error when encryptionKey is empty', () => {
      const cfg: AppConfig = {
        port: 3001,
        nodeEnv: 'development',
        openrouterApiKey: '',
        kieApiKey: '',
        geminiApiKey: '',
        encryptionKey: '',
        dataDir: '/tmp/data',
        webhookBaseUrl: '',
        telegramApiId: 0,
        telegramApiHash: '',
      };
      const errors = validateConfig(cfg);
      expect(errors).toContain('ENCRYPTION_KEY is required for secure storage');
    });

    it('should return error for invalid port', () => {
      const cfg: AppConfig = {
        port: 0,
        nodeEnv: 'development',
        openrouterApiKey: '',
        kieApiKey: '',
        geminiApiKey: '',
        encryptionKey: 'some-key',
        dataDir: '/tmp/data',
        webhookBaseUrl: '',
        telegramApiId: 0,
        telegramApiHash: '',
      };
      const errors = validateConfig(cfg);
      expect(errors).toContain('PORT must be between 1 and 65535');
    });

    it('should return no errors for valid config', () => {
      const cfg: AppConfig = {
        port: 3001,
        nodeEnv: 'development',
        openrouterApiKey: '',
        kieApiKey: '',
        geminiApiKey: '',
        encryptionKey: 'my-secret-key',
        dataDir: '/tmp/data',
        webhookBaseUrl: '',
        telegramApiId: 0,
        telegramApiHash: '',
      };
      const errors = validateConfig(cfg);
      expect(errors).toHaveLength(0);
    });
  });
});
