import { getEnv, getEnvVar, isBrowser, isNode, isTest, isProduction, type EnvConfig, } from '../env'; describe('Environment Utilities', () => { describe('getEnv', () => { test('should return an object', () => { const env = getEnv(); expect(typeof env).toBe('object'); expect(env).not.toBeNull(); }); test('should return consistent results on multiple calls', () => { const env1 = getEnv(); const env2 = getEnv(); expect(env1).toEqual(env2); }); }); describe('getEnvVar', () => { test('should return fallback when variable does not exist', () => { const fallback = 'default_value'; const value = getEnvVar('DEFINITELY_NON_EXISTENT_VAR_12345', fallback); expect(value).toBe(fallback); }); test('should return undefined when no fallback provided and variable does not exist', () => { const value = getEnvVar('DEFINITELY_NON_EXISTENT_VAR_12345'); expect(value).toBeUndefined(); }); test('should handle empty string fallback', () => { const value = getEnvVar('DEFINITELY_NON_EXISTENT_VAR_12345', ''); expect(value).toBe(''); }); }); describe('isBrowser', () => { test('should return a boolean', () => { const result = isBrowser(); expect(typeof result).toBe('boolean'); }); test('should be consistent across calls', () => { const result1 = isBrowser(); const result2 = isBrowser(); expect(result1).toBe(result2); }); }); describe('isNode', () => { test('should return a boolean', () => { const result = isNode(); expect(typeof result).toBe('boolean'); }); test('should be consistent across calls', () => { const result1 = isNode(); const result2 = isNode(); expect(result1).toBe(result2); }); test('should return true in Jest test environment', () => { // Jest runs in Node.js, so this should be true const result = isNode(); expect(result).toBe(true); }); }); describe('isTest', () => { test('should return a boolean', () => { const result = isTest(); expect(typeof result).toBe('boolean'); }); test('should return true in Jest test environment', () => { // We are running in Jest, so this should be true const result = isTest(); expect(result).toBe(true); }); }); describe('isProduction', () => { test('should return a boolean', () => { const result = isProduction(); expect(typeof result).toBe('boolean'); }); test('should return false in test environment', () => { // We are running tests, so this should not be production const result = isProduction(); expect(result).toBe(false); }); }); describe('EnvConfig type', () => { test('should accept valid configuration object', () => { const config: EnvConfig = { VITE_COUCHDB_URL: 'http://localhost:5984', VITE_COUCHDB_USERNAME: 'admin', VITE_COUCHDB_PASSWORD: 'password', VITE_MAILGUN_API_KEY: 'test-key', VITE_MAILGUN_DOMAIN: 'test.mailgun.org', NODE_ENV: 'test', CUSTOM_VAR: 'custom_value', }; expect(config.VITE_COUCHDB_URL).toBe('http://localhost:5984'); expect(config.NODE_ENV).toBe('test'); expect(config.CUSTOM_VAR).toBe('custom_value'); }); test('should handle undefined values', () => { const config: EnvConfig = { DEFINED_VAR: 'value', UNDEFINED_VAR: undefined, }; expect(config.DEFINED_VAR).toBe('value'); expect(config.UNDEFINED_VAR).toBeUndefined(); }); test('should allow dynamic key access', () => { const config: EnvConfig = { TEST_KEY: 'test_value', }; const key = 'TEST_KEY'; expect(config[key]).toBe('test_value'); }); }); describe('integration scenarios', () => { test('should work together consistently', () => { const env = getEnv(); const isTestEnv = isTest(); const isProdEnv = isProduction(); const isBrowserEnv = isBrowser(); const isNodeEnv = isNode(); // Basic consistency checks expect(typeof env).toBe('object'); expect(typeof isTestEnv).toBe('boolean'); expect(typeof isProdEnv).toBe('boolean'); expect(typeof isBrowserEnv).toBe('boolean'); expect(typeof isNodeEnv).toBe('boolean'); // In Jest test environment, we expect certain conditions expect(isTestEnv).toBe(true); expect(isProdEnv).toBe(false); expect(isNodeEnv).toBe(true); }); test('should handle missing environment gracefully', () => { const nonExistentVar = getEnvVar('DEFINITELY_NON_EXISTENT_VAR_XYZ_123'); expect(nonExistentVar).toBeUndefined(); const withFallback = getEnvVar( 'DEFINITELY_NON_EXISTENT_VAR_XYZ_123', 'fallback' ); expect(withFallback).toBe('fallback'); }); }); describe('edge cases', () => { test('should handle empty string environment variable names', () => { const value = getEnvVar(''); expect(value).toBeUndefined(); }); test('should handle whitespace in variable names', () => { const value = getEnvVar(' NON_EXISTENT '); expect(value).toBeUndefined(); }); test('should handle null fallback', () => { const value = getEnvVar('NON_EXISTENT', null as any); expect(value).toBeNull(); }); test('should handle numeric fallback', () => { const value = getEnvVar('NON_EXISTENT', 42 as any); expect(value).toBe(42); }); test('should handle boolean fallback', () => { const value = getEnvVar('NON_EXISTENT', true as any); expect(value).toBe(true); }); }); });