package data import ( "path/filepath" "testing" "github.com/gmgauthier/gostations/internal/radio" ) func TestFavorites_JSONRoundtrip(t *testing.T) { dir := t.TempDir() fpath := filepath.Join(dir, "favorites.json") f := &Favorites{ stations: make(map[string]radio.Station), path: fpath, } s1 := radio.Station{Name: "Test FM", Url: "http://example.com/stream1", Codec: "MP3"} s2 := radio.Station{Name: "Jazz 24", Url: "http://example.com/jazz", Codec: "AAC"} f.Add(s1) f.Add(s2) f.Add(s1) // dedup if err := f.Save(); err != nil { t.Fatalf("save: %v", err) } // reload f2 := &Favorites{ stations: make(map[string]radio.Station), path: fpath, } if err := f2.load(); err != nil { t.Fatalf("load: %v", err) } list := f2.List() if len(list) != 2 { t.Errorf("expected 2 stations after reload, got %d", len(list)) } if !f2.Contains("http://example.com/stream1") || !f2.Contains("http://example.com/jazz") { t.Error("contains check failed after roundtrip") } // remove f2.Remove("http://example.com/jazz") if f2.Contains("http://example.com/jazz") { t.Error("remove did not take effect") } if err := f2.Save(); err != nil { t.Fatal(err) } // fresh load f3 := &Favorites{stations: make(map[string]radio.Station), path: fpath} _ = f3.load() if len(f3.List()) != 1 { t.Errorf("expected 1 after remove+reload, got %d", len(f3.List())) } } func TestFavorites_EmptyAndMissing(t *testing.T) { dir := t.TempDir() fpath := filepath.Join(dir, "nonexistent-favs.json") f := &Favorites{stations: make(map[string]radio.Station), path: fpath} if err := f.load(); err != nil { t.Errorf("load of missing should not error, got %v", err) } if len(f.List()) != 0 { t.Error("expected empty list") } } func TestFavorites_AddRemoveIdempotent(t *testing.T) { f := &Favorites{stations: make(map[string]radio.Station), path: "/tmp/ignore.json"} s := radio.Station{Url: "http://x"} f.Add(s) f.Add(s) f.Remove("http://x") f.Remove("http://x") if len(f.List()) != 0 { t.Error("expected empty after remove") } }