package config import ( "strings" "testing" "time" "gopkg.in/yaml.v3" ) func TestPromptkitDefaultsAndYAML(t *testing.T) { cfg := Defaults() if cfg.Promptkit.Timeout != 2*time.Minute || cfg.Promptkit.Local.ConcurrencyLimit != 1 { t.Fatalf("Promptkit defaults = %#v", cfg.Promptkit) } if err := yaml.Unmarshal([]byte(` promptkit: profile: selected profile_file: /etc/weatherreporter/profile.yml timeout: 45s local: endpoint: http://127.0.0.1:8080 concurrency_limit: 0 `), &cfg); err != nil { t.Fatalf("Unmarshal() error = %v", err) } if cfg.Promptkit.Profile != "selected" || cfg.Promptkit.ProfileFile != "/etc/weatherreporter/profile.yml" || cfg.Promptkit.Timeout != 45*time.Second || cfg.Promptkit.Local.Endpoint != "http://127.0.0.1:8080" || cfg.Promptkit.Local.ConcurrencyLimit != 0 { t.Fatalf("Promptkit YAML = %#v", cfg.Promptkit) } if err := Validate(cfg); err != nil { t.Fatalf("Validate() error = %v", err) } } func TestValidatePromptkit(t *testing.T) { tests := []struct { name string mutate func(*PromptkitConfig) wantErr string }{ { name: "profile sources conflict", mutate: func(cfg *PromptkitConfig) { cfg.ProfileFile = "profile.yml" cfg.ProfileDir = "profiles" }, wantErr: "profile_file", }, { name: "nonpositive timeout", mutate: func(cfg *PromptkitConfig) { cfg.Timeout = 0 }, wantErr: "timeout", }, { name: "invalid local endpoint", mutate: func(cfg *PromptkitConfig) { cfg.Local.Endpoint = "not a URL" }, wantErr: "local.endpoint", }, { name: "negative local concurrency", mutate: func(cfg *PromptkitConfig) { cfg.Local.ConcurrencyLimit = -1 }, wantErr: "concurrency_limit", }, { name: "unlimited local concurrency", mutate: func(cfg *PromptkitConfig) { cfg.Local.Endpoint = "http://127.0.0.1:8080" cfg.Local.ConcurrencyLimit = 0 }, }, { name: "unregistered local backend", mutate: func(cfg *PromptkitConfig) { cfg.Local.Endpoint = "" cfg.Local.ConcurrencyLimit = 1 }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { cfg := Defaults() test.mutate(&cfg.Promptkit) err := Validate(cfg) if test.wantErr == "" { if err != nil { t.Fatalf("Validate() error = %v", err) } return } if err == nil || !strings.Contains(err.Error(), test.wantErr) { t.Fatalf("Validate() error = %v, want %q", err, test.wantErr) } }) } }