package arr import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "testing" "bellhop/internal/config" ) func TestSonarrSearch(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v3/series/lookup" { t.Errorf("path = %q", r.URL.Path) } if r.URL.Query().Get("term") != "severance" { t.Errorf("term = %q", r.URL.Query().Get("term")) } _, _ = io.WriteString(w, `[{"title":"Severance","year":2022,"tvdbId":371980}]`) })) defer srv.Close() s := NewSonarr(&config.ArrConfig{URL: srv.URL, APIKey: "k", QualityProfileID: 4, RootFolder: "/tv"}) results, err := s.Search(context.Background(), "severance") if err != nil { t.Fatalf("search: %v", err) } if len(results) != 1 || results[0].Title != "Severance" || results[0].Year != 2022 { t.Fatalf("unexpected results: %+v", results) } } func TestSonarrAdd(t *testing.T) { var got map[string]any srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost || r.URL.Path != "/api/v3/series" { t.Errorf("unexpected %s %s", r.Method, r.URL.Path) } _ = json.NewDecoder(r.Body).Decode(&got) w.WriteHeader(http.StatusCreated) })) defer srv.Close() s := NewSonarr(&config.ArrConfig{URL: srv.URL, APIKey: "k", QualityProfileID: 4, RootFolder: "/tv"}) res := makeResult("Severance", 2022, `{"title":"Severance","year":2022,"tvdbId":371980}`) if err := s.Add(context.Background(), res); err != nil { t.Fatalf("add: %v", err) } if got["qualityProfileId"].(float64) != 4 { t.Errorf("qualityProfileId = %v", got["qualityProfileId"]) } if got["rootFolderPath"] != "/tv" { t.Errorf("rootFolderPath = %v", got["rootFolderPath"]) } if got["monitored"] != true || got["seasonFolder"] != true { t.Errorf("monitored/seasonFolder wrong: %v", got) } opts := got["addOptions"].(map[string]any) if opts["monitor"] != "all" || opts["searchForMissingEpisodes"] != true { t.Errorf("addOptions wrong: %v", opts) } }