1
0

config_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package main
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. // TestConfig_LoadConfig tests configuration loading
  8. func TestConfig_LoadConfig(t *testing.T) {
  9. // Save original env vars
  10. origARPURL := os.Getenv("ARP_URL")
  11. origARPUsername := os.Getenv("ARP_USERNAME")
  12. origARPPassword := os.Getenv("ARP_PASSWORD")
  13. origOpenAIKey := os.Getenv("OPENAI_API_KEY")
  14. origOpenAIModel := os.Getenv("OPENAI_MODEL")
  15. origOpenAITemp := os.Getenv("OPENAI_TEMPERATURE")
  16. origOpenAIBaseURL := os.Getenv("OPENAI_BASE_URL")
  17. // Restore after test
  18. defer func() {
  19. os.Setenv("ARP_URL", origARPURL)
  20. os.Setenv("ARP_USERNAME", origARPUsername)
  21. os.Setenv("ARP_PASSWORD", origARPPassword)
  22. os.Setenv("OPENAI_API_KEY", origOpenAIKey)
  23. os.Setenv("OPENAI_MODEL", origOpenAIModel)
  24. os.Setenv("OPENAI_TEMPERATURE", origOpenAITemp)
  25. os.Setenv("OPENAI_BASE_URL", origOpenAIBaseURL)
  26. }()
  27. t.Run("ValidConfig", func(t *testing.T) {
  28. os.Setenv("ARP_URL", "http://localhost:8080")
  29. os.Setenv("ARP_USERNAME", "test@example.com")
  30. os.Setenv("ARP_PASSWORD", "testpass")
  31. os.Setenv("OPENAI_API_KEY", "test-key")
  32. os.Setenv("OPENAI_MODEL", "gpt-4")
  33. os.Setenv("OPENAI_TEMPERATURE", "0.7")
  34. os.Setenv("OPENAI_BASE_URL", "http://localhost:11434/v1")
  35. cfg, err := LoadConfig()
  36. if err != nil {
  37. t.Fatalf("LoadConfig failed: %v", err)
  38. }
  39. if cfg.ARPURL != "http://localhost:8080" {
  40. t.Errorf("Expected ARP_URL 'http://localhost:8080', got '%s'", cfg.ARPURL)
  41. }
  42. if cfg.ARPUsername != "test@example.com" {
  43. t.Errorf("Expected ARP_USERNAME 'test@example.com', got '%s'", cfg.ARPUsername)
  44. }
  45. if cfg.ARPPassword != "testpass" {
  46. t.Errorf("Expected ARP_PASSWORD 'testpass', got '%s'", cfg.ARPPassword)
  47. }
  48. if cfg.OpenAIKey != "test-key" {
  49. t.Errorf("Expected OpenAIKey 'test-key', got '%s'", cfg.OpenAIKey)
  50. }
  51. if cfg.OpenAIModel != "gpt-4" {
  52. t.Errorf("Expected OpenAIModel 'gpt-4', got '%s'", cfg.OpenAIModel)
  53. }
  54. if cfg.OpenAITemperature != 0.7 {
  55. t.Errorf("Expected OpenAITemperature 0.7, got %f", cfg.OpenAITemperature)
  56. }
  57. if cfg.OpenAIBaseURL != "http://localhost:11434/v1" {
  58. t.Errorf("Expected OpenAIBaseURL 'http://localhost:11434/v1', got '%s'", cfg.OpenAIBaseURL)
  59. }
  60. })
  61. t.Run("DefaultValues", func(t *testing.T) {
  62. os.Setenv("ARP_URL", "http://localhost:8080")
  63. os.Setenv("ARP_USERNAME", "test@example.com")
  64. os.Setenv("ARP_PASSWORD", "testpass")
  65. os.Setenv("OPENAI_API_KEY", "test-key")
  66. os.Unsetenv("OPENAI_MODEL")
  67. os.Unsetenv("OPENAI_TEMPERATURE")
  68. os.Unsetenv("OPENAI_BASE_URL")
  69. cfg, err := LoadConfig()
  70. if err != nil {
  71. t.Fatalf("LoadConfig failed: %v", err)
  72. }
  73. if cfg.OpenAIModel != "gpt-4" {
  74. t.Errorf("Expected default OpenAIModel 'gpt-4', got '%s'", cfg.OpenAIModel)
  75. }
  76. if cfg.OpenAITemperature != 0.0 {
  77. t.Errorf("Expected default OpenAITemperature 0.0, got %f", cfg.OpenAITemperature)
  78. }
  79. if cfg.OpenAIBaseURL != "" {
  80. t.Errorf("Expected default OpenAIBaseURL '', got '%s'", cfg.OpenAIBaseURL)
  81. }
  82. })
  83. t.Run("MissingARPURL", func(t *testing.T) {
  84. os.Unsetenv("ARP_URL")
  85. os.Setenv("ARP_USERNAME", "test@example.com")
  86. os.Setenv("ARP_PASSWORD", "testpass")
  87. os.Setenv("OPENAI_API_KEY", "test-key")
  88. _, err := LoadConfig()
  89. if err == nil {
  90. t.Error("Expected error for missing ARP_URL")
  91. }
  92. if !strings.Contains(err.Error(), "ARP_URL") {
  93. t.Errorf("Expected error to mention ARP_URL, got: %v", err)
  94. }
  95. })
  96. t.Run("MissingARPUsername", func(t *testing.T) {
  97. os.Setenv("ARP_URL", "http://localhost:8080")
  98. os.Unsetenv("ARP_USERNAME")
  99. os.Setenv("ARP_PASSWORD", "testpass")
  100. os.Setenv("OPENAI_API_KEY", "test-key")
  101. _, err := LoadConfig()
  102. if err == nil {
  103. t.Error("Expected error for missing ARP_USERNAME")
  104. }
  105. if !strings.Contains(err.Error(), "ARP_USERNAME") {
  106. t.Errorf("Expected error to mention ARP_USERNAME, got: %v", err)
  107. }
  108. })
  109. t.Run("MissingARPPassword", func(t *testing.T) {
  110. os.Setenv("ARP_URL", "http://localhost:8080")
  111. os.Setenv("ARP_USERNAME", "test@example.com")
  112. os.Unsetenv("ARP_PASSWORD")
  113. os.Setenv("OPENAI_API_KEY", "test-key")
  114. _, err := LoadConfig()
  115. if err == nil {
  116. t.Error("Expected error for missing ARP_PASSWORD")
  117. }
  118. if !strings.Contains(err.Error(), "ARP_PASSWORD") {
  119. t.Errorf("Expected error to mention ARP_PASSWORD, got: %v", err)
  120. }
  121. })
  122. t.Run("MissingOpenAIKey", func(t *testing.T) {
  123. os.Setenv("ARP_URL", "http://localhost:8080")
  124. os.Setenv("ARP_USERNAME", "test@example.com")
  125. os.Setenv("ARP_PASSWORD", "testpass")
  126. os.Unsetenv("OPENAI_API_KEY")
  127. _, err := LoadConfig()
  128. if err == nil {
  129. t.Error("Expected error for missing OPENAI_API_KEY")
  130. }
  131. if !strings.Contains(err.Error(), "OPENAI_API_KEY") {
  132. t.Errorf("Expected error to mention OPENAI_API_KEY, got: %v", err)
  133. }
  134. })
  135. t.Run("InvalidTemperature", func(t *testing.T) {
  136. os.Setenv("ARP_URL", "http://localhost:8080")
  137. os.Setenv("ARP_USERNAME", "test@example.com")
  138. os.Setenv("ARP_PASSWORD", "testpass")
  139. os.Setenv("OPENAI_API_KEY", "test-key")
  140. os.Setenv("OPENAI_TEMPERATURE", "invalid")
  141. cfg, err := LoadConfig()
  142. if err != nil {
  143. t.Fatalf("LoadConfig failed: %v", err)
  144. }
  145. // Should use default value when parsing fails
  146. if cfg.OpenAITemperature != 0.0 {
  147. t.Errorf("Expected default OpenAITemperature 0.0 for invalid input, got %f", cfg.OpenAITemperature)
  148. }
  149. })
  150. }