package tui import ( "context" "strings" "testing" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) func panelModel(t *testing.T) Model { t.Helper() graph, err := pipeline.Parse(pipeline.Template([]string{"implementer", "reviewer", "tester", "publisher"}, true)) if err != nil { t.Fatal(err) } for i := range graph.Stages { graph.Stages[i].Output = "Output for " + graph.Stages[i].Key graph.Stages[i].Prompt = strings.Repeat("A detailed instruction for this stage. ", 50) } m := New(context.Background(), "ws://localhost/connect") m.width = 120 m.height = 30 m.status = "127.0.0.1:8080" m.keys.detail = true m.detail = &protocol.PipelineDetail{PipelineSummary: protocol.PipelineSummary{ID: 1, Name: "demo", Version: 2, Stages: 4, CreatedAt: "2026-10-06T12:00:00Z", UpdatedAt: "2026-10-06T13:00:00Z"}, LatestVersion: 2, VersionCreatedAt: "2026-10-06T13:00:00Z", Graph: graph, Versions: []protocol.VersionSummary{{Version: 2}, {Version: 1}}} return m } func TestTabbedPanesAndIndependentScrolling(t *testing.T) { m := panelModel(t) view := ansi.Strip(m.View()) t.Log("\n" + view) for _, text := range []string{"Version 2 ▾", "Edit source", "LATEST", "Graph", "Stages", "implementer"} { if !strings.Contains(view, text) { t.Fatalf("missing %q", text) } } if strings.Contains(view, "Output for reviewer") { t.Fatal("inactive stage detail is visible") } if strings.Contains(view, "INPUT") || len(m.stageFields()) != 5 { t.Fatal("stage definitions must not show an input contract") } m = press(m, "tab") if m.stageIndex != 0 || !m.stageFocus { t.Fatal("Tab must change pane, not stage") } m = press(m, "2") if m.stageIndex != 1 || !m.stageFocus || m.stageTop != 0 { t.Fatal("Number did not select the stage") } view = ansi.Strip(m.View()) if !strings.Contains(strings.Join(m.stageLines(116), "\n"), "Output for reviewer") || m.selectedStage().Key != "reviewer" { t.Fatal("stage tab did not switch content") } graphTop := m.pipelineTop m = press(m, "j") if m.stageTop != 1 || m.pipelineTop != graphTop { t.Fatal("stage scrolling affected the graph") } m = press(m, "tab") graphTop = m.pipelineTop m = press(m, "j") if m.stageFocus || m.stageTop != 1 || m.pipelineTop < graphTop || m.pipelineTop > graphTop+1 { t.Fatal("graph scrolling affected stage details") } m = press(m, "1") if m.stageIndex != 0 || m.stageTop != 0 { t.Fatal("Number did not select first stage") } m = press(m, "[") if m.stageIndex != 3 { t.Fatal("stage tabs should wrap") } m = press(m, "]") if m.stageIndex != 0 { t.Fatal("forward stage tabs should wrap") } for _, focus := range []bool{false, true} { m.stageFocus = focus m.stageTop = 2 m = press(m, "n") if m.stageIndex != 1 || m.stageTop != 0 || m.stageFocus != focus { t.Fatal("n did not select next stage/reset scroll/preserve focus") } m = press(m, "p") m = press(m, "p") if m.stageIndex != 3 { t.Fatal("p must wrap to previous stage") } m = press(m, "n") if m.stageIndex != 0 { t.Fatal("n must wrap to first stage") } } } func TestPanelResponsiveLayout(t *testing.T) { for _, size := range []tea.WindowSizeMsg{{Width: 160, Height: 45}, {Width: 120, Height: 30}, {Width: 90, Height: 18}, {Width: 80, Height: 30}, {Width: 60, Height: 24}, {Width: 40, Height: 10}, {Width: 12, Height: 5}, {Width: 1, Height: 1}} { for _, focus := range []bool{false, true} { m := panelModel(t) m.stageFocus = focus updated, _ := m.Update(size) m = updated.(Model) view := ansi.Strip(m.View()) lines := strings.Split(view, "\n") if len(lines) != size.Height { t.Fatalf("%+v focus=%v: height=%d", size, focus, len(lines)) } for _, line := range lines { if lipgloss.Width(line) > size.Width { t.Fatalf("%+v: too wide %q", size, line) } } g := m.pipelineGeometry(size.Height - 4) if size.Height >= 24 && (g.graphWidth != size.Width || g.stageWidth != size.Width || (focus && g.stageHeight <= g.graphHeight) || (!focus && g.graphHeight < g.stageHeight)) { t.Fatal("full-width panes must prioritize focused stage details while retaining graph context") } if size.Width == 80 && (!strings.Contains(view, "Graph") || !strings.Contains(view, "Stages")) { t.Fatal("narrow terminal should stack both panes") } } } } func TestPipelineTabExpandsFocusedPane(t *testing.T) { m := panelModel(t) m.width, m.height = 120, 48 g := m.pipelineGeometry(44) if g.graphHeight <= g.stageHeight*2 { t.Fatal("pipeline graph does not occupy most height", g) } m = press(m, "tab") g = m.pipelineGeometry(44) if !m.stageFocus || g.stageHeight <= g.graphHeight*2 { t.Fatal("pipeline stage pane did not expand", g) } m = press(m, "tab") g = m.pipelineGeometry(44) if m.stageFocus || g.graphHeight <= g.stageHeight*2 { t.Fatal("pipeline graph did not re-expand", g) } } func TestStageCapabilitiesAndScrolledHeadings(t *testing.T) { m := panelModel(t) m.detail.Graph.Stages[0].Notify = &protocol.NotificationPolicy{On: []string{"failure"}, Destination: "system"} _, narrow, _ := m.stageColumnsWithHeaders(76) first := strings.Join(narrow[:min(16, len(narrow))], "\n") if !strings.Contains(first, "TOOLS") || !strings.Contains(first, "NOTIFY") { t.Fatal("capabilities hidden below long prompt", first) } header, rows, labels := m.stageColumnsWithHeaders(116) if len(rows) != len(labels) { t.Fatal("missing row heading context") } if labels[0] != header || !strings.Contains(labels[len(labels)-1], "CONFIGURATION") { t.Fatal("field-group context did not follow scroll") } for i, label := range labels { if strings.Contains(label, "NOTIFY") { m.stageTop = i break } } view := ansi.Strip(m.stagePanel(120, 9)) if !strings.Contains(view, "NOTIFY") || strings.Contains(view, "CONFIGURATION") { t.Fatal("pinned columns do not match scrolled group", view) } } func TestTerminalThemePalette(t *testing.T) { previous := lipgloss.ColorProfile() lipgloss.SetColorProfile(termenv.ANSI) defer lipgloss.SetColorProfile(previous) m := panelModel(t) view := m.View() if !strings.Contains(view, "\x1b[") { t.Fatal("expected theme colors") } if strings.Contains(view, "38;5;") || strings.Contains(view, "38;2;") || strings.Contains(view, "48;5;") || strings.Contains(view, "48;2;") { t.Fatal("UI should use ANSI theme colors, not fixed 256-color or RGB values") } if lipgloss.Width(m.View()) != m.width { t.Fatal("ANSI styling changed layout width") } } func TestStageOutcomeArrowsAlign(t *testing.T) { m := panelModel(t) m.detail.Graph = protocol.Graph{Stages: []protocol.Stage{ {Key: "alpha", Edges: map[string]string{ "one": "#success", "two": "#success", "three": "#success", "four": "#success", "five": "#success", "six": "#success", "seven": "#success", "eight": "#success", "nine": "#success", }}, {Key: "beta", FallbackError: true, Edges: map[string]string{ "success": "#success", "error": "#failure", "manual_review": "gamma", "very_long_outcome": "gamma", "宽": "gamma", }}, {Key: "gamma", Edges: map[string]string{"success": "#success"}}, }} m.stageIndex = 1 fields := m.stageFields() var outcomes string for _, field := range fields { if field.title == "OUTCOMES" { outcomes = field.text } } lines := strings.Split(ansi.Strip(outcomes), "\n") if len(lines) < 4 { t.Fatalf("expected several outcome routes, got %d: %q", len(lines), outcomes) } columns := make([]int, len(lines)) for i, line := range lines { idx := strings.Index(line, "→") if idx < 0 { t.Fatalf("route %q has no arrow", line) } columns[i] = lipgloss.Width(line[:idx]) } for i := 1; i < len(columns); i++ { if columns[i] != columns[0] { t.Fatalf("arrow column mismatch: route %q arrow at %d, want %d (all routes: %q)", lines[i], columns[i], columns[0], lines) } } } func TestSelectedTabRemainsVisible(t *testing.T) { m := panelModel(t) m.stageIndex = 3 for _, width := range []int{12, 24, 60} { tabs := ansi.Strip(m.stageTabs(width)) if !strings.Contains(tabs, "publisher") { t.Fatalf("selected tab missing at width %d: %q", width, tabs) } if lipgloss.Width(tabs) > width { t.Fatal("tabs overflow") } } }