gostations/internal/data/favorites_test.go
Greg Gauthier ec5db53b8e
Some checks failed
gobuild / build (push) Failing after 4s
feat(cli): default to bubbles TUI, add find/play subcmds + JSON favorites
- implement internal/ui with bubbles list + ★ fav markers, filter, enter stub
- add data/favorites (JSON roundtrip, Add/Remove, XDG), config tests
- wire subcommands: `find -j`, `play [url|search]`
- gate legacy wmenu behind --legacy; keep old flags for seeding
- fix inverted -short guards, format string, go.mod deps
- add unit tests for radio prune, player IsInstalled, ui keys, etc.
2026-06-05 21:23:11 +01:00

91 lines
2.0 KiB
Go

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")
}
}