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