With an empty search box, Movies and Series show one of Trakt's public lists: trending now, most watched this week, anticipated, popular, and for movies the US box office. [ ] switch lists, r reloads. Each entry is looked up in Radarr (tmdb:) or Sonarr (tvdb:), so it behaves like a search result: details, poster, tick when in the library, add, releases. Needs a Trakt client ID: new optional trakt.client_id setting (Config view section "Trakt", ARR_TUI_TRAKT_CLIENT_ID), masked like the API keys and checked by Test. ARR_TUI_TRAKT_URL points at another server for development. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
88 lines
3.4 KiB
Go
88 lines
3.4 KiB
Go
package trakt
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestEntries(t *testing.T) {
|
|
var got http.Header
|
|
var gotQuery string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
got, gotQuery = r.Header, r.URL.RawQuery
|
|
switch r.URL.Path {
|
|
case "/movies/trending":
|
|
w.Write([]byte(`[{"watchers":1234,"movie":{"title":"Dune","year":2021,"ids":{"trakt":1,"slug":"dune-2021","imdb":"tt1160419","tmdb":438631}}},
|
|
{"watchers":7,"movie":{"title":"No Year","year":null,"ids":{"trakt":2,"slug":"x","imdb":null,"tmdb":null}}}]`))
|
|
case "/movies/popular":
|
|
w.Write([]byte(`[{"title":"Heat","year":1995,"ids":{"trakt":3,"slug":"heat-1995","tmdb":949}}]`))
|
|
case "/shows/watched/weekly":
|
|
w.Write([]byte(`[{"watcher_count":52000,"play_count":1,"collected_count":2,"collector_count":3,"show":{"title":"Andor","year":2022,"ids":{"trakt":4,"slug":"andor","tvdb":393189,"tmdb":83867}}}]`))
|
|
case "/movies/boxoffice":
|
|
w.Write([]byte(`[{"revenue":48464322,"movie":{"title":"Big","year":2026,"ids":{"trakt":5,"tmdb":5}}}]`))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewClient(" my-id ", "arr-tui/test")
|
|
c.SetBaseURL(srv.URL)
|
|
movies, err := c.Entries(context.Background(), MovieLists[0], 20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Get("trakt-api-key") != "my-id" || got.Get("trakt-api-version") != "2" || got.Get("User-Agent") != "arr-tui/test" ||
|
|
got.Get("Content-Type") != "application/json" {
|
|
t.Errorf("headers: %v", got)
|
|
}
|
|
if gotQuery != "limit=20&page=1" {
|
|
t.Errorf("query %q", gotQuery)
|
|
}
|
|
if len(movies) != 2 || movies[0].Title != "Dune" || movies[0].IDs.TMDB != 438631 || movies[0].Count != 1234 || movies[1].IDs.TMDB != 0 {
|
|
t.Errorf("trending: %+v", movies)
|
|
}
|
|
if note := MovieLists[0].Note(movies[0].Count); note != "1.2k watching" {
|
|
t.Errorf("note %q", note)
|
|
}
|
|
|
|
popular, err := c.Entries(context.Background(), MovieLists[3], 20)
|
|
if err != nil || len(popular) != 1 || popular[0].Title != "Heat" || popular[0].IDs.TMDB != 949 || MovieLists[3].Note(popular[0].Count) != "" {
|
|
t.Errorf("popular (bare items): %+v %v", popular, err)
|
|
}
|
|
shows, err := c.Entries(context.Background(), ShowLists[1], 20)
|
|
if err != nil || len(shows) != 1 || shows[0].IDs.TVDB != 393189 || shows[0].Count != 52000 || ShowLists[1].Note(52000) != "52k watched it" {
|
|
t.Errorf("most watched shows: %+v %v", shows, err)
|
|
}
|
|
box, err := c.Entries(context.Background(), MovieLists[4], 20)
|
|
if err != nil || box[0].Count != 48464322 || MovieLists[4].Note(box[0].Count) != "$48M last weekend" {
|
|
t.Errorf("box office: %+v %v", box, err)
|
|
}
|
|
}
|
|
|
|
func TestEntriesErrors(t *testing.T) {
|
|
if _, err := NewClient("", "ua").Entries(context.Background(), MovieLists[0], 5); !errors.Is(err, ErrNotConfigured) {
|
|
t.Errorf("no client id: %v", err)
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
}))
|
|
defer srv.Close()
|
|
c := NewClient("bad", "ua")
|
|
c.SetBaseURL(srv.URL)
|
|
if _, err := c.Entries(context.Background(), MovieLists[0], 5); err == nil || err.Error() != "trakt: the client ID was not accepted (HTTP 403)" {
|
|
t.Errorf("rejected id: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCompact(t *testing.T) {
|
|
for n, want := range map[int64]string{0: "0", 950: "950", 1000: "1k", 1234: "1.2k", 34567: "34k", 1500000: "1.5M", 48464322: "48M"} {
|
|
if got := Compact(n); got != want {
|
|
t.Errorf("Compact(%d) = %q, want %q", n, got, want)
|
|
}
|
|
}
|
|
}
|