package console import ( "net/url" "testing" ) func rowsFor(hosts ...string) []InstanceRow { out := make([]InstanceRow, len(hosts)) for i, h := range hosts { out[i] = InstanceRow{Host: h, HealthWord: "Running"} } return out } func TestHealthSlugRoundTrips(t *testing.T) { for _, w := range healthWords { if got := healthFromSlug(healthSlug(w)); got != w { t.Fatalf("%q -> %q -> %q", w, healthSlug(w), got) } } if healthFromSlug("nonsense") != "" { t.Fatal("unknown slug must mean no filter, not an error") } } func TestHealthToneUsesExistingChipStyles(t *testing.T) { tests := map[string]string{ "Running": "ok", "Serving": "ok", "Failed": "attention", "Not responding": "attention", "Asleep": "notdeployed", "Setting up": "notdeployed", "No readings": "unknown", } for word, want := range tests { if got := healthTone(word); got != want { t.Errorf("healthTone(%q) = %q, want %q", word, got, want) } } } func TestPageFiltersAndCounts(t *testing.T) { rows := rowsFor("a.x", "ab.x", "b.x") rows[2].HealthWord = "Asleep" p := pageInstanceRows(rows, instanceQuery{Q: "a"}, 50) if p.Total != 2 || len(p.Rows) != 2 || p.NextCursor != "" { t.Fatalf("%+v", p) } if p.Counts["Running"] != 2 || p.Counts["Asleep"] != 1 { t.Fatalf("counts are over ALL rows, filtered or not: %+v", p.Counts) } p = pageInstanceRows(rows, instanceQuery{Health: "Asleep"}, 50) if p.Total != 1 || p.Rows[0].Host != "b.x" { t.Fatalf("%+v", p) } } func TestPageCursors(t *testing.T) { rows := rowsFor("a.x", "b.x", "c.x", "d.x", "e.x") p1 := pageInstanceRows(rows, instanceQuery{}, 2) if len(p1.Rows) != 2 || p1.NextCursor == "" || p1.Rows[1].Host != "b.x" { t.Fatalf("page1: %+v", p1) } p2 := pageInstanceRows(rows, instanceQuery{Cursor: p1.NextCursor}, 2) if len(p2.Rows) != 2 || p2.Rows[0].Host != "c.x" { t.Fatalf("page2: %+v", p2) } p3 := pageInstanceRows(rows, instanceQuery{Cursor: p2.NextCursor}, 2) if len(p3.Rows) != 1 || p3.Rows[0].Host != "e.x" || p3.NextCursor != "" { t.Fatalf("page3: %+v", p3) } } func TestPageGarbageCursorStartsOver(t *testing.T) { rows := rowsFor("a.x", "b.x") p := pageInstanceRows(rows, instanceQuery{Cursor: "!!not-base64!!"}, 50) if len(p.Rows) != 2 { t.Fatalf("bad cursor must degrade to page one: %+v", p) } } func TestPageAcceptsLegacyCursorAndPastLastIsEmpty(t *testing.T) { rows := rowsFor("a.x", "b.x") legacy := encodeInstanceCursor("a.x") p := pageInstanceRows(rows, instanceQuery{Cursor: legacy}, 50) if len(p.Rows) != 1 || p.Rows[0].Host != "b.x" { t.Fatalf("legacy cursor: %+v", p) } p = pageInstanceRows(rows, instanceQuery{Cursor: encodeInstanceCursor("z.x")}, 50) if len(p.Rows) != 0 || p.NextCursor != "" { t.Fatalf("past-last cursor: %+v", p) } } func TestParseInstanceQuery(t *testing.T) { q := parseInstanceQuery(url.Values{"q": {" api "}, "health": {"not-responding"}, "cursor": {"abc"}}) if q.Q != "api" || q.Health != "Not responding" || q.Cursor != "abc" { t.Fatalf("%+v", q) } }