package tui import ( "context" "net/http/httptest" "strings" "testing" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/server" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" ) func TestHomeShortcutOpensAllRunsWithoutClutteringPanels(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() srv := httptest.NewServer(server.Handler(ctx)) defer srv.Close() m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") connected := m.connect()().(connectedMsg) defer connected.conn.CloseNow() updated, _ := m.Update(connected) m = updated.(Model) m.width = 120 m.height = 40 m.runs = []protocol.RunSummary{{ID: 1, Status: "succeeded", Name: "passed"}, {ID: 2, Status: "failed", Name: "broken", FinishedAt: "2026-10-09T00:00:00Z"}, {ID: 3, Status: "running", Name: "active"}, {ID: 4, Status: "canceled", Name: "canceled"}, {ID: 5, Status: "failed", Name: "also broken", FinishedAt: "2026-10-09T00:00:00Z"}} view := ansi.Strip(strings.Join(m.homeLines(), "\n")) if strings.Contains(view, "r all runs") || strings.Contains(view, "r failed runs") { t.Fatal("shortcut hints clutter home panels", view) } updated, send := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) m = updated.(Model) if send == nil || !m.runOpen || !m.keys.runs || m.runFailedOnly || m.runIndex != 0 { t.Fatal("home shortcut did not open all runs") } if _, ok := send().(sentMsg); !ok { t.Fatal("failed list refresh request failed") } visible := m.visibleRuns() if len(visible) != 5 || visible[0].ID != 1 || visible[1].ID != 2 { t.Fatal("home runs were filtered", visible) } view = ansi.Strip(m.runsView(30)) if strings.Contains(view, "Failed runs") || !strings.Contains(view, "passed") || !strings.Contains(view, "active") { t.Fatal("all runs view is incorrect", view) } m.runIndex = 1 updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) m = updated.(Model) if cmd != nil || m.actionConfirm == nil || m.actionConfirm.Type != protocol.RetryRun { t.Fatal("r on failed list should request retry confirmation") } m.closeRuns() if m.runFailedOnly { t.Fatal("failed filter leaked after closing list") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}}) m = updated.(Model) m.commandInput = newInput() m.commandInput.SetValue(":runs") updated, _ = m.updatePalette(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if m.runFailedOnly || len(m.visibleRuns()) != 5 { t.Fatal("normal runs command retained failed filter") } } func TestFailedRunsEmptyAndRefreshRemainFiltered(t *testing.T) { m := New(context.Background(), "") m.width = 100 m.runOpen = true m.keys.runs = true m.runFailedOnly = true if !strings.Contains(ansi.Strip(m.runsView(20)), "No failed runs.") { t.Fatal("missing failed-only empty state") } updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.Runs, Runs: []protocol.RunSummary{{ID: 1, Status: "running"}, {ID: 2, Status: "failed"}}})) m = updated.(Model) if visible := m.visibleRuns(); len(visible) != 1 || visible[0].ID != 2 { t.Fatal("refresh lost failed filter", visible) } m.runDetail = &protocol.RunDetail{RunSummary: m.visibleRuns()[0]} m.requestedRunID = 2 updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc}) m = updated.(Model) if !m.runOpen || !m.runFailedOnly || m.runDetail != nil { t.Fatal("back from inspector lost failed list") } updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc}) m = updated.(Model) if !m.homePage() || m.runFailedOnly { t.Fatal("failed list did not return home") } }