cricinfo-cli-go 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +63 -0
- package/CONTRIBUTORS.md +75 -0
- package/LICENSE +21 -0
- package/Makefile +131 -0
- package/README.md +130 -0
- package/bin/cricinfo.js +44 -0
- package/cmd/cricinfo/main.go +15 -0
- package/go.mod +10 -0
- package/go.sum +10 -0
- package/internal/app/app.go +11 -0
- package/internal/app/app_test.go +122 -0
- package/internal/buildinfo/buildinfo.go +16 -0
- package/internal/cli/analysis.go +262 -0
- package/internal/cli/analysis_test.go +175 -0
- package/internal/cli/competitions.go +154 -0
- package/internal/cli/competitions_test.go +165 -0
- package/internal/cli/leagues.go +297 -0
- package/internal/cli/leagues_test.go +194 -0
- package/internal/cli/matches.go +403 -0
- package/internal/cli/matches_test.go +413 -0
- package/internal/cli/players.go +263 -0
- package/internal/cli/players_test.go +384 -0
- package/internal/cli/root.go +141 -0
- package/internal/cli/search.go +119 -0
- package/internal/cli/teams.go +214 -0
- package/internal/cli/teams_test.go +192 -0
- package/internal/cricinfo/analysis.go +1401 -0
- package/internal/cricinfo/analysis_phase15_test.go +267 -0
- package/internal/cricinfo/client.go +471 -0
- package/internal/cricinfo/client_test.go +280 -0
- package/internal/cricinfo/cmd/fixture-refresh/main.go +145 -0
- package/internal/cricinfo/competitions.go +405 -0
- package/internal/cricinfo/competitions_phase13_test.go +234 -0
- package/internal/cricinfo/coverage_ledger.go +122 -0
- package/internal/cricinfo/coverage_ledger_test.go +253 -0
- package/internal/cricinfo/decode.go +115 -0
- package/internal/cricinfo/decode_test.go +100 -0
- package/internal/cricinfo/entity_index.go +618 -0
- package/internal/cricinfo/entity_index_test.go +175 -0
- package/internal/cricinfo/fixture_matrix.go +243 -0
- package/internal/cricinfo/fixture_matrix_test.go +49 -0
- package/internal/cricinfo/fixtures_test.go +264 -0
- package/internal/cricinfo/historical_hydration.go +1641 -0
- package/internal/cricinfo/historical_phase14_test.go +542 -0
- package/internal/cricinfo/leagues.go +1210 -0
- package/internal/cricinfo/leagues_phase12_test.go +324 -0
- package/internal/cricinfo/live_leagues_test.go +169 -0
- package/internal/cricinfo/live_matches_test.go +203 -0
- package/internal/cricinfo/live_matrix_test.go +118 -0
- package/internal/cricinfo/live_players_test.go +122 -0
- package/internal/cricinfo/live_search_test.go +86 -0
- package/internal/cricinfo/live_smoke_test.go +213 -0
- package/internal/cricinfo/live_teams_test.go +104 -0
- package/internal/cricinfo/matches.go +1508 -0
- package/internal/cricinfo/matches_phase7_test.go +207 -0
- package/internal/cricinfo/matches_phase9_test.go +253 -0
- package/internal/cricinfo/normalize_entities.go +1727 -0
- package/internal/cricinfo/normalize_leagues.go +346 -0
- package/internal/cricinfo/players.go +1332 -0
- package/internal/cricinfo/players_phase10_test.go +174 -0
- package/internal/cricinfo/players_phase11_test.go +373 -0
- package/internal/cricinfo/render_contract.go +1088 -0
- package/internal/cricinfo/render_phase4_test.go +633 -0
- package/internal/cricinfo/renderer.go +1689 -0
- package/internal/cricinfo/resolver.go +813 -0
- package/internal/cricinfo/resolver_test.go +244 -0
- package/internal/cricinfo/teams.go +603 -0
- package/internal/cricinfo/teams_phase8_test.go +231 -0
- package/internal/cricinfo/testdata/fixtures/README.md +43 -0
- package/internal/cricinfo/testdata/fixtures/aux-competition-metadata/broadcasts.json +11 -0
- package/internal/cricinfo/testdata/fixtures/aux-competition-metadata/officials.json +150 -0
- package/internal/cricinfo/testdata/fixtures/details-plays/detail-110.json +157 -0
- package/internal/cricinfo/testdata/fixtures/details-plays/detail-52545007.json +145 -0
- package/internal/cricinfo/testdata/fixtures/details-plays/detail-52559021.json +143 -0
- package/internal/cricinfo/testdata/fixtures/details-plays/plays.json +15 -0
- package/internal/cricinfo/testdata/fixtures/endpoint-matrix.tsv +19 -0
- package/internal/cricinfo/testdata/fixtures/innings-fow-partnerships/fow-1.json +12 -0
- package/internal/cricinfo/testdata/fixtures/innings-fow-partnerships/fow.json +42 -0
- package/internal/cricinfo/testdata/fixtures/innings-fow-partnerships/innings-1-2.json +38 -0
- package/internal/cricinfo/testdata/fixtures/innings-fow-partnerships/partnership-1.json +31 -0
- package/internal/cricinfo/testdata/fixtures/innings-fow-partnerships/partnerships.json +42 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/calendar-offdays.json +20 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/calendar-ondays.json +21 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/calendar.json +14 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/season-2025.json +13 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/season-group-1.json +13 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/season-groups.json +11 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/season-type-1.json +13 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/season-types.json +11 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/seasons.json +30 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/standings-item-1.json +72 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/standings-root.json +3 -0
- package/internal/cricinfo/testdata/fixtures/leagues-seasons-standings/standings.json +15 -0
- package/internal/cricinfo/testdata/fixtures/matches-competitions/competition.json +460 -0
- package/internal/cricinfo/testdata/fixtures/matches-competitions/event-1529474.json +86 -0
- package/internal/cricinfo/testdata/fixtures/matches-competitions/matchcards-1527966.json +368 -0
- package/internal/cricinfo/testdata/fixtures/matches-competitions/situation-1529474.json +10 -0
- package/internal/cricinfo/testdata/fixtures/players/athlete-1361257-statistics.json +126 -0
- package/internal/cricinfo/testdata/fixtures/players/athlete-1361257.json +113 -0
- package/internal/cricinfo/testdata/fixtures/players/roster-1361257-linescores-1-1-statistics-0.json +208 -0
- package/internal/cricinfo/testdata/fixtures/players/roster-1361257-linescores-1-2-statistics-0.json +252 -0
- package/internal/cricinfo/testdata/fixtures/players/roster-1361257-linescores.json +74 -0
- package/internal/cricinfo/testdata/fixtures/players/roster-1361257-statistics-0.json +1008 -0
- package/internal/cricinfo/testdata/fixtures/root-discovery/events.json +72 -0
- package/internal/cricinfo/testdata/fixtures/root-discovery/root.json +28 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/competitor-789643.json +40 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/leaders-789643.json +353 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/records-789643.json +91 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/roster-1147772-object.json +231 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/roster-1147772.json +235 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/roster-789643.json +322 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/scores-789643.json +19 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/statistics-789643.json +629 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/team-789643-athletes.json +7 -0
- package/internal/cricinfo/testdata/fixtures/team-competitor/team-789643.json +67 -0
- package/internal/cricinfo/testdata/golden/match-empty.golden +1 -0
- package/internal/cricinfo/testdata/golden/match-list.golden +2 -0
- package/internal/cricinfo/testdata/golden/match-partial.golden +3 -0
- package/internal/cricinfo/types.go +54 -0
- package/package.json +51 -0
- package/scripts/postinstall.js +153 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
package cricinfo
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"fmt"
|
|
6
|
+
"net/http"
|
|
7
|
+
"net/http/httptest"
|
|
8
|
+
"path/filepath"
|
|
9
|
+
"strings"
|
|
10
|
+
"sync/atomic"
|
|
11
|
+
"testing"
|
|
12
|
+
"time"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
func TestResolverSearchSupportsNumericAndKnownRef(t *testing.T) {
|
|
16
|
+
t.Parallel()
|
|
17
|
+
|
|
18
|
+
server, eventsHits := newResolverFixtureServer(t)
|
|
19
|
+
defer server.Close()
|
|
20
|
+
|
|
21
|
+
client, err := NewClient(Config{
|
|
22
|
+
BaseURL: server.URL,
|
|
23
|
+
MaxRetries: 0,
|
|
24
|
+
Timeout: 2 * time.Second,
|
|
25
|
+
})
|
|
26
|
+
if err != nil {
|
|
27
|
+
t.Fatalf("NewClient error: %v", err)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
resolver, err := NewResolver(ResolverConfig{
|
|
31
|
+
Client: client,
|
|
32
|
+
IndexPath: filepath.Join(t.TempDir(), "resolver-index.json"),
|
|
33
|
+
EventSeedTTL: time.Hour,
|
|
34
|
+
MaxEventSeed: 5,
|
|
35
|
+
})
|
|
36
|
+
if err != nil {
|
|
37
|
+
t.Fatalf("NewResolver error: %v", err)
|
|
38
|
+
}
|
|
39
|
+
defer func() {
|
|
40
|
+
_ = resolver.Close()
|
|
41
|
+
}()
|
|
42
|
+
|
|
43
|
+
players, err := resolver.Search(context.Background(), EntityPlayer, "51", ResolveOptions{Limit: 3})
|
|
44
|
+
if err != nil {
|
|
45
|
+
t.Fatalf("Search players error: %v", err)
|
|
46
|
+
}
|
|
47
|
+
if len(players.Entities) == 0 || players.Entities[0].ID != "51" {
|
|
48
|
+
t.Fatalf("expected player 51 from numeric resolution, got %+v", players.Entities)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
teams, err := resolver.Search(context.Background(), EntityTeam, server.URL+"/teams/31", ResolveOptions{Limit: 3})
|
|
52
|
+
if err != nil {
|
|
53
|
+
t.Fatalf("Search teams error: %v", err)
|
|
54
|
+
}
|
|
55
|
+
if len(teams.Entities) == 0 || teams.Entities[0].ID != "31" {
|
|
56
|
+
t.Fatalf("expected team 31 from known-ref resolution, got %+v", teams.Entities)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
leagues, err := resolver.Search(context.Background(), EntityLeague, "11", ResolveOptions{Limit: 3})
|
|
60
|
+
if err != nil {
|
|
61
|
+
t.Fatalf("Search leagues error: %v", err)
|
|
62
|
+
}
|
|
63
|
+
if len(leagues.Entities) == 0 || leagues.Entities[0].ID != "11" {
|
|
64
|
+
t.Fatalf("expected league 11 from numeric resolution, got %+v", leagues.Entities)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
matches, err := resolver.Search(context.Background(), EntityMatch, "41", ResolveOptions{Limit: 3, LeagueID: "11"})
|
|
68
|
+
if err != nil {
|
|
69
|
+
t.Fatalf("Search matches error: %v", err)
|
|
70
|
+
}
|
|
71
|
+
if len(matches.Entities) == 0 || matches.Entities[0].ID != "41" {
|
|
72
|
+
t.Fatalf("expected match 41 from numeric/context-aware resolution, got %+v", matches.Entities)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if got := eventsHits.Load(); got == 0 {
|
|
76
|
+
t.Fatalf("expected /events to be used for incremental seed")
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func TestResolverSearchReusesPersistedEventSeedToAvoidTransportChurn(t *testing.T) {
|
|
81
|
+
t.Parallel()
|
|
82
|
+
|
|
83
|
+
server, eventsHits := newResolverFixtureServer(t)
|
|
84
|
+
defer server.Close()
|
|
85
|
+
|
|
86
|
+
cachePath := filepath.Join(t.TempDir(), "resolver-index.json")
|
|
87
|
+
|
|
88
|
+
newResolverFromDisk := func() *Resolver {
|
|
89
|
+
t.Helper()
|
|
90
|
+
client, err := NewClient(Config{
|
|
91
|
+
BaseURL: server.URL,
|
|
92
|
+
MaxRetries: 0,
|
|
93
|
+
Timeout: 2 * time.Second,
|
|
94
|
+
})
|
|
95
|
+
if err != nil {
|
|
96
|
+
t.Fatalf("NewClient error: %v", err)
|
|
97
|
+
}
|
|
98
|
+
resolver, err := NewResolver(ResolverConfig{
|
|
99
|
+
Client: client,
|
|
100
|
+
IndexPath: cachePath,
|
|
101
|
+
EventSeedTTL: 24 * time.Hour,
|
|
102
|
+
MaxEventSeed: 5,
|
|
103
|
+
Now: func() time.Time { return time.Date(2026, 4, 10, 10, 0, 0, 0, time.UTC) },
|
|
104
|
+
})
|
|
105
|
+
if err != nil {
|
|
106
|
+
t.Fatalf("NewResolver error: %v", err)
|
|
107
|
+
}
|
|
108
|
+
return resolver
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
resolver1 := newResolverFromDisk()
|
|
112
|
+
if _, err := resolver1.Search(context.Background(), EntityMatch, "", ResolveOptions{Limit: 5}); err != nil {
|
|
113
|
+
t.Fatalf("first Search error: %v", err)
|
|
114
|
+
}
|
|
115
|
+
if err := resolver1.Close(); err != nil {
|
|
116
|
+
t.Fatalf("resolver1 Close error: %v", err)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
firstEventsHits := eventsHits.Load()
|
|
120
|
+
if firstEventsHits == 0 {
|
|
121
|
+
t.Fatalf("expected first search to hit /events")
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
resolver2 := newResolverFromDisk()
|
|
125
|
+
if _, err := resolver2.Search(context.Background(), EntityMatch, "", ResolveOptions{Limit: 5}); err != nil {
|
|
126
|
+
t.Fatalf("second Search error: %v", err)
|
|
127
|
+
}
|
|
128
|
+
if err := resolver2.Close(); err != nil {
|
|
129
|
+
t.Fatalf("resolver2 Close error: %v", err)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if got := eventsHits.Load(); got != firstEventsHits {
|
|
133
|
+
t.Fatalf("expected second search to reuse cached /events seed (hits=%d), got %d", firstEventsHits, got)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func TestResolverSearchHydratesNamelessCachedPlayerByID(t *testing.T) {
|
|
138
|
+
t.Parallel()
|
|
139
|
+
|
|
140
|
+
server, _ := newResolverFixtureServer(t)
|
|
141
|
+
defer server.Close()
|
|
142
|
+
|
|
143
|
+
client, err := NewClient(Config{
|
|
144
|
+
BaseURL: server.URL,
|
|
145
|
+
MaxRetries: 0,
|
|
146
|
+
Timeout: 2 * time.Second,
|
|
147
|
+
})
|
|
148
|
+
if err != nil {
|
|
149
|
+
t.Fatalf("NewClient error: %v", err)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
resolver, err := NewResolver(ResolverConfig{
|
|
153
|
+
Client: client,
|
|
154
|
+
IndexPath: filepath.Join(t.TempDir(), "resolver-index.json"),
|
|
155
|
+
EventSeedTTL: time.Hour,
|
|
156
|
+
MaxEventSeed: 5,
|
|
157
|
+
})
|
|
158
|
+
if err != nil {
|
|
159
|
+
t.Fatalf("NewResolver error: %v", err)
|
|
160
|
+
}
|
|
161
|
+
defer func() {
|
|
162
|
+
_ = resolver.Close()
|
|
163
|
+
}()
|
|
164
|
+
|
|
165
|
+
if err := resolver.index.Upsert(IndexedEntity{
|
|
166
|
+
Kind: EntityPlayer,
|
|
167
|
+
ID: "51",
|
|
168
|
+
UpdatedAt: time.Now().UTC(),
|
|
169
|
+
}); err != nil {
|
|
170
|
+
t.Fatalf("seed nameless player error: %v", err)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
result, err := resolver.Search(context.Background(), EntityPlayer, "51", ResolveOptions{Limit: 3})
|
|
174
|
+
if err != nil {
|
|
175
|
+
t.Fatalf("Search player by id error: %v", err)
|
|
176
|
+
}
|
|
177
|
+
if len(result.Entities) == 0 {
|
|
178
|
+
t.Fatalf("expected player search results")
|
|
179
|
+
}
|
|
180
|
+
if strings.TrimSpace(result.Entities[0].Name) == "" {
|
|
181
|
+
t.Fatalf("expected resolver to hydrate nameless cached player, got %+v", result.Entities[0])
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
func newResolverFixtureServer(t *testing.T) (*httptest.Server, *atomic.Int32) {
|
|
186
|
+
t.Helper()
|
|
187
|
+
|
|
188
|
+
eventsHits := &atomic.Int32{}
|
|
189
|
+
|
|
190
|
+
var server *httptest.Server
|
|
191
|
+
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
192
|
+
w.Header().Set("Content-Type", "application/json")
|
|
193
|
+
|
|
194
|
+
switch r.URL.Path {
|
|
195
|
+
case "/events":
|
|
196
|
+
eventsHits.Add(1)
|
|
197
|
+
_, _ = fmt.Fprintf(w, `{"count":1,"items":[{"$ref":"%s/leagues/11/events/21"}],"pageCount":1,"pageIndex":1,"pageSize":25}`,
|
|
198
|
+
server.URL,
|
|
199
|
+
)
|
|
200
|
+
case "/events/41":
|
|
201
|
+
w.WriteHeader(http.StatusNotFound)
|
|
202
|
+
_, _ = w.Write([]byte(`{"error":"not found"}`))
|
|
203
|
+
case "/leagues/11/events/21":
|
|
204
|
+
_, _ = fmt.Fprintf(w, `{"$ref":"%s/leagues/11/events/21","id":"21","shortDescription":"Fixture Event","leagues":[{"$ref":"%s/leagues/11","id":"11","name":"League Eleven"}],"competitions":[{"$ref":"%s/leagues/11/events/21/competitions/41","id":"41","description":"Fixture Match","shortDescription":"Match 41","competitors":[{"$ref":"%s/leagues/11/events/21/competitions/41/competitors/31","id":"31","team":{"$ref":"%s/teams/31"},"roster":{"$ref":"%s/leagues/11/events/21/competitions/41/competitors/31/roster"}}]}]}`,
|
|
205
|
+
server.URL,
|
|
206
|
+
server.URL,
|
|
207
|
+
server.URL,
|
|
208
|
+
server.URL,
|
|
209
|
+
server.URL,
|
|
210
|
+
server.URL,
|
|
211
|
+
)
|
|
212
|
+
case "/leagues/11/events/21/competitions/41":
|
|
213
|
+
_, _ = fmt.Fprintf(w, `{"$ref":"%s/leagues/11/events/21/competitions/41","id":"41","description":"Fixture Match","shortDescription":"Match 41","competitors":[{"$ref":"%s/leagues/11/events/21/competitions/41/competitors/31","id":"31","team":{"$ref":"%s/teams/31"},"roster":{"$ref":"%s/leagues/11/events/21/competitions/41/competitors/31/roster"}}]}`,
|
|
214
|
+
server.URL,
|
|
215
|
+
server.URL,
|
|
216
|
+
server.URL,
|
|
217
|
+
server.URL,
|
|
218
|
+
)
|
|
219
|
+
case "/leagues/11/events/21/competitions/41/competitors/31/roster":
|
|
220
|
+
_, _ = fmt.Fprintf(w, `{"entries":[{"playerId":"51","athlete":{"$ref":"%s/athletes/51"}}]}`,
|
|
221
|
+
server.URL,
|
|
222
|
+
)
|
|
223
|
+
case "/athletes/51":
|
|
224
|
+
_, _ = fmt.Fprintf(w, `{"$ref":"%s/athletes/51","id":"51","displayName":"Fixture Player","fullName":"Fixture Player"}`,
|
|
225
|
+
server.URL,
|
|
226
|
+
)
|
|
227
|
+
case "/teams/31":
|
|
228
|
+
_, _ = fmt.Fprintf(w, `{"$ref":"%s/teams/31","id":"31","displayName":"Fixture Team","shortDisplayName":"FTeam","abbreviation":"FT"}`,
|
|
229
|
+
server.URL,
|
|
230
|
+
)
|
|
231
|
+
case "/leagues/11":
|
|
232
|
+
_, _ = fmt.Fprintf(w, `{"$ref":"%s/leagues/11","id":"11","name":"League Eleven","slug":"league-eleven","abbreviation":"L11"}`,
|
|
233
|
+
server.URL,
|
|
234
|
+
)
|
|
235
|
+
default:
|
|
236
|
+
w.WriteHeader(http.StatusNotFound)
|
|
237
|
+
_, _ = fmt.Fprintf(w, `{"error":"unexpected path %s"}`,
|
|
238
|
+
r.URL.Path,
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
}))
|
|
242
|
+
|
|
243
|
+
return server, eventsHits
|
|
244
|
+
}
|