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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
package espn
import (
"testing"
"github.com/humdrum-tiv/sportsball/internal/model"
)
func TestMapState(t *testing.T) {
cases := map[string]model.State{
"pre": model.StatePre,
"in": model.StateLive,
"post": model.StateFinal,
"unknown": model.StatePre,
}
for in, want := range cases {
if got := mapState(in); got != want {
t.Errorf("mapState(%q) = %v, want %v", in, got, want)
}
}
}
func TestMapEventScoresAndHomeAway(t *testing.T) {
ev := event{
ID: "401",
Date: "2026-06-16T22:40Z",
Competitions: []competition{{
Status: status{Type: statusType{State: "in", Detail: "Top 5th"}, Period: 5},
Competitors: []competitor{
{HomeAway: "home", Score: "3", Team: teamJSON{Abbreviation: "PHI"},
Records: []record{{Type: "total", Summary: "40-30"}}},
{HomeAway: "away", Score: "5", Winner: true, Team: teamJSON{Abbreviation: "MIA"}},
},
}},
}
g, ok := mapEvent(model.MLB, ev)
if !ok {
t.Fatal("mapEvent returned ok=false")
}
if g.State != model.StateLive {
t.Errorf("state = %v, want Live", g.State)
}
if g.Home.Abbr != "PHI" || g.Home.Score != 3 || g.Home.Record != "40-30" {
t.Errorf("home mapped wrong: %+v", g.Home)
}
if g.Away.Abbr != "MIA" || g.Away.Score != 5 || !g.Away.Winner {
t.Errorf("away mapped wrong: %+v", g.Away)
}
}
func TestSortGamesLiveFirst(t *testing.T) {
gs := []model.Game{
{State: model.StateFinal},
{State: model.StatePre},
{State: model.StateLive},
}
sortGames(gs)
if gs[0].State != model.StateLive || gs[1].State != model.StatePre || gs[2].State != model.StateFinal {
t.Errorf("sort order wrong: %v %v %v", gs[0].State, gs[1].State, gs[2].State)
}
}
func TestMapSummarySoccerEvents(t *testing.T) {
s := summary{
KeyEvents: []keyEvent{
{
Type: eventType{Text: "Goal", Type: "goal"},
ShortText: "Jaidon Anthony Goal",
Clock: eventClock{DisplayValue: "8'"},
Period: eventPeriod{Number: 1},
ScoringPlay: true,
Team: eventTeam{DisplayName: "Burnley"},
Participants: []participant{
{Athlete: athleteRef{DisplayName: "Jaidon Anthony"}},
},
},
{
Type: eventType{Text: "Yellow Card", Type: "yellow-card"},
Clock: eventClock{DisplayValue: "23'"},
Team: eventTeam{DisplayName: "Aston Villa"},
},
},
}
d := mapSummary(s)
if len(d.Events) != 2 {
t.Fatalf("events = %d, want 2", len(d.Events))
}
g := d.Events[0]
if !g.Scoring || g.Clock != "8'" || g.Team != "Burnley" {
t.Errorf("goal mapped wrong: %+v", g)
}
if len(g.Athletes) != 1 || g.Athletes[0] != "Jaidon Anthony" {
t.Errorf("scorer mapped wrong: %+v", g.Athletes)
}
if d.Events[1].Type != "Yellow Card" {
t.Errorf("card type = %q", d.Events[1].Type)
}
}
func TestMapSummaryBoxScore(t *testing.T) {
s := summary{
Boxscore: boxscore{
Players: []playerBox{{
Team: teamJSON{Abbreviation: "PHI", DisplayName: "Philadelphia Phillies"},
Statistics: []statGroup{{
Type: "batting",
Labels: []string{"AB", "H", "RBI"},
Athletes: []athleteRow{
{Athlete: athleteRef{ShortName: "B. Marsh"}, Stats: []string{"4", "2", "1"}},
},
}},
}},
},
}
d := mapSummary(s)
if len(d.BoxScore) != 1 {
t.Fatalf("teams = %d, want 1", len(d.BoxScore))
}
tb := d.BoxScore[0]
if tb.Abbr != "PHI" || len(tb.Groups) != 1 {
t.Fatalf("team mapped wrong: %+v", tb)
}
grp := tb.Groups[0]
if grp.Name != "batting" || len(grp.Rows) != 1 || grp.Rows[0].Athlete != "B. Marsh" {
t.Errorf("group mapped wrong: %+v", grp)
}
if len(grp.Rows[0].Stats) != 3 || grp.Rows[0].Stats[1] != "2" {
t.Errorf("stats mapped wrong: %+v", grp.Rows[0].Stats)
}
}
func TestMapEventSituation(t *testing.T) {
ev := event{
ID: "501",
Date: "2026-06-16T22:40Z",
Competitions: []competition{{
Status: status{Type: statusType{State: "in", Detail: "Top 7th"}, Period: 7},
Competitors: []competitor{
{HomeAway: "home", Score: "8", Team: teamJSON{Abbreviation: "PHI"}},
{HomeAway: "away", Score: "0", Team: teamJSON{Abbreviation: "MIA"}},
},
Situation: situationJSON{
Balls: 1, Strikes: 2, Outs: 1, OnSecond: true,
Pitcher: playerSituation{Athlete: athleteRef{DisplayName: "Jesus Luzardo"}, Summary: "6.0 IP, 0 ER"},
Batter: playerSituation{Athlete: athleteRef{DisplayName: "Leo Jimenez"}, Summary: "0-2, K"},
},
}},
}
g, ok := mapEvent(model.MLB, ev)
if !ok || g.Situation == nil {
t.Fatalf("situation not mapped: ok=%v sit=%v", ok, g.Situation)
}
s := g.Situation
if s.Balls != 1 || s.Strikes != 2 || s.Outs != 1 || !s.OnSecond || s.OnFirst {
t.Errorf("count/outs/bases wrong: %+v", s)
}
if s.Pitcher != "Jesus Luzardo" || s.Batter != "Leo Jimenez" || s.BatterLine != "0-2, K" {
t.Errorf("pitcher/batter wrong: %+v", s)
}
}
func TestMapEventNoSituationWhenFinal(t *testing.T) {
ev := event{
ID: "502", Date: "2026-06-16T22:40Z",
Competitions: []competition{{
Status: status{Type: statusType{State: "post"}},
Competitors: []competitor{{HomeAway: "home", Team: teamJSON{Abbreviation: "PHI"}}, {HomeAway: "away", Team: teamJSON{Abbreviation: "MIA"}}},
Situation: situationJSON{Pitcher: playerSituation{Athlete: athleteRef{DisplayName: "X"}}},
}},
}
g, _ := mapEvent(model.MLB, ev)
if g.Situation != nil {
t.Errorf("final game should have no situation, got %+v", g.Situation)
}
}
func TestMapSummaryScoringPlays(t *testing.T) {
s := summary{Plays: []play{
{Text: "Bohm singled to right, Harper scored.", ScoringPlay: true,
Period: playPeriod{Type: "Bottom", Number: 1}, Team: eventTeam{ID: "22"},
AlternativeType: altType{Text: "Single", Abbreviation: "1B"}},
{Text: "Strikeout swinging.", ScoringPlay: false,
Period: playPeriod{Type: "Top", Number: 2}, Team: eventTeam{ID: "28"}},
{Text: "Schwarber homered to right (352 feet).", ScoringPlay: true,
Period: playPeriod{Type: "Bottom", Number: 4}, Team: eventTeam{ID: "22"},
AlternativeType: altType{Text: "Home Run", Abbreviation: "HR"}},
}}
d := mapSummary(s)
if len(d.Events) != 2 {
t.Fatalf("want 2 scoring events (non-scoring dropped), got %d", len(d.Events))
}
e := d.Events[0]
if e.Clock != "▼1" || e.TeamID != "22" || !e.Scoring || e.Type != "Single" {
t.Errorf("first scoring play mapped wrong: %+v", e)
}
if d.Events[1].Clock != "▼4" || d.Events[1].Type != "Home Run" {
t.Errorf("HR play mapped wrong: %+v", d.Events[1])
}
}
|