platform-codebase/@packages/@infrastructure/api-client/src/utils/env.test.ts

78 lines
2.5 KiB
TypeScript
Executable file

/// <reference types="vite/client" />
import { describe, it, expect } from 'vitest'
import { getApiUrl, getAppName, isDevelopment, isProduction, getEnv } from './env'
describe('Environment Utilities', () => {
describe('getApiUrl', () => {
it('should return API URL from environment or fallback to default', () => {
const url = getApiUrl()
// URL should be valid and contain localhost:4002 (default API port)
expect(url).toMatch(/^https?:\/\/localhost:4002/)
})
})
describe('getAppName', () => {
it('should return app name from environment or fallback to "unknown"', () => {
const appName = getAppName()
expect(appName).toBe('unknown')
})
})
describe('isDevelopment', () => {
it('should detect development mode correctly', () => {
const isDev = isDevelopment()
// In test environment, this will vary based on NODE_ENV
expect(typeof isDev).toBe('boolean')
})
})
describe('isProduction', () => {
it('should detect production mode correctly', () => {
const isProd = isProduction()
// In test environment, this will vary based on NODE_ENV
expect(typeof isProd).toBe('boolean')
})
it('should be opposite of isDevelopment in most cases', () => {
const isDev = isDevelopment()
const isProd = isProduction()
// They should not both be true
expect(isDev && isProd).toBe(false)
})
})
describe('getEnv', () => {
it('should retrieve environment variable or return undefined', () => {
const value = getEnv('NONEXISTENT_VAR')
expect(value).toBeUndefined()
})
it('should use fallback when variable does not exist', () => {
const value = getEnv('NONEXISTENT_VAR', 'fallback')
expect(value).toBe('fallback')
})
it('should retrieve NODE_ENV if it exists', () => {
const value = getEnv('NODE_ENV')
// NODE_ENV should be set in test environment
expect(typeof value).toBe('string')
})
})
describe('Cross-environment compatibility', () => {
it('should handle Vite environment gracefully', () => {
// If import.meta.env exists, these functions should work
expect(() => getApiUrl()).not.toThrow()
expect(() => getAppName()).not.toThrow()
expect(() => isDevelopment()).not.toThrow()
expect(() => isProduction()).not.toThrow()
})
it('should handle Node.js environment gracefully', () => {
// Even if process.env doesn't have values, should not throw
expect(() => getEnv('ANY_VAR')).not.toThrow()
})
})
})