internal/providers/provider_edge_live_test.go
393 lines · 12320 bytes
13 symbols in this file
1package providers_test
2
3import (
4 "fmt"
5 "net/http"
6 "os"
7 "sort"
8 "strings"
9 "testing"
10 "time"
11
12 "github.com/wraient/curd/internal/curdhost"
13 _ "github.com/wraient/curd/internal/loadproviders"
14 "github.com/wraient/curd/internal/providers"
15 "github.com/wraient/curd/internal/providers/anineko"
16 "github.com/wraient/curd/internal/providers/anipub"
17 "github.com/wraient/curd/internal/providers/senshi"
18)
19
20// Run with:
21//
22// CURD_LIVE_EDGE=1 go test ./internal/providers -run TestLiveProviderEdgeCases -v -timeout 45m
23func initLiveEdge(tb testing.TB) {
24 tb.Helper()
25 if os.Getenv("CURD_LIVE_EDGE") == "" {
26 tb.Skip("set CURD_LIVE_EDGE=1 to run live provider edge-case tests")
27 }
28 if curdhost.HTTPClient == nil {
29 curdhost.HTTPClient = func() *http.Client { return http.DefaultClient }
30 }
31}
32
33type edgeAnimeCase struct {
34 name string
35 query string
36 wantInTitle string
37}
38
39var edgeAnimeCases = []edgeAnimeCase{
40 // punctuation & symbols (how users actually type)
41 {name: "colon_apostrophe", query: "frieren beyond journey's end", wantInTitle: "frieren"},
42 {name: "colon_short", query: "re:zero", wantInTitle: "re:zero"},
43 {name: "colon_spaced", query: "re zero", wantInTitle: "re:zero"},
44 {name: "semicolon", query: "steins;gate", wantInTitle: "stein"},
45 {name: "slash", query: "fate/stay night", wantInTitle: "fate"},
46 {name: "bang", query: "k-on!", wantInTitle: "k-on"},
47 {name: "double_bang", query: "love live superstar", wantInTitle: "love live"},
48
49 // apostrophe variants
50 {name: "apostrophe_full", query: "JoJo's Bizarre Adventure", wantInTitle: "jojo"},
51 {name: "apostrophe_stripped", query: "jojos bizarre adventure", wantInTitle: "jojo"},
52
53 // romaji vs english
54 {name: "romaji_snk", query: "shingeki no kyojin", wantInTitle: "attack on titan"},
55 {name: "english_aot", query: "attack on titan", wantInTitle: "attack on titan"},
56 {name: "romaji_mha", query: "boku no hero academia", wantInTitle: "hero academia"},
57 {name: "english_mha", query: "my hero academia", wantInTitle: "hero academia"},
58 {name: "romaji_frieren", query: "sousou no frieren", wantInTitle: "frieren"},
59
60 // short / numeric queries
61 {name: "numeric_86", query: "86", wantInTitle: "86"},
62 {name: "two_digit", query: "91 days", wantInTitle: "91 days"},
63 {name: "three_letters", query: "mha", wantInTitle: "hero"},
64
65 // x-in-title
66 {name: "x_hxh", query: "hunter x hunter", wantInTitle: "hunter"},
67 {name: "x_spy", query: "spy x family", wantInTitle: "spy"},
68
69 // long titles (truncated user search)
70 {name: "long_magirevo", query: "magical revolution reincarnated princess", wantInTitle: "magical revolution"},
71 {name: "long_slime", query: "that time i got reincarnated as a slime", wantInTitle: "slime"},
72 {name: "long_apothecary", query: "apothecary diaries", wantInTitle: "apothecary"},
73
74 // ambiguous / crowded results
75 {name: "ambiguous_monogatari", query: "monogatari", wantInTitle: "monogatari"},
76 {name: "ambiguous_fate", query: "fate", wantInTitle: "fate"},
77 {name: "ambiguous_gundam", query: "gundam", wantInTitle: "gundam"},
78
79 // season / cour noise
80 {name: "season_noise", query: "frieren season 2", wantInTitle: "frieren"},
81 {name: "cour_alias", query: "mushoku tensei jobless reincarnation", wantInTitle: "mushoku"},
82
83 // spinoffs & similar titles
84 {name: "spinoff_mini", query: "frieren marumaru mahou", wantInTitle: "frieren"},
85 {name: "long_runner", query: "one piece", wantInTitle: "one piece"},
86
87 // abbreviations & nicknames
88 {name: "abbr_sao", query: "sao", wantInTitle: "sword art"},
89 {name: "abbr_danmachi", query: "danmachi", wantInTitle: "danmachi"},
90 {name: "abbr_csm", query: "chainsaw man", wantInTitle: "chainsaw"},
91
92 // movies & one-shots
93 {name: "movie_your_name", query: "your name", wantInTitle: "your name"},
94 {name: "movie_suzume", query: "suzume", wantInTitle: "suzume"},
95
96 // recent popular
97 {name: "oshi_no_ko", query: "oshi no ko", wantInTitle: "oshi"},
98 {name: "bocchi", query: "bocchi the rock", wantInTitle: "bocchi"},
99 {name: "dungeon_meshi", query: "dungeon meshi", wantInTitle: "dungeon"},
100 {name: "delicious_in_dungeon", query: "delicious in dungeon", wantInTitle: "dungeon"},
101
102 // edge punctuation stripped
103 {name: "bleach_tybw", query: "bleach thousand year blood war", wantInTitle: "bleach"},
104 {name: "evangelion", query: "neon genesis evangelion", wantInTitle: "evangelion"},
105
106 // mild typo / spacing
107 {name: "demon_slayer", query: "demon slayer", wantInTitle: "demon"},
108 {name: "kimetsu", query: "kimetsu no yaiba", wantInTitle: "demon"},
109
110 // unicode-ish (user without diacritics)
111 {name: "witch_hat", query: "witch hat atelier", wantInTitle: "witch"},
112
113 // single word common
114 {name: "single_naruto", query: "naruto", wantInTitle: "naruto"},
115 {name: "single_bleach", query: "bleach", wantInTitle: "bleach"},
116}
117
118type edgeProvider struct {
119 name string
120 search func(string, string) ([]providers.SelectionOption, error)
121 episodes func(string, string) ([]string, error)
122 streams func(string, providers.PlaybackConfig, int) ([]string, map[string]providers.StreamPlaybackHint, error)
123}
124
125func edgeProviders() []edgeProvider {
126 return []edgeProvider{
127 {
128 name: "senshi",
129 search: func(q, mode string) ([]providers.SelectionOption, error) {
130 return (&senshi.Provider{}).SearchAnime(q, mode)
131 },
132 episodes: func(id, mode string) ([]string, error) {
133 return (&senshi.Provider{}).EpisodesList(id, mode)
134 },
135 streams: func(id string, cfg providers.PlaybackConfig, ep int) ([]string, map[string]providers.StreamPlaybackHint, error) {
136 return (&senshi.Provider{}).GetEpisodeURLForModeWithHints(cfg, id, ep, cfg.SubOrDub)
137 },
138 },
139 {
140 name: "anipub",
141 search: func(q, mode string) ([]providers.SelectionOption, error) {
142 return (&anipub.Provider{}).SearchAnime(q, mode)
143 },
144 episodes: func(id, mode string) ([]string, error) {
145 return (&anipub.Provider{}).EpisodesList(id, mode)
146 },
147 streams: func(id string, cfg providers.PlaybackConfig, ep int) ([]string, map[string]providers.StreamPlaybackHint, error) {
148 return (&anipub.Provider{}).GetEpisodeURLForModeWithHints(cfg, id, ep, cfg.SubOrDub)
149 },
150 },
151 {
152 name: "anineko",
153 search: func(q, mode string) ([]providers.SelectionOption, error) {
154 return (&anineko.Provider{}).SearchAnime(q, mode)
155 },
156 episodes: func(id, mode string) ([]string, error) {
157 return (&anineko.Provider{}).EpisodesList(id, mode)
158 },
159 streams: func(id string, cfg providers.PlaybackConfig, ep int) ([]string, map[string]providers.StreamPlaybackHint, error) {
160 return (&anineko.Provider{}).GetEpisodeURLForModeWithHints(cfg, id, ep, cfg.SubOrDub)
161 },
162 },
163 }
164}
165
166type edgeResult struct {
167 provider string
168 caseName string
169 query string
170 searchOK bool
171 matchOK bool
172 episodesOK bool
173 streamOK bool
174 searchErr string
175 episodesErr string
176 streamErr string
177 pickedTitle string
178 pickedKey string
179 epCount int
180 searchMs int64
181 flowMs int64
182}
183
184func pickEdgeResult(options []providers.SelectionOption, want string) (providers.SelectionOption, bool) {
185 want = strings.ToLower(strings.TrimSpace(want))
186 if want == "" && len(options) > 0 {
187 return options[0], true
188 }
189 for _, option := range options {
190 for _, candidate := range []string{option.Title, option.Label} {
191 if strings.Contains(strings.ToLower(candidate), want) {
192 return option, true
193 }
194 }
195 }
196 if len(options) > 0 {
197 return options[0], false
198 }
199 return providers.SelectionOption{}, false
200}
201
202func runEdgeCase(provider edgeProvider, tc edgeAnimeCase) edgeResult {
203 res := edgeResult{
204 provider: provider.name,
205 caseName: tc.name,
206 query: tc.query,
207 }
208
209 searchStart := time.Now()
210 options, err := provider.search(tc.query, "sub")
211 res.searchMs = time.Since(searchStart).Milliseconds()
212 if err != nil {
213 res.searchErr = err.Error()
214 return res
215 }
216 if len(options) == 0 {
217 res.searchErr = "no results"
218 return res
219 }
220 res.searchOK = true
221
222 picked, matched := pickEdgeResult(options, tc.wantInTitle)
223 res.pickedTitle = picked.Title
224 if res.pickedTitle == "" {
225 res.pickedTitle = picked.Label
226 }
227 res.pickedKey = picked.Key
228 res.matchOK = matched
229
230 if provider.episodes == nil || provider.streams == nil || picked.Key == "" {
231 return res
232 }
233
234 flowStart := time.Now()
235 eps, err := provider.episodes(picked.Key, "sub")
236 if err != nil {
237 res.episodesErr = err.Error()
238 res.flowMs = time.Since(flowStart).Milliseconds()
239 return res
240 }
241 if len(eps) == 0 {
242 res.episodesErr = "no episodes"
243 res.flowMs = time.Since(flowStart).Milliseconds()
244 return res
245 }
246 res.episodesOK = true
247 res.epCount = len(eps)
248
249 links, _, err := provider.streams(picked.Key, providers.PlaybackConfig{SubOrDub: "sub"}, 1)
250 res.flowMs = time.Since(flowStart).Milliseconds()
251 if err != nil {
252 res.streamErr = err.Error()
253 return res
254 }
255 if len(links) == 0 {
256 res.streamErr = "no stream url"
257 return res
258 }
259 res.streamOK = true
260 return res
261}
262
263func TestLiveProviderEdgeCases(t *testing.T) {
264 initLiveEdge(t)
265
266 providersList := edgeProviders()
267 results := make([]edgeResult, 0, len(edgeAnimeCases)*len(providersList))
268
269 for _, provider := range providersList {
270 for _, tc := range edgeAnimeCases {
271 res := runEdgeCase(provider, tc)
272 results = append(results, res)
273 t.Logf("%s", edgeStatusLine(res))
274 time.Sleep(150 * time.Millisecond)
275 }
276 }
277
278 sort.Slice(results, func(i, j int) bool {
279 if results[i].provider != results[j].provider {
280 return results[i].provider < results[j].provider
281 }
282 return results[i].caseName < results[j].caseName
283 })
284
285 t.Log(edgeSummary(results))
286
287 type counts struct{ search, match, episodes, stream, total int }
288 byProvider := map[string]counts{}
289 for _, res := range results {
290 c := byProvider[res.provider]
291 c.total++
292 if res.searchOK {
293 c.search++
294 }
295 if res.matchOK {
296 c.match++
297 }
298 if res.episodesOK {
299 c.episodes++
300 }
301 if res.streamOK {
302 c.stream++
303 }
304 byProvider[res.provider] = c
305 }
306
307 for name, c := range byProvider {
308 t.Logf("%s: search %d/%d match %d/%d episodes %d/%d stream %d/%d",
309 name, c.search, c.total, c.match, c.total, c.episodes, c.total, c.stream, c.total)
310 }
311
312 failures := make([]string, 0)
313 for _, res := range results {
314 if !res.searchOK {
315 failures = append(failures, fmt.Sprintf("%s/%s search failed: %s", res.provider, res.caseName, res.searchErr))
316 continue
317 }
318 if !res.matchOK {
319 failures = append(failures, fmt.Sprintf("%s/%s weak match: picked %q for query %q", res.provider, res.caseName, res.pickedTitle, res.query))
320 }
321 if res.searchOK && !res.streamOK {
322 failures = append(failures, fmt.Sprintf("%s/%s stream failed: episodes_err=%s stream_err=%s", res.provider, res.caseName, res.episodesErr, res.streamErr))
323 }
324 }
325
326 critical := []string{"english_aot", "single_naruto", "demon_slayer", "dungeon_meshi", "romaji_frieren"}
327 for _, provider := range []string{"senshi", "anineko"} {
328 for _, caseName := range critical {
329 for _, res := range results {
330 if res.provider == provider && res.caseName == caseName {
331 if !res.streamOK {
332 t.Errorf("critical case %s/%s must stream: search=%v episodes=%v stream_err=%s", provider, caseName, res.searchOK, res.episodesOK, res.streamErr)
333 }
334 break
335 }
336 }
337 }
338 }
339
340 if len(failures) > 0 {
341 t.Logf("%d edge-case issues logged (see above)", len(failures))
342 }
343}
344
345func edgeStatusLine(res edgeResult) string {
346 match := "ok"
347 if res.searchOK && !res.matchOK {
348 match = "weak"
349 }
350 if !res.searchOK {
351 match = "fail"
352 }
353 stream := "-"
354 if res.streamOK {
355 stream = "ok"
356 } else if res.searchOK {
357 stream = "fail"
358 }
359 return fmt.Sprintf("[%s] %-22s %-28s search=%s match=%s eps=%v stream=%s (%dms+%dms) picked=%q",
360 res.provider, res.caseName, res.query,
361 boolMark(res.searchOK), match, res.episodesOK, stream,
362 res.searchMs, res.flowMs, truncate(res.pickedTitle, 48))
363}
364
365func edgeSummary(results []edgeResult) string {
366 var b strings.Builder
367 b.WriteString("\n=== provider edge-case summary ===\n")
368 current := ""
369 for _, res := range results {
370 if res.provider != current {
371 current = res.provider
372 b.WriteString("\n-- " + current + " --\n")
373 }
374 b.WriteString(edgeStatusLine(res))
375 b.WriteByte('\n')
376 }
377 return b.String()
378}
379
380func boolMark(ok bool) string {
381 if ok {
382 return "ok"
383 }
384 return "FAIL"
385}
386
387func truncate(s string, max int) string {
388 s = strings.TrimSpace(s)
389 if len(s) <= max {
390 return s
391 }
392 return s[:max-3] + "..."
393}