| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package main
- import (
- "os"
- "strings"
- "testing"
- )
- // TestConfig_LoadConfig tests configuration loading
- func TestConfig_LoadConfig(t *testing.T) {
- // Save original env vars
- origARPURL := os.Getenv("ARP_URL")
- origARPUsername := os.Getenv("ARP_USERNAME")
- origARPPassword := os.Getenv("ARP_PASSWORD")
- origOpenAIKey := os.Getenv("OPENAI_API_KEY")
- origOpenAIModel := os.Getenv("OPENAI_MODEL")
- origOpenAITemp := os.Getenv("OPENAI_TEMPERATURE")
- origOpenAIBaseURL := os.Getenv("OPENAI_BASE_URL")
- // Restore after test
- defer func() {
- os.Setenv("ARP_URL", origARPURL)
- os.Setenv("ARP_USERNAME", origARPUsername)
- os.Setenv("ARP_PASSWORD", origARPPassword)
- os.Setenv("OPENAI_API_KEY", origOpenAIKey)
- os.Setenv("OPENAI_MODEL", origOpenAIModel)
- os.Setenv("OPENAI_TEMPERATURE", origOpenAITemp)
- os.Setenv("OPENAI_BASE_URL", origOpenAIBaseURL)
- }()
- t.Run("ValidConfig", func(t *testing.T) {
- os.Setenv("ARP_URL", "http://localhost:8080")
- os.Setenv("ARP_USERNAME", "test@example.com")
- os.Setenv("ARP_PASSWORD", "testpass")
- os.Setenv("OPENAI_API_KEY", "test-key")
- os.Setenv("OPENAI_MODEL", "gpt-4")
- os.Setenv("OPENAI_TEMPERATURE", "0.7")
- os.Setenv("OPENAI_BASE_URL", "http://localhost:11434/v1")
- cfg, err := LoadConfig()
- if err != nil {
- t.Fatalf("LoadConfig failed: %v", err)
- }
- if cfg.ARPURL != "http://localhost:8080" {
- t.Errorf("Expected ARP_URL 'http://localhost:8080', got '%s'", cfg.ARPURL)
- }
- if cfg.ARPUsername != "test@example.com" {
- t.Errorf("Expected ARP_USERNAME 'test@example.com', got '%s'", cfg.ARPUsername)
- }
- if cfg.ARPPassword != "testpass" {
- t.Errorf("Expected ARP_PASSWORD 'testpass', got '%s'", cfg.ARPPassword)
- }
- if cfg.OpenAIKey != "test-key" {
- t.Errorf("Expected OpenAIKey 'test-key', got '%s'", cfg.OpenAIKey)
- }
- if cfg.OpenAIModel != "gpt-4" {
- t.Errorf("Expected OpenAIModel 'gpt-4', got '%s'", cfg.OpenAIModel)
- }
- if cfg.OpenAITemperature != 0.7 {
- t.Errorf("Expected OpenAITemperature 0.7, got %f", cfg.OpenAITemperature)
- }
- if cfg.OpenAIBaseURL != "http://localhost:11434/v1" {
- t.Errorf("Expected OpenAIBaseURL 'http://localhost:11434/v1', got '%s'", cfg.OpenAIBaseURL)
- }
- })
- t.Run("DefaultValues", func(t *testing.T) {
- os.Setenv("ARP_URL", "http://localhost:8080")
- os.Setenv("ARP_USERNAME", "test@example.com")
- os.Setenv("ARP_PASSWORD", "testpass")
- os.Setenv("OPENAI_API_KEY", "test-key")
- os.Unsetenv("OPENAI_MODEL")
- os.Unsetenv("OPENAI_TEMPERATURE")
- os.Unsetenv("OPENAI_BASE_URL")
- cfg, err := LoadConfig()
- if err != nil {
- t.Fatalf("LoadConfig failed: %v", err)
- }
- if cfg.OpenAIModel != "gpt-4" {
- t.Errorf("Expected default OpenAIModel 'gpt-4', got '%s'", cfg.OpenAIModel)
- }
- if cfg.OpenAITemperature != 0.0 {
- t.Errorf("Expected default OpenAITemperature 0.0, got %f", cfg.OpenAITemperature)
- }
- if cfg.OpenAIBaseURL != "" {
- t.Errorf("Expected default OpenAIBaseURL '', got '%s'", cfg.OpenAIBaseURL)
- }
- })
- t.Run("MissingARPURL", func(t *testing.T) {
- os.Unsetenv("ARP_URL")
- os.Setenv("ARP_USERNAME", "test@example.com")
- os.Setenv("ARP_PASSWORD", "testpass")
- os.Setenv("OPENAI_API_KEY", "test-key")
- _, err := LoadConfig()
- if err == nil {
- t.Error("Expected error for missing ARP_URL")
- }
- if !strings.Contains(err.Error(), "ARP_URL") {
- t.Errorf("Expected error to mention ARP_URL, got: %v", err)
- }
- })
- t.Run("MissingARPUsername", func(t *testing.T) {
- os.Setenv("ARP_URL", "http://localhost:8080")
- os.Unsetenv("ARP_USERNAME")
- os.Setenv("ARP_PASSWORD", "testpass")
- os.Setenv("OPENAI_API_KEY", "test-key")
- _, err := LoadConfig()
- if err == nil {
- t.Error("Expected error for missing ARP_USERNAME")
- }
- if !strings.Contains(err.Error(), "ARP_USERNAME") {
- t.Errorf("Expected error to mention ARP_USERNAME, got: %v", err)
- }
- })
- t.Run("MissingARPPassword", func(t *testing.T) {
- os.Setenv("ARP_URL", "http://localhost:8080")
- os.Setenv("ARP_USERNAME", "test@example.com")
- os.Unsetenv("ARP_PASSWORD")
- os.Setenv("OPENAI_API_KEY", "test-key")
- _, err := LoadConfig()
- if err == nil {
- t.Error("Expected error for missing ARP_PASSWORD")
- }
- if !strings.Contains(err.Error(), "ARP_PASSWORD") {
- t.Errorf("Expected error to mention ARP_PASSWORD, got: %v", err)
- }
- })
- t.Run("MissingOpenAIKey", func(t *testing.T) {
- os.Setenv("ARP_URL", "http://localhost:8080")
- os.Setenv("ARP_USERNAME", "test@example.com")
- os.Setenv("ARP_PASSWORD", "testpass")
- os.Unsetenv("OPENAI_API_KEY")
- _, err := LoadConfig()
- if err == nil {
- t.Error("Expected error for missing OPENAI_API_KEY")
- }
- if !strings.Contains(err.Error(), "OPENAI_API_KEY") {
- t.Errorf("Expected error to mention OPENAI_API_KEY, got: %v", err)
- }
- })
- t.Run("InvalidTemperature", func(t *testing.T) {
- os.Setenv("ARP_URL", "http://localhost:8080")
- os.Setenv("ARP_USERNAME", "test@example.com")
- os.Setenv("ARP_PASSWORD", "testpass")
- os.Setenv("OPENAI_API_KEY", "test-key")
- os.Setenv("OPENAI_TEMPERATURE", "invalid")
- cfg, err := LoadConfig()
- if err != nil {
- t.Fatalf("LoadConfig failed: %v", err)
- }
- // Should use default value when parsing fails
- if cfg.OpenAITemperature != 0.0 {
- t.Errorf("Expected default OpenAITemperature 0.0 for invalid input, got %f", cfg.OpenAITemperature)
- }
- })
- }
|