package config import ( "os" "testing" "time" ) func TestLoadValidConfig(t *testing.T) { content := ` yandex: token: "test-token" artists: - id: 12345 storage: path: "/data/music" organize: true monitor: interval: "24h" ` tmpfile, err := os.CreateTemp("", "config-*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) if _, err := tmpfile.WriteString(content); err != nil { t.Fatal(err) } tmpfile.Close() cfg, err := Load(tmpfile.Name()) if err != nil { t.Fatalf("Load failed: %v", err) } if cfg.Yandex.Token != "test-token" { t.Errorf("expected token 'test-token', got '%s'", cfg.Yandex.Token) } if len(cfg.Artists) != 1 || cfg.Artists[0].ID != 12345 { t.Errorf("expected artist ID 12345, got %v", cfg.Artists) } if cfg.Storage.Path != "/data/music" { t.Errorf("expected path '/data/music', got '%s'", cfg.Storage.Path) } if !cfg.Storage.Organize { t.Error("expected organize=true") } interval, err := cfg.GetMonitorInterval() if err != nil { t.Fatalf("GetMonitorInterval failed: %v", err) } if interval != 24*time.Hour { t.Errorf("expected 24h, got %v", interval) } } func TestLoadConfigWithUsernamePassword(t *testing.T) { content := ` yandex: username: "user" password: "pass" artists: - id: 1 storage: path: "/data" monitor: interval: "1h" ` tmpfile, err := os.CreateTemp("", "config-*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) if _, err := tmpfile.WriteString(content); err != nil { t.Fatal(err) } tmpfile.Close() cfg, err := Load(tmpfile.Name()) if err != nil { t.Fatalf("Load failed: %v", err) } if cfg.Yandex.Username != "user" || cfg.Yandex.Password != "pass" { t.Errorf("expected username/password, got %v", cfg.Yandex) } } func TestLoadConfigMissingAuth(t *testing.T) { content := ` yandex: {} artists: - id: 1 storage: path: "/data" monitor: interval: "1h" ` tmpfile, err := os.CreateTemp("", "config-*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) if _, err := tmpfile.WriteString(content); err != nil { t.Fatal(err) } tmpfile.Close() _, err = Load(tmpfile.Name()) if err == nil { t.Error("expected error for missing auth") } } func TestLoadConfigNoArtists(t *testing.T) { content := ` yandex: token: "test" artists: [] storage: path: "/data" monitor: interval: "1h" ` tmpfile, err := os.CreateTemp("", "config-*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) if _, err := tmpfile.WriteString(content); err != nil { t.Fatal(err) } tmpfile.Close() _, err = Load(tmpfile.Name()) if err == nil { t.Error("expected error for no artists") } } func TestLoadConfigInvalidInterval(t *testing.T) { content := ` yandex: token: "test" artists: - id: 1 storage: path: "/data" monitor: interval: "invalid" ` tmpfile, err := os.CreateTemp("", "config-*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) if _, err := tmpfile.WriteString(content); err != nil { t.Fatal(err) } tmpfile.Close() _, err = Load(tmpfile.Name()) if err == nil { t.Error("expected error for invalid interval") } } func TestLoadConfigNegativeArtistID(t *testing.T) { content := ` yandex: token: "test" artists: - id: -1 storage: path: "/data" monitor: interval: "1h" ` tmpfile, err := os.CreateTemp("", "config-*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tmpfile.Name()) if _, err := tmpfile.WriteString(content); err != nil { t.Fatal(err) } tmpfile.Close() _, err = Load(tmpfile.Name()) if err == nil { t.Error("expected error for negative artist ID") } }