internal/provider_stack_test.go
616 lines · 20924 bytes
1package internal
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "github.com/wraient/curd/internal/providers"
11)
12
13type stackStubProvider struct {
14 name string
15 searchResults map[string][]SelectionOption
16 episodeResults map[string]map[string][]string
17 episodeErrors map[string]map[string]error
18 searchCalls []string
19 calls []string
20}
21
22func (s *stackStubProvider) Name() string { return s.name }
23
24func (s *stackStubProvider) SearchAnime(query, mode string) ([]SelectionOption, error) {
25 mode = normalizeTranslationType(mode)
26 s.searchCalls = append(s.searchCalls, s.name+":"+mode+":"+query)
27 return s.searchResults[mode], nil
28}
29
30func (s *stackStubProvider) EpisodesList(showID, mode string) ([]string, error) {
31 return nil, nil
32}
33
34func (s *stackStubProvider) GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
35 return s.GetEpisodeURLForMode(config, id, epNo, config.SubOrDub)
36}
37
38func (s *stackStubProvider) GetEpisodeURLForMode(config CurdConfig, id string, epNo int, mode string) ([]string, error) {
39 mode = normalizeTranslationType(mode)
40 s.calls = append(s.calls, s.name+":"+mode)
41 if byID, ok := s.episodeErrors[id]; ok {
42 if err := byID[mode]; err != nil {
43 return nil, err
44 }
45 }
46 if byID, ok := s.episodeResults[id]; ok {
47 return byID[mode], nil
48 }
49 return nil, nil
50}
51
52func withProviderFactories(t *testing.T, stubs ...*stackStubProvider) {
53 t.Helper()
54 withAllProvidersEnabledForTest(t)
55 restores := make([]func(), 0, len(stubs))
56 for _, stub := range stubs {
57 stub := stub
58 restores = append(restores, providers.SetFactoryForTest(stub.name, func() providers.Provider {
59 return &stackStubProviderBridge{stub}
60 }))
61 }
62 t.Cleanup(func() {
63 for i := len(restores) - 1; i >= 0; i-- {
64 restores[i]()
65 }
66 })
67}
68
69type stackStubProviderBridge struct {
70 *stackStubProvider
71}
72
73func (s *stackStubProviderBridge) SearchAnime(query, mode string) ([]providers.SelectionOption, error) {
74 options, err := s.stackStubProvider.SearchAnime(query, mode)
75 return toProviderSelectionOptions(options), err
76}
77
78func (s *stackStubProviderBridge) EpisodesList(showID, mode string) ([]string, error) {
79 return s.stackStubProvider.EpisodesList(showID, mode)
80}
81
82func (s *stackStubProviderBridge) GetEpisodeURL(config providers.PlaybackConfig, id string, epNo int) ([]string, error) {
83 return s.stackStubProvider.GetEpisodeURL(CurdConfig{SubOrDub: config.SubOrDub}, id, epNo)
84}
85
86func (s *stackStubProviderBridge) GetEpisodeURLForMode(config providers.PlaybackConfig, id string, epNo int, mode string) ([]string, error) {
87 return s.stackStubProvider.GetEpisodeURLForMode(CurdConfig{SubOrDub: config.SubOrDub}, id, epNo, mode)
88}
89
90func TestConfiguredProviderNamesAcceptsOrderedLists(t *testing.T) {
91 withAllProvidersEnabledForTest(t)
92
93 cases := []struct {
94 name string
95 cfg *CurdConfig
96 want []string
97 }{
98 {name: "empty", cfg: &CurdConfig{}, want: []string{"senshi", "anipub", "anineko", "allanime", "animepahe"}},
99 {name: "json list", cfg: &CurdConfig{Provider: `["allanime","animepahe"]`}, want: []string{"allanime", "animepahe"}},
100 {name: "comma list", cfg: &CurdConfig{Provider: "animepahe,allanime"}, want: []string{"animepahe", "allanime"}},
101 {name: "plus list", cfg: &CurdConfig{Provider: "allanime+animepahe"}, want: []string{"allanime", "animepahe"}},
102 {name: "legacy alias", cfg: &CurdConfig{Provider: "stacked"}, want: []string{"senshi", "anipub", "anineko", "allanime", "animepahe"}},
103 }
104
105 for _, tc := range cases {
106 got := ConfiguredProviderNames(tc.cfg)
107 if len(got) != len(tc.want) {
108 t.Fatalf("%s: got %v, want %v", tc.name, got, tc.want)
109 }
110 for i := range got {
111 if got[i] != tc.want[i] {
112 t.Fatalf("%s: got %v, want %v", tc.name, got, tc.want)
113 }
114 }
115 }
116}
117
118func TestCanonicalProviderConfigValueMigratesLegacyValuesWithoutAddingAnimepahe(t *testing.T) {
119 cases := []struct {
120 name string
121 raw string
122 want string
123 }{
124 {name: "legacy allanime", raw: "allanime", want: `["allanime"]`},
125 {name: "legacy animepahe", raw: "animepahe", want: `["animepahe"]`},
126 {name: "ordered stack", raw: "animepahe,allanime", want: `["animepahe","allanime"]`},
127 {name: "animepahe opt out", raw: "allanime,no-animepahe", want: `["allanime","no-animepahe"]`},
128 {name: "empty default", raw: "", want: "stacked"},
129 }
130
131 for _, tc := range cases {
132 if got := canonicalProviderConfigValue(tc.raw); got != tc.want {
133 t.Fatalf("%s: got %s, want %s", tc.name, got, tc.want)
134 }
135 }
136}
137
138func TestLoadConfigMigratesLegacyProviderToListWithoutAddingAnimepahe(t *testing.T) {
139 configPath := filepath.Join(t.TempDir(), "curd.conf")
140 if err := os.WriteFile(configPath, []byte("Provider=allanime\nAddMissingOptions=true\n"), 0644); err != nil {
141 t.Fatalf("write config: %v", err)
142 }
143
144 config, err := LoadConfig(configPath)
145 if err != nil {
146 t.Fatalf("LoadConfig returned error: %v", err)
147 }
148 if config.Provider != `["allanime"]` {
149 t.Fatalf("got provider %s, want [\"allanime\"]", config.Provider)
150 }
151
152 contents, err := os.ReadFile(configPath)
153 if err != nil {
154 t.Fatalf("read config: %v", err)
155 }
156 if !strings.Contains(string(contents), `Provider=["allanime"]`) {
157 t.Fatalf("config file was not migrated to allanime-only list:\n%s", string(contents))
158 }
159 if strings.Contains(string(contents), "animepahe") {
160 t.Fatalf("migration added animepahe without consent:\n%s", string(contents))
161 }
162}
163
164func TestSearchAnimeDecliningAnimepaheWritesOptOutAndDoesNotAskAgain(t *testing.T) {
165 allanime := &stackStubProvider{
166 name: "allanime",
167 searchResults: map[string][]SelectionOption{"sub": {}},
168 }
169 animepahe := &stackStubProvider{
170 name: "animepahe",
171 searchResults: map[string][]SelectionOption{
172 "sub": {{Title: "Hidden", Key: "pahe-id", Label: "Hidden"}},
173 },
174 }
175 withProviderFactories(t, allanime, animepahe)
176
177 configPath := filepath.Join(t.TempDir(), "curd.conf")
178 if err := os.WriteFile(configPath, []byte("Provider=[\"allanime\"]\n"), 0644); err != nil {
179 t.Fatalf("write config: %v", err)
180 }
181 previousConfigPath := GlobalConfigPath
182 GlobalConfigPath = configPath
183 config := &CurdConfig{Provider: `["allanime"]`}
184 previousConfig := GetGlobalConfig()
185 SetGlobalConfig(config)
186 t.Cleanup(func() {
187 GlobalConfigPath = previousConfigPath
188 SetGlobalConfig(previousConfig)
189 })
190
191 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
192 if len(options) < 2 || options[0].Key != "use" || options[1].Key != "never" {
193 t.Fatalf("unexpected Animepahe fallback options: %#v", options)
194 }
195 return SelectionOption{Key: "never"}, nil
196 })
197
198 results, err := SearchAnime("missing", "sub")
199 if err != nil {
200 t.Fatalf("SearchAnime returned error: %v", err)
201 }
202 if len(results) != 0 {
203 t.Fatalf("expected no results after declining Animepahe, got %#v", results)
204 }
205 if config.Provider != `["allanime","no-animepahe"]` {
206 t.Fatalf("provider config was not updated with opt out: %s", config.Provider)
207 }
208 contents, err := os.ReadFile(configPath)
209 if err != nil {
210 t.Fatalf("read config: %v", err)
211 }
212 if !strings.Contains(string(contents), `Provider=["allanime","no-animepahe"]`) {
213 t.Fatalf("config file missing opt out:\n%s", string(contents))
214 }
215
216 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
217 t.Fatalf("Animepahe prompt should not be shown after no-animepahe opt out")
218 return SelectionOption{}, nil
219 })
220 if _, err := SearchAnime("missing again", "sub"); err != nil {
221 t.Fatalf("SearchAnime after opt out returned error: %v", err)
222 }
223}
224
225func TestSearchAnimeAcceptingAnimepaheAddsItBeforeSearching(t *testing.T) {
226 allanime := &stackStubProvider{
227 name: "allanime",
228 searchResults: map[string][]SelectionOption{"sub": {}},
229 }
230 animepahe := &stackStubProvider{
231 name: "animepahe",
232 searchResults: map[string][]SelectionOption{
233 "sub": {{Title: "Fallback", Key: "pahe-id", Label: "Fallback"}},
234 },
235 }
236 withProviderFactories(t, allanime, animepahe)
237
238 configPath := filepath.Join(t.TempDir(), "curd.conf")
239 if err := os.WriteFile(configPath, []byte("Provider=[\"allanime\"]\n"), 0644); err != nil {
240 t.Fatalf("write config: %v", err)
241 }
242 previousConfigPath := GlobalConfigPath
243 GlobalConfigPath = configPath
244 config := &CurdConfig{Provider: `["allanime"]`}
245 previousConfig := GetGlobalConfig()
246 SetGlobalConfig(config)
247 t.Cleanup(func() {
248 GlobalConfigPath = previousConfigPath
249 SetGlobalConfig(previousConfig)
250 })
251
252 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
253 return SelectionOption{Key: "use"}, nil
254 })
255
256 results, err := SearchAnime("missing", "sub")
257 if err != nil {
258 t.Fatalf("SearchAnime returned error: %v", err)
259 }
260 if len(results) != 1 || results[0].Key != "animepahe::pahe-id" {
261 t.Fatalf("expected Animepahe fallback result, got %#v", results)
262 }
263 if config.Provider != `["allanime","animepahe"]` {
264 t.Fatalf("provider config was not updated with Animepahe: %s", config.Provider)
265 }
266 contents, err := os.ReadFile(configPath)
267 if err != nil {
268 t.Fatalf("read config: %v", err)
269 }
270 if !strings.Contains(string(contents), `Provider=["allanime","animepahe"]`) {
271 t.Fatalf("config file missing Animepahe fallback:\n%s", string(contents))
272 }
273}
274
275func TestSearchAnimeReturnsProviderQualifiedStackResultsInOrder(t *testing.T) {
276 allanime := &stackStubProvider{
277 name: "allanime",
278 searchResults: map[string][]SelectionOption{
279 "sub": {{Title: "First", Key: "allanime-id", Label: "First"}},
280 },
281 }
282 animepahe := &stackStubProvider{
283 name: "animepahe",
284 searchResults: map[string][]SelectionOption{
285 "sub": {{Title: "Second", Key: "pahe-id", Label: "Second"}},
286 },
287 }
288 withProviderFactories(t, allanime, animepahe)
289
290 previousConfig := GetGlobalConfig()
291 SetGlobalConfig(&CurdConfig{Provider: `["allanime","animepahe"]`})
292 t.Cleanup(func() { SetGlobalConfig(previousConfig) })
293
294 results, err := SearchAnime("query", "sub")
295 if err != nil {
296 t.Fatalf("SearchAnime returned error: %v", err)
297 }
298 if len(results) != 2 {
299 t.Fatalf("got %d results, want 2: %#v", len(results), results)
300 }
301 if results[0].Key != "allanime::allanime-id" || results[1].Key != "animepahe::pahe-id" {
302 t.Fatalf("results were not provider-qualified in config order: %#v", results)
303 }
304}
305
306func TestResolveEpisodeURLUsesProviderListOrder(t *testing.T) {
307 allanime := &stackStubProvider{
308 name: "allanime",
309 episodeResults: map[string]map[string][]string{
310 "allanime-id": {"sub": {"allanime-sub"}},
311 },
312 }
313 animepahe := &stackStubProvider{
314 name: "animepahe",
315 episodeResults: map[string]map[string][]string{
316 "pahe-id": {"sub": {"animepahe-sub"}},
317 },
318 searchResults: map[string][]SelectionOption{
319 "sub": {{Title: "Example", Key: "pahe-id"}},
320 },
321 }
322 withProviderFactories(t, allanime, animepahe)
323
324 cfg := CurdConfig{Provider: `["animepahe","allanime"]`, SubOrDub: "sub"}
325 anime := &Anime{
326 Title: AnimeTitle{Romaji: "Example"},
327 ProviderName: "allanime",
328 ProviderId: "allanime-id",
329 }
330
331 result, err := ResolveEpisodeURL(cfg, anime, 1)
332 if err != nil {
333 t.Fatalf("ResolveEpisodeURL returned error: %v", err)
334 }
335 if result.ProviderName != "animepahe" || result.ProviderID != "pahe-id" || result.Links[0] != "animepahe-sub" {
336 t.Fatalf("expected animepahe to win by config order, got %#v", result)
337 }
338}
339
340func TestResolveEpisodeURLForPlaybackTriesAllPreferredProvidersBeforeAudioFallback(t *testing.T) {
341 allanime := &stackStubProvider{
342 name: "allanime",
343 searchResults: map[string][]SelectionOption{
344 "dub": {{Title: "Example", Key: "shared-id"}},
345 "sub": {{Title: "Example", Key: "shared-id"}},
346 },
347 episodeResults: map[string]map[string][]string{
348 "shared-id": {"sub": {"allanime-sub"}},
349 },
350 episodeErrors: map[string]map[string]error{
351 "shared-id": {"dub": errors.New("no allanime dub")},
352 },
353 }
354 animepahe := &stackStubProvider{
355 name: "animepahe",
356 episodeResults: map[string]map[string][]string{
357 "pahe-id": {"dub": {"animepahe-dub"}},
358 },
359 searchResults: map[string][]SelectionOption{
360 "dub": {{Title: "Example", Key: "pahe-id"}},
361 },
362 }
363 withProviderFactories(t, allanime, animepahe)
364 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
365 t.Fatalf("audio fallback prompt should not be shown while another provider has preferred audio")
366 return SelectionOption{}, nil
367 })
368
369 cfg := CurdConfig{Provider: `["allanime","animepahe"]`, SubOrDub: "dub"}
370 anime := &Anime{
371 Title: AnimeTitle{Romaji: "Example"},
372 ProviderName: "allanime",
373 ProviderId: "shared-id",
374 }
375
376 result, err := ResolveEpisodeURLForPlayback(cfg, anime, 1)
377 if err != nil {
378 t.Fatalf("ResolveEpisodeURLForPlayback returned error: %v", err)
379 }
380 if result.ProviderName != "animepahe" || result.Mode != "dub" || result.Links[0] != "animepahe-dub" {
381 t.Fatalf("expected animepahe dub before sub fallback, got %#v", result)
382 }
383 for _, call := range allanime.calls {
384 if call == "allanime:sub" {
385 t.Fatalf("allanime sub fallback was tried before animepahe dub: %#v", allanime.calls)
386 }
387 }
388}
389
390func TestResolveEpisodeURLForPlaybackAcceptingAnimepaheUsesPreferredBeforeSubFallback(t *testing.T) {
391 allanime := &stackStubProvider{
392 name: "allanime",
393 episodeResults: map[string]map[string][]string{
394 "shared-id": {"sub": {"allanime-sub"}},
395 },
396 episodeErrors: map[string]map[string]error{
397 "shared-id": {"dub": errors.New("no allanime dub")},
398 },
399 }
400 animepahe := &stackStubProvider{
401 name: "animepahe",
402 episodeResults: map[string]map[string][]string{
403 "pahe-id": {"dub": {"animepahe-dub"}},
404 },
405 searchResults: map[string][]SelectionOption{
406 "dub": {{Title: "Example", Key: "pahe-id"}},
407 },
408 }
409 withProviderFactories(t, allanime, animepahe)
410
411 configPath := filepath.Join(t.TempDir(), "curd.conf")
412 if err := os.WriteFile(configPath, []byte("Provider=[\"allanime\"]\n"), 0644); err != nil {
413 t.Fatalf("write config: %v", err)
414 }
415 previousConfigPath := GlobalConfigPath
416 GlobalConfigPath = configPath
417 config := &CurdConfig{Provider: `["allanime"]`, SubOrDub: "dub"}
418 previousConfig := GetGlobalConfig()
419 SetGlobalConfig(config)
420 t.Cleanup(func() {
421 GlobalConfigPath = previousConfigPath
422 SetGlobalConfig(previousConfig)
423 })
424
425 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
426 switch options[0].Key {
427 case "use":
428 return SelectionOption{Key: "use"}, nil
429 case "play":
430 t.Fatalf("sub fallback prompt should not be shown while Animepahe has dub")
431 default:
432 t.Fatalf("unexpected prompt options: %#v", options)
433 }
434 return SelectionOption{}, nil
435 })
436
437 anime := &Anime{
438 Title: AnimeTitle{Romaji: "Example"},
439 ProviderName: "allanime",
440 ProviderId: "shared-id",
441 }
442 result, err := ResolveEpisodeURLForPlayback(*config, anime, 155)
443 if err != nil {
444 t.Fatalf("ResolveEpisodeURLForPlayback returned error: %v", err)
445 }
446 if result.ProviderName != "animepahe" || result.Mode != "dub" || result.Links[0] != "animepahe-dub" {
447 t.Fatalf("expected Animepahe dub before sub fallback, got %#v", result)
448 }
449 if config.Provider != `["allanime","animepahe"]` {
450 t.Fatalf("provider config was not updated with Animepahe: %s", config.Provider)
451 }
452 contents, err := os.ReadFile(configPath)
453 if err != nil {
454 t.Fatalf("read config: %v", err)
455 }
456 if !strings.Contains(string(contents), `Provider=["allanime","animepahe"]`) {
457 t.Fatalf("config file missing Animepahe fallback:\n%s", string(contents))
458 }
459 if got := strings.Join(allanime.calls, ","); got != "allanime:dub" {
460 t.Fatalf("expected one AllAnime dub attempt before Animepahe, got %s", got)
461 }
462 if got := strings.Join(animepahe.calls, ","); got != "animepahe:dub" {
463 t.Fatalf("expected Animepahe dub before sub fallback, got %s", got)
464 }
465}
466
467func TestResolveEpisodeURLForPlaybackDecliningAnimepaheFallsBackToAllanimeSub(t *testing.T) {
468 allanime := &stackStubProvider{
469 name: "allanime",
470 episodeResults: map[string]map[string][]string{
471 "shared-id": {"sub": {"allanime-sub"}},
472 },
473 episodeErrors: map[string]map[string]error{
474 "shared-id": {"dub": errors.New("no allanime dub")},
475 },
476 }
477 animepahe := &stackStubProvider{
478 name: "animepahe",
479 episodeResults: map[string]map[string][]string{
480 "pahe-id": {"dub": {"animepahe-dub"}, "sub": {"animepahe-sub"}},
481 },
482 searchResults: map[string][]SelectionOption{
483 "dub": {{Title: "Example", Key: "pahe-id"}},
484 "sub": {{Title: "Example", Key: "pahe-id"}},
485 },
486 }
487 withProviderFactories(t, allanime, animepahe)
488
489 configPath := filepath.Join(t.TempDir(), "curd.conf")
490 if err := os.WriteFile(configPath, []byte("Provider=[\"allanime\"]\n"), 0644); err != nil {
491 t.Fatalf("write config: %v", err)
492 }
493 previousConfigPath := GlobalConfigPath
494 GlobalConfigPath = configPath
495 config := &CurdConfig{Provider: `["allanime"]`, SubOrDub: "dub"}
496 previousConfig := GetGlobalConfig()
497 SetGlobalConfig(config)
498 t.Cleanup(func() {
499 GlobalConfigPath = previousConfigPath
500 SetGlobalConfig(previousConfig)
501 })
502
503 sawSubPrompt := false
504 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
505 switch options[0].Key {
506 case "use":
507 return SelectionOption{Key: "never"}, nil
508 case "play":
509 sawSubPrompt = true
510 return SelectionOption{Key: "play"}, nil
511 default:
512 t.Fatalf("unexpected prompt options: %#v", options)
513 }
514 return SelectionOption{}, nil
515 })
516
517 anime := &Anime{
518 Title: AnimeTitle{Romaji: "Example"},
519 ProviderName: "allanime",
520 ProviderId: "shared-id",
521 }
522 result, err := ResolveEpisodeURLForPlayback(*config, anime, 155)
523 if err != nil {
524 t.Fatalf("ResolveEpisodeURLForPlayback returned error: %v", err)
525 }
526 if result.ProviderName != "allanime" || result.Mode != "sub" || result.Links[0] != "allanime-sub" {
527 t.Fatalf("expected AllAnime sub fallback after declining Animepahe, got %#v", result)
528 }
529 if !sawSubPrompt {
530 t.Fatalf("expected prompt before playing sub fallback")
531 }
532 if config.Provider != `["allanime","no-animepahe"]` {
533 t.Fatalf("provider config was not updated with opt out: %s", config.Provider)
534 }
535 contents, err := os.ReadFile(configPath)
536 if err != nil {
537 t.Fatalf("read config: %v", err)
538 }
539 if !strings.Contains(string(contents), `Provider=["allanime","no-animepahe"]`) {
540 t.Fatalf("config file missing opt out:\n%s", string(contents))
541 }
542 if len(animepahe.searchCalls) != 0 || len(animepahe.calls) != 0 {
543 t.Fatalf("Animepahe should not be touched after opt out, search=%#v episode=%#v", animepahe.searchCalls, animepahe.calls)
544 }
545 if got := strings.Join(allanime.calls, ","); got != "allanime:dub,allanime:sub" {
546 t.Fatalf("expected AllAnime dub then AllAnime sub after declining Animepahe, got %s", got)
547 }
548}
549
550func TestResolveEpisodeURLForPlaybackNoAnimepaheSkipsConsentAndOffersAllanimeSub(t *testing.T) {
551 allanime := &stackStubProvider{
552 name: "allanime",
553 searchResults: map[string][]SelectionOption{
554 "dub": {{Title: "Example", Key: "shared-id"}},
555 "sub": {{Title: "Example", Key: "shared-id"}},
556 },
557 episodeResults: map[string]map[string][]string{
558 "shared-id": {"sub": {"allanime-sub"}},
559 },
560 episodeErrors: map[string]map[string]error{
561 "shared-id": {"dub": errors.New("no allanime dub")},
562 },
563 }
564 animepahe := &stackStubProvider{
565 name: "animepahe",
566 episodeResults: map[string]map[string][]string{
567 "pahe-id": {"dub": {"animepahe-dub"}, "sub": {"animepahe-sub"}},
568 },
569 searchResults: map[string][]SelectionOption{
570 "dub": {{Title: "Example", Key: "pahe-id"}},
571 "sub": {{Title: "Example", Key: "pahe-id"}},
572 },
573 }
574 withProviderFactories(t, allanime, animepahe)
575
576 config := &CurdConfig{Provider: `["allanime","no-animepahe"]`, SubOrDub: "dub"}
577 previousConfig := GetGlobalConfig()
578 SetGlobalConfig(config)
579 t.Cleanup(func() { SetGlobalConfig(previousConfig) })
580
581 sawSubPrompt := false
582 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
583 switch options[0].Key {
584 case "use":
585 t.Fatalf("Animepahe consent prompt should not be shown when no-animepahe is configured")
586 case "play":
587 sawSubPrompt = true
588 return SelectionOption{Key: "play"}, nil
589 default:
590 t.Fatalf("unexpected prompt options: %#v", options)
591 }
592 return SelectionOption{}, nil
593 })
594
595 anime := &Anime{
596 Title: AnimeTitle{Romaji: "Example"},
597 ProviderName: "animepahe",
598 ProviderId: "pahe-id",
599 }
600 result, err := ResolveEpisodeURLForPlayback(*config, anime, 155)
601 if err != nil {
602 t.Fatalf("ResolveEpisodeURLForPlayback returned error: %v", err)
603 }
604 if result.ProviderName != "allanime" || result.Mode != "sub" || result.Links[0] != "allanime-sub" {
605 t.Fatalf("expected AllAnime sub fallback with no-animepahe, got %#v", result)
606 }
607 if !sawSubPrompt {
608 t.Fatalf("expected prompt before playing sub fallback")
609 }
610 if len(animepahe.searchCalls) != 0 || len(animepahe.calls) != 0 {
611 t.Fatalf("Animepahe should not be touched with no-animepahe, search=%#v episode=%#v", animepahe.searchCalls, animepahe.calls)
612 }
613 if got := strings.Join(allanime.calls, ","); got != "allanime:dub,allanime:sub" {
614 t.Fatalf("expected AllAnime dub then AllAnime sub with no-animepahe, got %s", got)
615 }
616}