internal/episode_link_recovery_test.go
278 lines · 9829 bytes
1package internal
2
3import (
4 "errors"
5 "strings"
6 "testing"
7)
8
9func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
10 withAudio := episodeLinkFailureRecoveryOptions("sub", true)
11 if len(withAudio) != 3 {
12 t.Fatalf("expected 3 options with audio, got %#v", withAudio)
13 }
14 if withAudio[0].Key != "remap" {
15 t.Fatalf("remap should be first: %#v", withAudio[0])
16 }
17 if withAudio[1].Key != "audio" || !strings.Contains(withAudio[1].Label, "dub") {
18 t.Fatalf("audio should be second and mention dub: %#v", withAudio[1])
19 }
20 if withAudio[2].Key != "episode" || !strings.Contains(strings.ToLower(withAudio[2].Label), "wrong") {
21 t.Fatalf("episode correction should be last ranked action: %#v", withAudio[2])
22 }
23 for _, opt := range withAudio {
24 if strings.HasPrefix(opt.Label, "1.") || strings.HasPrefix(opt.Label, "2.") {
25 t.Fatalf("recovery options should not be numbered: %#v", opt)
26 }
27 }
28
29 withoutAudio := episodeLinkFailureRecoveryOptions("dub", false)
30 if len(withoutAudio) != 2 {
31 t.Fatalf("expected 2 options without audio, got %#v", withoutAudio)
32 }
33 if withoutAudio[0].Key != "remap" || withoutAudio[1].Key != "episode" {
34 t.Fatalf("unexpected options without audio: %#v", withoutAudio)
35 }
36}
37
38func TestEpisodeLinkFailureDiagnosisIncludesEpisodeModeProviders(t *testing.T) {
39 withAllProvidersEnabledForTest(t)
40 cfg := &CurdConfig{Provider: `["senshi","allanime"]`, SubOrDub: "sub"}
41 anime := &Anime{
42 Title: AnimeTitle{English: "Frieren: Beyond Journey's End"},
43 Ep: Episode{Number: 12},
44 ProviderName: "senshi",
45 }
46 msg := episodeLinkFailureDiagnosis(cfg, anime, errors.New("no sub episode links found across providers"))
47 for _, want := range []string{
48 `Couldn't find Episode 12 (sub)`,
49 `Frieren: Beyond Journey's End`,
50 "Tried:",
51 "senshi",
52 "allanime",
53 "Reason:",
54 "no sub episode links",
55 } {
56 if !strings.Contains(msg, want) {
57 t.Fatalf("diagnosis missing %q in:\n%s", want, msg)
58 }
59 }
60}
61
62func TestPromptEpisodeLinkFailureRecoveryMapsExitKeysToBack(t *testing.T) {
63 cfg := &CurdConfig{SubOrDub: "sub", Provider: `["allanime"]`}
64 anime := &Anime{Title: AnimeTitle{Romaji: "Example"}, Ep: Episode{Number: 1}}
65
66 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
67 return SelectionOption{Key: "-2", Label: "Back"}, nil
68 })
69 if got := promptEpisodeLinkFailureRecovery(cfg, anime, errors.New("fail"), true); got != "back" {
70 t.Fatalf("expected back for -2, got %q", got)
71 }
72
73 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
74 return SelectionOption{Key: "-1", Label: "Quit"}, nil
75 })
76 if got := promptEpisodeLinkFailureRecovery(cfg, anime, errors.New("fail"), true); got != "quit" {
77 t.Fatalf("expected quit for -1, got %q", got)
78 }
79}
80
81func TestResolveEpisodeLinksWithRecoverySucceedsWithoutRecoveryMenu(t *testing.T) {
82 provider := &stackStubProvider{
83 name: "allanime",
84 episodeResults: map[string]map[string][]string{
85 "allanime-id": {"sub": {"sub-url"}},
86 },
87 searchResults: map[string][]SelectionOption{
88 "sub": {{Title: "Example", Key: "allanime-id"}},
89 },
90 }
91 withProviderFactories(t, provider)
92
93 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
94 t.Fatalf("recovery menu should not open when preferred resolve succeeds: %#v", options)
95 return SelectionOption{}, nil
96 })
97
98 cfg := &CurdConfig{Provider: `["allanime"]`, SubOrDub: "sub"}
99 anime := &Anime{
100 Title: AnimeTitle{Romaji: "Example"},
101 ProviderName: "allanime",
102 ProviderId: "allanime-id",
103 Ep: Episode{Number: 1},
104 }
105
106 result, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false)
107 if !ok {
108 t.Fatal("expected success without recovery")
109 }
110 if result.Links[0] != "sub-url" || result.Mode != "sub" {
111 t.Fatalf("unexpected result: %#v", result)
112 }
113}
114
115func TestResolveEpisodeLinksWithRecoveryOnlyAfterPreferredAndAlternateFail(t *testing.T) {
116 // Both modes have zero streams → no alternate prompt (nothing to offer), then recovery.
117 providerBothDead := &stackStubProvider{
118 name: "allanime",
119 episodeErrors: map[string]map[string]error{
120 "allanime-id": {
121 "sub": errors.New("no sub"),
122 "dub": errors.New("no dub"),
123 },
124 },
125 searchResults: map[string][]SelectionOption{
126 "sub": {{Title: "Example", Key: "allanime-id"}},
127 "dub": {{Title: "Example", Key: "allanime-id"}},
128 },
129 }
130 withProviderFactories(t, providerBothDead)
131
132 var promptPhases []string
133 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
134 if len(options) >= 1 && options[0].Key == "play" {
135 promptPhases = append(promptPhases, "alternate-audio")
136 return SelectionOption{Key: "cancel"}, nil
137 }
138 if len(options) >= 1 && options[0].Key == "remap" {
139 promptPhases = append(promptPhases, "recovery")
140 keys := make([]string, 0, len(options))
141 for _, opt := range options {
142 keys = append(keys, opt.Key)
143 }
144 joined := strings.Join(keys, ",")
145 if !strings.Contains(joined, "remap") || !strings.Contains(joined, "episode") || !strings.Contains(joined, "audio") {
146 t.Fatalf("recovery menu missing expected actions: %s", joined)
147 }
148 if strings.Contains(joined, "quit") || strings.Contains(joined, "cancel") {
149 t.Fatalf("recovery menu should not embed cancel/quit keys: %s", joined)
150 }
151 return SelectionOption{Key: "back"}, nil
152 }
153 t.Fatalf("unexpected prompt options: %#v", options)
154 return SelectionOption{}, nil
155 })
156
157 cfg := &CurdConfig{Provider: `["allanime","no-animepahe"]`, SubOrDub: "sub"}
158 anime := &Anime{
159 Title: AnimeTitle{Romaji: "Example"},
160 ProviderName: "allanime",
161 ProviderId: "allanime-id",
162 Ep: Episode{Number: 3},
163 }
164
165 result, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false)
166 if ok || len(result.Links) > 0 {
167 t.Fatalf("expected user back-out after recovery, got ok=%v result=%#v", ok, result)
168 }
169 // No playable alternate streams ⇒ skip audio prompt, go straight to recovery.
170 if got := strings.Join(promptPhases, ","); got != "recovery" {
171 t.Fatalf("expected recovery only when alternate has no streams, got %q", got)
172 }
173 if len(providerBothDead.calls) < 1 || providerBothDead.calls[0] != "allanime:sub" {
174 t.Fatalf("expected preferred sub first, got %#v", providerBothDead.calls)
175 }
176
177 // When alternate streams exist, user is asked before recovery.
178 providerAltOK := &stackStubProvider{
179 name: "allanime",
180 episodeResults: map[string]map[string][]string{
181 "allanime-id": {"dub": {"dub-url"}},
182 },
183 episodeErrors: map[string]map[string]error{
184 "allanime-id": {"sub": errors.New("no sub")},
185 },
186 searchResults: map[string][]SelectionOption{
187 "sub": {{Title: "Example", Key: "allanime-id"}},
188 "dub": {{Title: "Example", Key: "allanime-id"}},
189 },
190 }
191 withProviderFactories(t, providerAltOK)
192 promptPhases = nil
193 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
194 if len(options) >= 1 && options[0].Key == "play" {
195 promptPhases = append(promptPhases, "alternate-audio")
196 return SelectionOption{Key: "cancel"}, nil
197 }
198 if len(options) >= 1 && options[0].Key == "remap" {
199 promptPhases = append(promptPhases, "recovery")
200 return SelectionOption{Key: "back"}, nil
201 }
202 t.Fatalf("unexpected prompt options: %#v", options)
203 return SelectionOption{}, nil
204 })
205 anime.Ep.Number = 4
206 if _, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false); ok {
207 t.Fatal("expected back-out after declining alternate")
208 }
209 if got := strings.Join(promptPhases, ","); got != "alternate-audio,recovery" {
210 t.Fatalf("expected alternate audio prompt before recovery when dub exists, got %q", got)
211 }
212}
213
214func TestResolveEpisodeLinksWithRecoveryRemapThenSucceeds(t *testing.T) {
215 // First mapping fails; after user remaps via ResolveAnimeProviderMapping stubs are hard.
216 // Simulate remap by having recovery choose "episode" change that unlocks links? Better:
217 // recovery "audio" after preferred-only failure with dub available without going through
218 // the built-in alternate prompt (user declined it).
219
220 provider := &stackStubProvider{
221 name: "allanime",
222 episodeResults: map[string]map[string][]string{
223 "allanime-id": {"dub": {"dub-url"}},
224 },
225 episodeErrors: map[string]map[string]error{
226 "allanime-id": {"sub": errors.New("no sub")},
227 },
228 searchResults: map[string][]SelectionOption{
229 "sub": {{Title: "Example", Key: "allanime-id"}},
230 "dub": {{Title: "Example", Key: "allanime-id"}},
231 },
232 }
233 withProviderFactories(t, provider)
234
235 phase := 0
236 withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) {
237 phase++
238 switch phase {
239 case 1:
240 // Decline alternate during ResolveEpisodeURLForPlayback.
241 if options[0].Key != "play" {
242 t.Fatalf("expected alternate audio prompt first, got %#v", options)
243 }
244 return SelectionOption{Key: "cancel"}, nil
245 case 2:
246 // Recovery menu — pick explicit audio retry.
247 if options[0].Key != "remap" {
248 t.Fatalf("expected recovery menu, got %#v", options)
249 }
250 return SelectionOption{Key: "audio"}, nil
251 case 3:
252 // Approve alternate from recovery path.
253 if options[0].Key != "play" {
254 t.Fatalf("expected alternate audio approval, got %#v", options)
255 }
256 return SelectionOption{Key: "play"}, nil
257 default:
258 t.Fatalf("unexpected extra prompt phase %d: %#v", phase, options)
259 return SelectionOption{}, nil
260 }
261 })
262
263 cfg := &CurdConfig{Provider: `["allanime","no-animepahe"]`, SubOrDub: "sub"}
264 anime := &Anime{
265 Title: AnimeTitle{Romaji: "Example"},
266 ProviderName: "allanime",
267 ProviderId: "allanime-id",
268 Ep: Episode{Number: 1},
269 }
270
271 result, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false)
272 if !ok {
273 t.Fatal("expected recovery audio path to succeed")
274 }
275 if result.Mode != "dub" || result.Links[0] != "dub-url" {
276 t.Fatalf("expected dub result after recovery audio, got %#v", result)
277 }
278}