▍ humdrum codex / sportsball v0.1.0
license AGPL-3.0
2.3 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)
	}
}