const config = require('../../config/env');

describe('Environment Configuration', () => {
  const originalEnv = process.env;

  beforeEach(() => {
    // Restore original env before each test
    jest.resetModules();
    process.env = { ...originalEnv };
  });

  afterAll(() => {
    process.env = originalEnv;
  });

  describe('Configuration Loading', () => {
    it('should load default values when env vars are not set', () => {
      expect(config.port).toBe(5000);
      expect(config.nodeEnv).toBeDefined();
    });

    it('should use environment variables when set', () => {
      expect(config.jwtExpiresIn).toBeDefined();
      expect(config.mongoUri).toBeDefined();
    });

    it('should parse CORS_ORIGIN as array', () => {
      expect(Array.isArray(config.corsOrigin)).toBe(true);
    });

    it('should have geocode configuration', () => {
      expect(config.geocodeUrl).toBeDefined();
      expect(config.geocodeUserAgent).toBeDefined();
      expect(config.geocodeMinDelayMs).toBeGreaterThan(0);
    });
  });

  describe('Configuration Values', () => {
    it('should have valid port number', () => {
      expect(config.port).toBeGreaterThan(0);
      expect(config.port).toBeLessThan(65536);
    });

    it('should have valid geocode delay', () => {
      expect(config.geocodeMinDelayMs).toBeGreaterThanOrEqual(1000);
    });

    it('should have valid JWT expiration format', () => {
      expect(config.jwtExpiresIn).toMatch(/^\d+[smhd]$/);
    });
  });
});
