package config_test import ( "os" "path/filepath" "strings" "testing" "git.savin.nyc/alex/go-iar-notificator/config" ) func TestLoadConfig_Success(t *testing.T) { // Create a temporary config file tempDir := t.TempDir() configFile := filepath.Join(tempDir, "test_config.yml") yamlContent := ` credentials: secret_key: "test_secret" ttd_api_key: "test_api_key" directory: "/test/dir" watch_debounce_seconds: 5 logging: level: "INFO" ` err := os.WriteFile(configFile, []byte(yamlContent), 0644) if err != nil { t.Fatalf("Failed to create temp config file: %v", err) } // Test LoadConfig cfg, err := config.LoadConfig(configFile) if err != nil { t.Fatalf("Expected no error, got %v", err) } // Verify the config if cfg.Credentials.SecretKey != "test_secret" { t.Errorf("Expected SecretKey 'test_secret', got %s", cfg.Credentials.SecretKey) } if cfg.Credentials.TTDApiKey != "test_api_key" { t.Errorf("Expected TTDApiKey 'test_api_key', got %s", cfg.Credentials.TTDApiKey) } if cfg.Directory != "/test/dir" { t.Errorf("Expected Directory '/test/dir', got %s", cfg.Directory) } if cfg.WatchDebounceSeconds != 5 { t.Errorf("Expected WatchDebounceSeconds 5, got %d", cfg.WatchDebounceSeconds) } if cfg.Logger == nil { t.Error("Expected Logger to be set, got nil") } } func TestLoadConfig_FileNotFound(t *testing.T) { _, err := config.LoadConfig("/nonexistent/config.yml") if err == nil { t.Error("Expected error for non-existent file, got nil") } if !strings.Contains(err.Error(), "no such file") { t.Errorf("Expected error message to contain 'no such file', got %v", err) } } func TestLoadConfig_InvalidYAML(t *testing.T) { // Create a temporary file with invalid YAML tempDir := t.TempDir() configFile := filepath.Join(tempDir, "invalid_config.yml") invalidYAML := ` credentials: secret_key: "test" invalid yaml here ` err := os.WriteFile(configFile, []byte(invalidYAML), 0644) if err != nil { t.Fatalf("Failed to create temp config file: %v", err) } _, err = config.LoadConfig(configFile) if err == nil { t.Error("Expected error for invalid YAML, got nil") } if !strings.Contains(err.Error(), "yaml") { t.Errorf("Expected error message to contain 'yaml', got %v", err) } } func TestLoadConfig_MissingCredentials(t *testing.T) { // Create a temporary config file with missing credentials tempDir := t.TempDir() configFile := filepath.Join(tempDir, "missing_creds_config.yml") yamlContent := ` directory: "/test/dir" watch_debounce_seconds: 5 logging: level: "INFO" ` err := os.WriteFile(configFile, []byte(yamlContent), 0644) if err != nil { t.Fatalf("Failed to create temp config file: %v", err) } _, err = config.LoadConfig(configFile) if err == nil { t.Error("Expected error for missing credentials, got nil") } else if !strings.Contains(err.Error(), "missing required credentials") { t.Errorf("Expected error message to contain 'missing required credentials', got %v", err) } }