Files
gitblog/internal/config/config_test.go
2025-09-15 04:02:11 +03:00

83 lines
2.0 KiB
Go

package config
import (
"os"
"testing"
"time"
)
func TestLoad(t *testing.T) {
// Set test environment variables
os.Setenv("GITHUB_TOKEN", "test-token")
os.Setenv("GITHUB_OWNER", "test-owner")
os.Setenv("GITHUB_REPO", "test-repo")
os.Setenv("PORT", "9090")
os.Setenv("CACHE_DURATION", "30m")
os.Setenv("UPDATE_INTERVAL", "10m")
os.Setenv("DEFAULT_THEME", "dark")
os.Setenv("BASE_URL", "https://example.com")
cfg, err := Load()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if cfg.GitHubToken != "test-token" {
t.Errorf("Expected GitHubToken to be 'test-token', got %s", cfg.GitHubToken)
}
if cfg.Port != "9090" {
t.Errorf("Expected Port to be '9090', got %s", cfg.Port)
}
if cfg.CacheDuration != 30*time.Minute {
t.Errorf("Expected CacheDuration to be 30m, got %v", cfg.CacheDuration)
}
if cfg.Theme != "dark" {
t.Errorf("Expected Theme to be 'dark', got %s", cfg.Theme)
}
if cfg.BaseURL != "https://example.com" {
t.Errorf("Expected BaseURL to be 'https://example.com', got %s", cfg.BaseURL)
}
}
func TestLoadMissingRequiredVars(t *testing.T) {
// Clear environment variables
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITHUB_OWNER")
os.Unsetenv("GITHUB_REPO")
_, err := Load()
if err == nil {
t.Fatal("Expected error for missing required variables")
}
}
func TestGetEnv(t *testing.T) {
os.Setenv("TEST_VAR", "test-value")
defer os.Unsetenv("TEST_VAR")
if got := getEnv("TEST_VAR", "default"); got != "test-value" {
t.Errorf("Expected 'test-value', got %s", got)
}
if got := getEnv("NONEXISTENT_VAR", "default"); got != "default" {
t.Errorf("Expected 'default', got %s", got)
}
}
func TestGetDurationEnv(t *testing.T) {
os.Setenv("TEST_DURATION", "5m")
defer os.Unsetenv("TEST_DURATION")
if got := getDurationEnv("TEST_DURATION", 1*time.Minute); got != 5*time.Minute {
t.Errorf("Expected 5m, got %v", got)
}
if got := getDurationEnv("INVALID_DURATION", 1*time.Minute); got != 1*time.Minute {
t.Errorf("Expected 1m, got %v", got)
}
}