package ui import ( "strings" "testing" "git.stinnesbeck.com/nils/arr-tui/internal/arr" "git.stinnesbeck.com/nils/arr-tui/internal/radarr" "git.stinnesbeck.com/nils/arr-tui/internal/sonarr" ) func TestMovieItemStatus(t *testing.T) { tests := []struct { name string m radarr.Movie want string have, miss bool }{ {"missing", radarr.Movie{ID: 1, Title: "A", Monitored: true, IsAvailable: true}, "missing", false, true}, {"unreleased", radarr.Movie{ID: 2, Title: "B", Monitored: true}, "not released", false, false}, {"unmonitored", radarr.Movie{ID: 3, Title: "C", IsAvailable: true}, "unmonitored", false, false}, {"downloaded", radarr.Movie{ID: 4, Title: "D", Monitored: true, HasFile: true, IsAvailable: true}, "✓", true, false}, } for _, tc := range tests { it := newMovieItem(tc.m) if !strings.Contains(it.status, tc.want) || it.have != tc.have || it.missing != tc.miss { t.Errorf("%s: status=%q have=%v missing=%v", tc.name, it.status, it.have, it.missing) } } } func TestSeriesItemStatus(t *testing.T) { s := sonarr.Series{ID: 1, Title: "S", Year: 2008, Monitored: true, Statistics: sonarr.Statistics{EpisodeFileCount: 3, EpisodeCount: 10}} it := newSeriesItem(s) if !it.have || !it.missing || !strings.Contains(it.status, "3/10") || it.label != "S (2008)" { t.Errorf("item = %+v", it) } s.Statistics.EpisodeFileCount = 10 if it := newSeriesItem(s); it.missing || !strings.Contains(it.status, "green") { t.Errorf("complete series: %+v", it) } } func TestFilterItems(t *testing.T) { items := []libItem{ {id: 1, title: "Zulu", monitored: true, missing: true}, {id: 2, title: "alpha", monitored: true, have: true}, {id: 3, title: "Beta"}, } ids := func(f int, text string) string { var s string for _, it := range filterItems(items, f, text, attrFilter{}) { s += itoa(it.id) } return s } for _, tc := range []struct { filter int text string want string }{ {0, "", "231"}, // sorted case-insensitively by title {1, "", "1"}, {2, "", "2"}, {3, "", "21"}, {4, "", "3"}, {0, " ALP ", "2"}, {1, "alpha", ""}, } { if got := ids(tc.filter, tc.text); got != tc.want { t.Errorf("filter %d %q = %q, want %q", tc.filter, tc.text, got, tc.want) } } } func TestEpisodeStatus(t *testing.T) { const today = "2026-06-01" for _, tc := range []struct { e sonarr.Episode want string }{ {sonarr.Episode{HasFile: true}, "downloaded"}, {sonarr.Episode{AirDate: "2027-01-01", Monitored: true}, "unaired"}, {sonarr.Episode{Monitored: true}, "unaired"}, {sonarr.Episode{AirDate: "2020-01-01"}, "unmonitored"}, {sonarr.Episode{AirDate: "2020-01-01", Monitored: true}, "missing"}, } { if got := episodeStatus(tc.e, today); !strings.Contains(got, tc.want) { t.Errorf("%+v = %q, want %q", tc.e, got, tc.want) } } } func TestAttrFilter(t *testing.T) { uhd := arr.FileAttrs{Resolution: 2160, Codec: arr.CodecX265, HDR: true, DV: true, Languages: []string{"English", "German"}} hd := arr.FileAttrs{Resolution: 1080, Codec: arr.CodecX264, Languages: []string{"French"}} sd := arr.FileAttrs{Resolution: 480} for _, tc := range []struct { name string f attrFilter files []arr.FileAttrs want bool }{ {"inactive matches any file", attrFilter{}, []arr.FileAttrs{hd}, true}, {"no files never matches an active filter", attrFilter{res: "1080p"}, nil, false}, {"resolution", attrFilter{res: "1080p"}, []arr.FileAttrs{hd}, true}, {"resolution mismatch", attrFilter{res: "2160p"}, []arr.FileAttrs{hd}, false}, {"SD", attrFilter{res: "SD"}, []arr.FileAttrs{sd}, true}, {"SD excludes HD", attrFilter{res: "SD"}, []arr.FileAttrs{hd}, false}, {"codec", attrFilter{codec: "x265"}, []arr.FileAttrs{uhd}, true}, {"hdr includes DV+HDR", attrFilter{hdr: "HDR"}, []arr.FileAttrs{uhd}, true}, {"dv", attrFilter{hdr: "DV"}, []arr.FileAttrs{hd}, false}, {"sdr", attrFilter{hdr: "SDR"}, []arr.FileAttrs{hd}, true}, {"sdr excludes hdr", attrFilter{hdr: "SDR"}, []arr.FileAttrs{uhd}, false}, {"audio", attrFilter{audio: "Atmos"}, []arr.FileAttrs{{Audio: []string{"Atmos", "TrueHD"}}}, true}, {"audio mismatch", attrFilter{audio: "DTS"}, []arr.FileAttrs{{Audio: []string{"DD+"}}}, false}, {"language is case-insensitive", attrFilter{lang: "german"}, []arr.FileAttrs{uhd}, true}, {"all criteria must match one file", attrFilter{res: "1080p", codec: "x265"}, []arr.FileAttrs{hd, uhd}, false}, {"any file of a series", attrFilter{res: "1080p", codec: "x264"}, []arr.FileAttrs{uhd, hd}, true}, } { if got := tc.f.match(tc.files); got != tc.want { t.Errorf("%s: match = %v, want %v", tc.name, got, tc.want) } } if s := (attrFilter{res: "1080p", hdr: "DV"}).String(); s != "1080p DV" { t.Errorf("String = %q", s) } } func TestFilterItemsByAttrs(t *testing.T) { items := []libItem{ {id: 1, title: "A", attrs: []arr.FileAttrs{{Resolution: 1080, Codec: arr.CodecX265}}, attrsKnown: true}, {id: 2, title: "B", attrs: []arr.FileAttrs{{Resolution: 1080, Codec: arr.CodecX264}}, attrsKnown: true}, {id: 3, title: "C"}, // series whose files were not read yet } got := filterItems(items, 0, "", attrFilter{codec: arr.CodecX265}) if len(got) != 1 || got[0].id != 1 { t.Errorf("got %+v", got) } if got := filterItems(items, 0, "", attrFilter{}); len(got) != 3 { t.Errorf("without filter: %d items", len(got)) } } func TestMovieItemAttrs(t *testing.T) { m := radarr.Movie{ID: 1, Title: "M", HasFile: true, MovieFile: &arr.MediaFile{SceneName: "M.2160p.DV.x265-G"}} it := newMovieItem(m) if !it.attrsKnown || len(it.attrs) != 1 || !it.attrs[0].DV || it.attrs[0].Codec != arr.CodecX265 { t.Errorf("attrs = %+v", it.attrs) } if it := newMovieItem(radarr.Movie{ID: 2, Title: "N"}); !it.attrsKnown || len(it.attrs) != 0 { t.Errorf("movie without file: %+v", it) } if it := newSeriesItem(sonarr.Series{ID: 3, Title: "S", Statistics: sonarr.Statistics{EpisodeFileCount: 2}}); it.attrsKnown { t.Error("series with files must fetch its attributes") } }