Добавил поиск метаданных по каталогам

This commit is contained in:
2026-06-14 15:21:01 +03:00
parent 9c1b178e46
commit 5087f35861
21 changed files with 1435 additions and 72 deletions
+109
View File
@@ -0,0 +1,109 @@
package metadata
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func newTMDB(t *testing.T, url string) *TMDB {
t.Helper()
c, err := NewTMDB(TMDBConfig{APIKey: "k", BaseURL: url})
if err != nil {
t.Fatalf("NewTMDB: %v", err)
}
return c
}
func TestTMDB_SearchMovie(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/search/movie" {
t.Errorf("path = %q", r.URL.Path)
}
q := r.URL.Query()
if q.Get("api_key") != "k" || q.Get("query") != "The Matrix" || q.Get("year") != "1999" {
t.Errorf("query = %v", q)
}
_, _ = w.Write([]byte(`{"results":[
{"id":603,"title":"The Matrix","original_title":"The Matrix","release_date":"1999-03-31"}
]}`))
}))
defer srv.Close()
got, err := newTMDB(t, srv.URL).Search(context.Background(), Query{Type: Movie, Title: "The Matrix", Year: 1999})
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(got) != 1 {
t.Fatalf("got %d candidates", len(got))
}
c := got[0]
if c.Provider != "tmdb" || c.ID != "603" || c.Title != "The Matrix" || c.Year != 1999 {
t.Errorf("candidate = %+v", c)
}
}
func TestTMDB_SearchSeries(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/search/tv" {
t.Errorf("path = %q", r.URL.Path)
}
if r.URL.Query().Get("first_air_date_year") != "2015" {
t.Errorf("year param = %q", r.URL.Query().Get("first_air_date_year"))
}
_, _ = w.Write([]byte(`{"results":[
{"id":60622,"name":"Fargo","original_name":"Fargo","first_air_date":"2014-04-15"}
]}`))
}))
defer srv.Close()
got, err := newTMDB(t, srv.URL).Search(context.Background(), Query{Type: Series, Title: "Fargo", Year: 2015})
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(got) != 1 || got[0].ID != "60622" || got[0].Title != "Fargo" || got[0].Year != 2014 {
t.Errorf("candidate = %+v", got[0])
}
}
func TestTMDB_SeasonEpisodeCounts(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/tv/60622" {
t.Errorf("path = %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"seasons":[
{"season_number":0,"episode_count":2},
{"season_number":1,"episode_count":10},
{"season_number":2,"episode_count":10}
]}`))
}))
defer srv.Close()
counts, err := newTMDB(t, srv.URL).SeasonEpisodeCounts(context.Background(), "60622")
if err != nil {
t.Fatalf("SeasonEpisodeCounts: %v", err)
}
if counts[1] != 10 || counts[2] != 10 || counts[0] != 2 {
t.Errorf("counts = %v", counts)
}
}
func TestTMDB_ErrorStatus(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"status_message":"invalid key"}`))
}))
defer srv.Close()
_, err := newTMDB(t, srv.URL).Search(context.Background(), Query{Type: Movie, Title: "X"})
if err == nil {
t.Fatal("want error on 401")
}
}
func TestNewTMDB_RequiresKey(t *testing.T) {
if _, err := NewTMDB(TMDBConfig{}); err == nil {
t.Fatal("want error without api_key")
}
}