/**
 * Unit tests for configuration module
 */

describe('Configuration Module', () => {
  // Store original environment variables
  const originalEnv = process.env;

  beforeEach(() => {
    // Reset environment variables before each test
    jest.resetModules();
    process.env = { ...originalEnv };
  });

  afterAll(() => {
    // Restore original environment variables
    process.env = originalEnv;
  });

  describe('GITLAB_BASE_URL', () => {
    it('should use environment variable when set', () => {
      // Set environment variable
      process.env.GITLAB_BASE_URL = 'https://custom-gitlab.com/repo/-/raw/main/';
      
      // Import config after setting env var
      const { GITLAB_BASE_URL } = require('../config');
      
      expect(GITLAB_BASE_URL).toBe('https://custom-gitlab.com/repo/-/raw/main/');
    });

    it('should use default value when environment variable is not set', () => {
      // Ensure environment variable is not set
      delete process.env.GITLAB_BASE_URL;
      
      // Import config
      const { GITLAB_BASE_URL } = require('../config');
      
      expect(GITLAB_BASE_URL).toBe('http://gitlab.yeepay.com/awesome/awesome-components/-/raw/main/');
    });
  });

  describe('MAIN_LLMS_URL', () => {
    it('should use environment variable when set', () => {
      // Set environment variable
      process.env.MAIN_LLMS_URL = 'https://custom-gitlab.com/repo/-/raw/main/custom-llms.txt';
      
      // Import config after setting env var
      const { MAIN_LLMS_URL } = require('../config');
      
      expect(MAIN_LLMS_URL).toBe('https://custom-gitlab.com/repo/-/raw/main/custom-llms.txt');
    });

    it('should use default value when environment variable is not set', () => {
      // Ensure environment variable is not set
      delete process.env.MAIN_LLMS_URL;
      
      // Import config
      const { MAIN_LLMS_URL } = require('../config');
      
      expect(MAIN_LLMS_URL).toBe('http://gitlab.yeepay.com/awesome/awesome-components/-/raw/main/llms.txt');
    });
  });

  describe('GITLAB_PERSONAL_ACCESS_TOKEN', () => {
    it('should use environment variable when set', () => {
      // Set environment variable
      process.env.GITLAB_PERSONAL_ACCESS_TOKEN = 'test-token-123456';

      // Import config after setting env var
      const { GITLAB_PERSONAL_ACCESS_TOKEN } = require('../config');

      expect(GITLAB_PERSONAL_ACCESS_TOKEN).toBe('test-token-123456');
    });

    it('should be undefined when environment variable is not set', () => {
      // Ensure environment variable is not set
      delete process.env.GITLAB_PERSONAL_ACCESS_TOKEN;

      // Import config
      const { GITLAB_PERSONAL_ACCESS_TOKEN } = require('../config');

      expect(GITLAB_PERSONAL_ACCESS_TOKEN).toBeUndefined();
    });

    it('should handle empty string', () => {
      // Set empty environment variable
      process.env.GITLAB_PERSONAL_ACCESS_TOKEN = '';

      // Import config
      const { GITLAB_PERSONAL_ACCESS_TOKEN } = require('../config');

      expect(GITLAB_PERSONAL_ACCESS_TOKEN).toBe('');
    });
  });

  describe('PORT', () => {
    it('should use environment variable when set to valid number', () => {
      // Set environment variable
      process.env.PORT = '8080';

      // Import config after setting env var
      const { PORT } = require('../config');

      expect(PORT).toBe(8080);
    });

    it('should use default value when environment variable is not set', () => {
      // Ensure environment variable is not set
      delete process.env.PORT;

      // Import config
      const { PORT } = require('../config');

      expect(PORT).toBe(3000);
    });

    it('should use default value when environment variable is not a valid number', () => {
      // Set invalid environment variable
      process.env.PORT = 'invalid';

      // Import config
      const { PORT } = require('../config');

      expect(PORT).toBe(NaN); // parseInt('invalid', 10) returns NaN
    });
  });

  describe('config object', () => {
    it('should contain all required properties', () => {
      // Import config
      const { config } = require('../config');

      expect(config).toHaveProperty('port');
      expect(config).toHaveProperty('name');
      expect(config).toHaveProperty('version');
      expect(config).toHaveProperty('description');
      expect(config).toHaveProperty('urls');
      expect(config).toHaveProperty('auth');
      expect(config.urls).toHaveProperty('gitlabBase');
      expect(config.urls).toHaveProperty('mainLlms');
      expect(config.auth).toHaveProperty('gitlabToken');
    });

    it('should have correct default values', () => {
      // Ensure environment variables are not set
      delete process.env.GITLAB_BASE_URL;
      delete process.env.MAIN_LLMS_URL;
      delete process.env.GITLAB_PERSONAL_ACCESS_TOKEN;
      delete process.env.PORT;

      // Import config
      const { config } = require('../config');

      expect(config.port).toBe(3000);
      expect(config.name).toBe('awesome-components-mcp');
      expect(config.version).toBe('1.0.0');
      expect(config.urls.gitlabBase).toBe('http://gitlab.yeepay.com/awesome/awesome-components/-/raw/main/');
      expect(config.urls.mainLlms).toBe('http://gitlab.yeepay.com/awesome/awesome-components/-/raw/main/llms.txt');
      expect(config.auth.gitlabToken).toBeUndefined();
    });

    it('should include auth token when environment variable is set', () => {
      // Set environment variables
      process.env.GITLAB_PERSONAL_ACCESS_TOKEN = 'test-token-123456';

      // Import config
      const { config } = require('../config');

      expect(config.auth.gitlabToken).toBe('test-token-123456');
    });
  });
});
