package config import ( "os" "path/filepath" "testing" ) func writeConfig(t *testing.T, body string) { t.Helper() dir := t.TempDir() t.Setenv("XDG_CONFIG_HOME", dir) if err := os.MkdirAll(filepath.Join(dir, "terdut-tui"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "terdut-tui", "config.yaml"), []byte(body), 0o600); err != nil { t.Fatal(err) } } func TestLoad_TeamIsOptional(t *testing.T) { writeConfig(t, "server_url: https://terdut.example.com\n") cfg, err := Load() if err != nil { t.Fatalf("load: %v", err) } if cfg.Team != "" { t.Errorf("expected no default team, got %q", cfg.Team) } writeConfig(t, "server_url: https://terdut.example.com\nteam: Ops\n") cfg, err = Load() if err != nil { t.Fatalf("load: %v", err) } if cfg.Team != "Ops" { t.Errorf("expected team Ops, got %q", cfg.Team) } } // Signing in replaced the API key, so a config that has only a server URL is // complete, and one that still carries an api_key is noted rather than refused. func TestLoad_NoAPIKeyNeeded(t *testing.T) { writeConfig(t, "server_url: https://terdut.example.com\nusername: niklas\n") cfg, err := Load() if err != nil { t.Fatalf("load: %v", err) } if cfg.Username != "niklas" || cfg.LegacyAPIKey { t.Errorf("unexpected config %+v", cfg) } writeConfig(t, "server_url: https://terdut.example.com\napi_key: old\n") cfg, err = Load() if err != nil { t.Fatalf("a leftover api_key must not stop the TUI starting: %v", err) } if !cfg.LegacyAPIKey { t.Error("expected the leftover api_key to be noted") } }