package tui import ( "context" "fmt" "strings" "testing" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "git.a-wan.net/alan/pipelines/internal/protocol" ) func press(m Model, key string) Model { msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(key)} if key == "enter" { msg = tea.KeyMsg{Type: tea.KeyEnter} } if key == "esc" { msg = tea.KeyMsg{Type: tea.KeyEsc} } updated, _ := m.Update(msg) return updated.(Model) } func TestPaletteAndUnreadLogs(t *testing.T) { m := New(context.Background(), "ws://localhost/connect") m.height = 18 if !strings.Contains(ansi.Strip(m.footerContent()), "LOGS 0") { t.Fatal("expected empty log count") } for i := 0; i < 35; i++ { m.appendLine(fmt.Sprintf("line %02d", i)) } if strings.Contains(m.View(), "line 00") || m.unread != 35 { t.Fatal("logs leaked into home or unread count wrong") } m = press(m, ":") m = press(m, "lgs") if matches := m.matches(); len(matches) != 1 || matches[0] != ":logs" { t.Fatalf("fuzzy matches: %v", matches) } m = press(m, "enter") if !m.logOpen || m.paletteOpen || m.logTop != 25 || m.unread != 25 { t.Fatalf("open: top=%d unread=%d", m.logTop, m.unread) } if !strings.Contains(m.View(), "line 34") { t.Fatal("logs not rendered") } m = press(m, "h") if m.logTop != 20 || m.unread != 20 { t.Fatalf("h: top=%d unread=%d", m.logTop, m.unread) } m = press(m, "k") if m.logTop != 19 || m.unread != 19 { t.Fatal("k did not scroll a line") } m = press(m, "j") if m.logTop != 20 || m.unread != 19 { t.Fatal("j or read tracking wrong") } m = press(m, "l") if m.logTop != 25 { t.Fatal("l did not scroll half a page") } for m.logTop > 0 { m = press(m, "h") } if m.unread != 0 { t.Fatalf("old visible logs remain unread: %d", m.unread) } m.appendLine("arrived while scrolled up") if m.unread != 1 || m.logTop != 0 { t.Fatal("new log should stay unread without disturbing scroll") } for m.logTop < len(m.lines)-m.logPageSize() { m = press(m, "l") } if m.unread != 0 { t.Fatal("new log not marked read") } m.appendLine("live line") if m.unread != 0 || m.logTop != len(m.lines)-m.logPageSize() { t.Fatal("bottom did not follow live logs") } m = press(m, "esc") m.appendLine("after closing\nsecond line") if m.logOpen || m.unread != 2 || !strings.Contains(ansi.Strip(m.footerContent()), "● 2 NEW · LOGS") { t.Fatal("unread footer wrong") } } func TestLogBadgeRetainsReadLogs(t *testing.T) { m := New(context.Background(), "ws://localhost/connect") m.appendLine("connection lost") if !strings.Contains(ansi.Strip(m.footerContent()), "● 1 NEW · LOGS 1") { t.Fatal("unread logs need an explicit badge") } m = press(m, ":") m = press(m, "logs") m = press(m, "enter") m = press(m, "esc") if m.unread != 0 || !strings.Contains(ansi.Strip(m.footerContent()), "LOGS 1") || strings.Contains(ansi.Strip(m.footerContent()), "NEW") { t.Fatal("read logs must remain discoverable") } for _, width := range []int{1, 12, 40, 80} { m.width = width if lipgloss.Width(m.footerContent()) > width { t.Fatal("log badge overflows footer") } } } func TestHomeShowsEmptyLatestRunsBeforePipelines(t *testing.T) { m := New(context.Background(), "ws://localhost/connect") m.width = 120 m.height = 30 m.loaded = true m.pipelines = []protocol.PipelineSummary{{Name: "bug fix", Version: 3, Stages: 5}} view := ansi.Strip(m.View()) for _, text := range []string{"Latest runs", "Outcome", "No runs yet.", "Existing pipelines", "bug fix"} { if !strings.Contains(view, text) { t.Fatalf("missing %q", text) } } if strings.Index(view, "Latest runs") > strings.Index(view, "Existing pipelines") { t.Fatal("recent runs should be the primary home section") } if strings.Contains(view, "SUCCESS") || strings.Contains(view, "FAILURE") { t.Fatal("home must not fabricate execution outcomes") } } func TestHomeOnlyAdvertisesSearchAndCommands(t *testing.T) { for _, populated := range []bool{false, true} { m := New(context.Background(), "ws://localhost/connect") m.width, m.height = 120, 40 m.loaded = true if populated { m.pipelines = []protocol.PipelineSummary{{Name: "demo", Version: 1, Stages: 3}} m.usage = &protocol.UsageStats{} } view := ansi.Strip(m.View()) for _, hint := range []string{"/ omni search", ": commands"} { if !strings.Contains(view, hint) { t.Fatal("missing home hint", hint, view) } } for _, hint := range []string{"r all runs", "n new", "d drafts", "b templates", "test ping", ":quit", ":status", ":logs", "Press b"} { if strings.Contains(view, hint) { t.Fatal("home advertises extra shortcut", hint, view) } } if len(m.keys.ShortHelp()) != 2 { t.Fatal("home help contains extra shortcuts") } available := m.pageCommands() for _, command := range []string{":new", ":drafts", ":templates", ":runs", ":status", ":logs", ":quit"} { if !strings.Contains(strings.Join(available, " "), command) { t.Fatal("home action missing from commands", command) } } m = press(m, ":") if !m.paletteOpen { t.Fatal("command palette inaccessible") } } } func TestDashboardAndModalDimensions(t *testing.T) { for _, size := range []tea.WindowSizeMsg{{Width: 100, Height: 30}, {Width: 40, Height: 10}, {Width: 12, Height: 5}, {Width: 1, Height: 1}} { for _, mode := range []string{"home", "palette", "logs"} { m := New(context.Background(), "ws://localhost/connect") updated, _ := m.Update(size) m = updated.(Model) m.loaded = true m.pipelines = []protocol.PipelineSummary{{Name: "demo", Version: 2, Stages: 3, CreatedAt: "2026-10-06T10:00:00Z", UpdatedAt: "2026-10-07T10:00:00Z"}} m.appendLine("private log") if mode == "palette" { m = press(m, ":") } if mode == "logs" { m = press(m, ":") m = press(m, "logs") m = press(m, "enter") } view := ansi.Strip(m.View()) lines := strings.Split(view, "\n") if len(lines) != size.Height { t.Fatalf("%s %+v: height=%d", mode, size, len(lines)) } for _, line := range lines { if lipgloss.Width(line) > size.Width { t.Fatalf("%s %+v: too wide: %q", mode, size, line) } } if size.Width == 100 && mode == "home" { for _, text := range []string{"Existing pipelines", "demo", "v2", "3 stages", "Not run yet"} { if !strings.Contains(view, text) { t.Fatalf("missing %q", text) } } if strings.Contains(view, "private log") { t.Fatal("logs displayed on home") } } } } }