Files
arr-tui/internal/ui/notify_test.go
T

79 lines
2.8 KiB
Go

package ui
import (
"git.stinnesbeck.com/nils/arr-tui/internal/sabnzbd"
"testing"
"github.com/rivo/tview"
)
func TestPlainText(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{"[green]Done[-]", "Done"},
{"[red]" + tview.Escape("Movie [1080p] failed"), "Movie [1080p] failed"},
{"plain", "plain"},
{"[yellow]a [::b]b[::-]", "a b"},
} {
if got := plainText(tc.in); got != tc.want {
t.Errorf("plainText(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestToastProgress(t *testing.T) {
if !(toast{text: "Searching…"}).progress() || (toast{text: "Done"}).progress() {
t.Error("progress detection wrong")
}
}
func TestOnlyFullOverlays(t *testing.T) {
a := &App{}
if !a.onlyFullOverlays() {
t.Error("no overlays: view shortcuts must work")
}
a.overlays = []overlay{{full: true}, {full: true}}
if !a.onlyFullOverlays() {
t.Error("full-screen views must not block view shortcuts")
}
a.overlays = append(a.overlays, overlay{})
if a.onlyFullOverlays() {
t.Error("a dialog must block view shortcuts")
}
}
func TestDiffHistory(t *testing.T) {
slot := func(id, name, status string) sabnzbd.HistorySlot {
return sabnzbd.HistorySlot{ID: id, Name: name, Status: status, FailMessage: map[bool]string{true: "disk full"}[status == "Failed"]}
}
seen := map[string]string{}
// The first poll only records what is already there.
if ev := diffHistory(seen, []sabnzbd.HistorySlot{slot("1", "old", "Completed"), slot("2", "old fail", "Failed")}, true); len(ev) != 0 {
t.Fatalf("baseline produced %v", ev)
}
// Nothing new.
if ev := diffHistory(seen, []sabnzbd.HistorySlot{slot("1", "old", "Completed")}, false); len(ev) != 0 {
t.Fatalf("unchanged history produced %v", ev)
}
// A new job that is still being processed is not final yet.
if ev := diffHistory(seen, []sabnzbd.HistorySlot{slot("3", "movie", "Extracting")}, false); len(ev) != 0 {
t.Fatalf("extracting produced %v", ev)
}
// It completes, a second one fails, a third appears already completed.
ev := diffHistory(seen, []sabnzbd.HistorySlot{
slot("3", "movie", "Completed"), slot("4", "show", "Failed"), slot("5", "quick", "Completed"), slot("1", "old", "Completed"),
}, false)
if len(ev) != 3 || ev[0].name != "movie" || ev[0].failed || ev[1].name != "show" || !ev[1].failed || ev[1].message != "disk full" || ev[2].name != "quick" {
t.Fatalf("events = %+v", ev)
}
// Reported once only.
if ev := diffHistory(seen, []sabnzbd.HistorySlot{slot("3", "movie", "Completed"), slot("4", "show", "Failed")}, false); len(ev) != 0 {
t.Errorf("repeated: %+v", ev)
}
// A retried job that completes later is reported again.
diffHistory(seen, []sabnzbd.HistorySlot{slot("4", "show", "Queued")}, false)
if ev := diffHistory(seen, []sabnzbd.HistorySlot{slot("4", "show", "Completed")}, false); len(ev) != 1 {
t.Errorf("retry: %+v", ev)
}
}