internal/providers/senshi/provider_test.go
142 lines · 3916 bytes
1package senshi
2
3import (
4 "encoding/json"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "testing"
10
11 "github.com/wraient/curd/internal/curdhost"
12 "github.com/wraient/curd/internal/providers"
13)
14
15type roundTripFunc func(*http.Request) (*http.Response, error)
16
17func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
18 return f(req)
19}
20
21func withSenshiTestClient(t *testing.T, client *http.Client) {
22 t.Helper()
23 previous := curdhost.HTTPClient
24 t.Cleanup(func() {
25 curdhost.HTTPClient = previous
26 })
27 curdhost.HTTPClient = func() *http.Client { return client }
28}
29
30func jsonResponse(req *http.Request, statusCode int, payload any) *http.Response {
31 body, _ := json.Marshal(payload)
32 resp := &http.Response{
33 StatusCode: statusCode,
34 Header: make(http.Header),
35 Body: io.NopCloser(strings.NewReader(string(body))),
36 Request: req,
37 }
38 resp.Header.Set("Content-Type", "application/json")
39 return resp
40}
41
42func TestSearchAnimeParsesResults(t *testing.T) {
43 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44 if r.URL.Path != "/anime/filter" || r.Method != http.MethodPost {
45 http.NotFound(w, r)
46 return
47 }
48 _ = json.NewEncoder(w).Encode(filterResponse{
49 Data: []animeItem{{
50 ID: 52991,
51 PublicID: "4medy",
52 Title: "Sousou no Frieren",
53 TitleEnglish: "Frieren: Beyond Journey's End",
54 Type: "TV",
55 AniEpisodes: "28",
56 AniYear: 2023,
57 }},
58 Total: 1,
59 })
60 }))
61 defer server.Close()
62
63 withSenshiTestClient(t, server.Client())
64 originalBase := baseURL
65 t.Cleanup(func() { baseURL = originalBase })
66 baseURL = server.URL
67
68 options, err := searchAnime("frieren", "sub")
69 if err != nil {
70 t.Fatalf("searchAnime: %v", err)
71 }
72 if len(options) != 1 {
73 t.Fatalf("expected 1 option, got %d", len(options))
74 }
75 if options[0].Key != "52991" {
76 t.Fatalf("unexpected key %q", options[0].Key)
77 }
78 if !strings.Contains(options[0].Thumbnail, "/posters/52991.webp") {
79 t.Fatalf("unexpected thumbnail %q", options[0].Thumbnail)
80 }
81 item, ok := options[0].ExtraData.(SearchItem)
82 if !ok || item.Episodes != 28 {
83 t.Fatalf("unexpected extra data %+v", options[0].ExtraData)
84 }
85}
86
87func TestEpisodesListParsesEpisodeIDs(t *testing.T) {
88 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
89 if r.URL.Path != "/episodes/52991" {
90 http.NotFound(w, r)
91 return
92 }
93 _ = json.NewEncoder(w).Encode([]episodeItem{
94 {ID: 1, EpID: 1, MalID: 52991},
95 {ID: 3, EpID: 3, MalID: 52991},
96 })
97 }))
98 defer server.Close()
99
100 withSenshiTestClient(t, server.Client())
101 originalBase := baseURL
102 t.Cleanup(func() { baseURL = originalBase })
103 baseURL = server.URL
104
105 episodes, err := episodesList("52991", "sub")
106 if err != nil {
107 t.Fatalf("episodesList: %v", err)
108 }
109 if len(episodes) != 2 || episodes[0] != "1" || episodes[1] != "3" {
110 t.Fatalf("unexpected episodes %#v", episodes)
111 }
112}
113
114func TestGetEpisodeStreamsForModeSelectsHardSub(t *testing.T) {
115 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
116 if r.URL.Path != "/episode-embeds/52991/1" {
117 http.NotFound(w, r)
118 return
119 }
120 _ = json.NewEncoder(w).Encode([]embedItem{
121 {URL: "https://cdn.example/sub.m3u8", Status: "HardSub"},
122 {URL: "https://cdn.example/dub.m3u8", Status: "Dub"},
123 })
124 }))
125 defer server.Close()
126
127 withSenshiTestClient(t, server.Client())
128 originalBase := baseURL
129 t.Cleanup(func() { baseURL = originalBase })
130 baseURL = server.URL
131
132 links, hints, err := getEpisodeStreamsForMode("52991", providers.PlaybackConfig{SubOrDub: "sub"}, 1)
133 if err != nil {
134 t.Fatalf("getEpisodeStreamsForMode: %v", err)
135 }
136 if len(links) != 1 || links[0] != "https://cdn.example/sub.m3u8" {
137 t.Fatalf("unexpected links %#v", links)
138 }
139 if hints[links[0]].Referrer != baseURL+"/" {
140 t.Fatalf("unexpected referrer %q", hints[links[0]].Referrer)
141 }
142}