package ui import ( "testing" "time" "github.com/humdrum-tiv/sportsball/internal/config" "github.com/humdrum-tiv/sportsball/internal/model" ) func TestFavKeyUsesIDThenAbbr(t *testing.T) { if got := favKey(model.MLB, model.Team{ID: "22", Abbr: "PHI"}); got != "mlb:22" { t.Errorf("with id = %q, want mlb:22", got) } if got := favKey(model.MLB, model.Team{Abbr: "PHI"}); got != "mlb:PHI" { t.Errorf("no id = %q, want mlb:PHI", got) } } func TestLoadFavoritesAndHasFav(t *testing.T) { cfg := config.Config{Favorites: []config.FavTeam{{League: "mlb", ID: "22", Abbr: "PHI"}}} a := App{favs: loadFavorites(cfg)} favGame := model.Game{League: model.MLB, Home: model.Team{ID: "22"}, Away: model.Team{ID: "99"}} otherGame := model.Game{League: model.MLB, Home: model.Team{ID: "1"}, Away: model.Team{ID: "2"}} if !a.hasFav(favGame) { t.Error("favGame should be favorite") } if a.hasFav(otherGame) { t.Error("otherGame should not be favorite") } } func TestFavoriteHighlightsPicksLiveNextLast(t *testing.T) { now := time.Now() fav := model.Team{ID: "22", Abbr: "PHI"} other := model.Team{ID: "99", Abbr: "NYM"} games := []model.Game{ {ID: "old", League: model.MLB, State: model.StateFinal, Home: fav, Start: now.AddDate(0, 0, -3)}, {ID: "recent", League: model.MLB, State: model.StateFinal, Away: fav, Start: now.AddDate(0, 0, -1)}, {ID: "live", League: model.MLB, State: model.StateLive, Home: fav, Start: now}, {ID: "soon", League: model.MLB, State: model.StatePre, Away: fav, Start: now.AddDate(0, 0, 1)}, {ID: "later", League: model.MLB, State: model.StatePre, Home: fav, Start: now.AddDate(0, 0, 4)}, {ID: "nofav", League: model.MLB, State: model.StatePre, Home: other, Away: other, Start: now}, } a := App{ games: map[model.LeagueID][]model.Game{model.MLB: games}, favs: map[string]bool{favKey(model.MLB, fav): true}, } got := a.favoriteHighlights(model.MLB, now) ids := map[string]bool{} for _, g := range got { ids[g.ID] = true } // live + most-recent final + next scheduled, nothing else. want := []string{"live", "recent", "soon"} for _, w := range want { if !ids[w] { t.Errorf("missing %q in highlights %v", w, ids) } } for _, bad := range []string{"old", "later", "nofav"} { if ids[bad] { t.Errorf("unexpected %q in highlights", bad) } } if len(got) != 3 { t.Errorf("got %d highlights, want 3: %v", len(got), ids) } }