package harness import ( "bytes" "context" "strings" "testing" "time" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" "github.com/muesli/termenv" ) func pickerWith(options []string, query, current string) picker { q := textinput.New() q.SetValue(query) // Production Pick focuses the input; without Focus, textinput.Update drops // typed runes, so the filter-change assertion could never be exercised. q.Focus() return picker{title: "Choose the interactive OpenCode model", options: options, query: q, current: current, width: 80, height: 20, plain: true} } func TestPickerWindowCentersOnActiveItem(t *testing.T) { options := make([]string, 30) for i := range options { options[i] = "provider/model-" + string(rune('a'+i%26)) } p := pickerWith(options, "", "") p.height = 20 rows := p.visibleRows() if rows != maxVisibleRows { t.Fatalf("visible rows = %d", rows) } for _, test := range []struct{ index, want int }{{0, 0}, {5, 0}, {10, 4}, {20, 14}, {29, 18}} { p.index = test.index if got := p.windowStart(len(options), rows); got != test.want { t.Fatalf("index %d: window start = %d, want %d", test.index, got, test.want) } if test.index < p.windowStart(len(options), rows) || test.index >= p.windowStart(len(options), rows)+rows { t.Fatalf("index %d is outside its own window", test.index) } } // A short terminal keeps a small but nonzero window sized to the measured // heading and fixed chrome (1 title line + 5 chrome = 6; 8-6 = 2). p.height = 8 if got := p.visibleRows(); got != 2 { t.Fatalf("short terminal rows = %d, want 2", got) } } func TestPickerPlainRenderingHasNoANSI(t *testing.T) { p := pickerWith([]string{"opencode-go/deepseek-v4.1-flash", "other/model"}, "", "opencode-go/deepseek-v4.1-flash") view := p.View() if strings.Contains(view, "\x1b[") { t.Fatal("plain picker emitted ANSI") } for _, want := range []string{"Choose the interactive OpenCode model", "Filter:", "› ", "(current)", "Type to filter"} { if !strings.Contains(view, want) { t.Fatalf("plain picker missing %q\n%s", want, view) } } } func TestPickerNoMatchStateIsExplicit(t *testing.T) { p := pickerWith([]string{"opencode-go/deepseek-v4.1-flash"}, "zzzz", "") view := p.View() if !strings.Contains(view, "No models match") || !strings.Contains(view, "defaults are unchanged") { t.Fatalf("missing no-match state\n%s", view) } if matches := p.matches(); len(matches) != 0 { t.Fatalf("unexpected matches: %v", matches) } } func TestPickerNarrowWidthAndLongNamesDoNotOverflow(t *testing.T) { long := strings.Repeat("provider/", 4) + strings.Repeat("x", 60) p := pickerWith([]string{long, "short/model"}, "", long) p.width = 30 for _, line := range strings.Split(p.View(), "\n") { if got := ansi.StringWidth(line); got > 30 { t.Fatalf("narrow picker line width %d > 30: %q", got, line) } } if !strings.Contains(p.View(), "…") { t.Fatal("long model name was not truncated") } } func TestPickerStyledSelectionUsesBackgroundHighlight(t *testing.T) { previous := lipgloss.ColorProfile() lipgloss.SetColorProfile(termenv.TrueColor) defer lipgloss.SetColorProfile(previous) p := pickerWith([]string{"opencode-go/deepseek-v4.1-flash"}, "", "") p.plain = false view := p.View() if !strings.Contains(view, "\x1b[") { t.Fatal("styled picker omitted ANSI") } if !strings.Contains(view, "› ") { t.Fatal("styled picker omitted the textual selection marker") } if !strings.Contains(view, "48;2;") && !strings.Contains(view, "48;5;") { t.Fatalf("TrueColor styled picker lost its background highlight: %q", view) } } func TestPreselectIndexFindsSavedModel(t *testing.T) { options := []string{"a/one", "b/two", "c/three"} if got := preselectIndex(options, "b/two"); got != 1 { t.Fatalf("preselect = %d", got) } if got := preselectIndex(options, "missing/model"); got != 0 { t.Fatalf("missing preselect = %d", got) } } func TestPickFailsFastWithoutTerminal(t *testing.T) { var out bytes.Buffer // A short deadline keeps a TTY-detection regression from hanging the suite. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() _, err := Pick(ctx, Selection{Title: "Model", Options: []string{"a/b"}}, bytes.NewBuffer(nil), &out) if err == nil || !strings.Contains(err.Error(), "requires a terminal") { t.Fatalf("non-terminal picker error = %v", err) } if _, err := Pick(ctx, Selection{Title: "Model"}, bytes.NewBuffer(nil), &out); err == nil || !strings.Contains(err.Error(), "no available models") { t.Fatalf("empty catalog error = %v", err) } } func TestNoColorEnvironmentSuppressesStyles(t *testing.T) { t.Setenv("NO_COLOR", "1") if !plainOutput() { t.Fatal("NO_COLOR was not honored") } p := pickerWith([]string{"opencode-go/deepseek-v4.1-flash"}, "", "") p.plain = plainOutput() if strings.Contains(p.View(), "\x1b[") { t.Fatal("NO_COLOR picker render emitted ANSI") } } func TestOnboardingPlainOutputHasHandoffAndSummary(t *testing.T) { var out bytes.Buffer o := NewOnboarding(&out) o.Header("pi", "Configure") o.Versions("OpenCode 2.0.26 · Pi 1.1.0 · Node 22.19.0") o.AuthHandoff() o.Summary("pi", "provider/interactive", "provider/stages", "pi", "host") text := out.String() if strings.Contains(text, "\x1b[") { t.Fatalf("non-terminal onboarding emitted ANSI: %q", text) } for _, want := range []string{"Pipelines harness setup", "Docker image harnesses:", "OpenCode 2.0.26", "Pi sign-in", "/login", "authentication method", "Sign in with an API key", "ctrl+c/ctrl+d", "reuse them", "provider sign-in", "Configuration complete", "provider/interactive", "provider/stages"} { if !strings.Contains(text, want) { t.Fatalf("onboarding missing %q\n%s", want, text) } } } func TestPickerPreservesIndexAcrossBlinkAndResetsOnFilterChange(t *testing.T) { p := pickerWith([]string{"a/one", "b/two", "c/three"}, "", "c/three") p.index = 2 // saved/arrow-selected model for i := 0; i < 5; i++ { next, _ := p.Update(textinput.Blink()) p = next.(picker) if p.index != 2 { t.Fatalf("cursor blink reset index to %d (want 2)", p.index) } } next, _ := p.Update(struct{ unrelated string }{"message"}) p = next.(picker) if p.index != 2 { t.Fatalf("unrelated message reset index to %d (want 2)", p.index) } // The saved model is still the active selection after several blinks. if selected := p.matches()[p.index]; selected != "c/three" { t.Fatalf("saved model selection drifted to %q", selected) } // Typing a matching filter resets the active row to the first match. next, _ = p.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'a'}}) p = next.(picker) if p.index != 0 { t.Fatalf("filter change did not reset index: %d", p.index) } } func TestPickerSelectedRowReadableAcrossColorProfiles(t *testing.T) { previousProfile := lipgloss.ColorProfile() previousDark := lipgloss.HasDarkBackground() defer func() { lipgloss.SetColorProfile(previousProfile) lipgloss.SetHasDarkBackground(previousDark) }() p := pickerWith([]string{"opencode-go/deepseek-v4.1-flash", "other/model"}, "", "") p.plain = false lipgloss.SetHasDarkBackground(true) lipgloss.SetColorProfile(termenv.ANSI) ansiView := p.View() if !strings.Contains(ansiView, "› ") { t.Fatalf("ANSI picker omitted the textual selection marker: %q", ansiView) } for _, collision := range []string{"[46m", ";46", "48;", "48;5", "48;2"} { if strings.Contains(ansiView, collision) { t.Fatalf("ANSI selected row kept background %q (cyan-on-cyan): %q", collision, ansiView) } } if !strings.Contains(ansiView, "\x1b[1m") { t.Fatalf("ANSI selected row lost its bold emphasis: %q", ansiView) } lipgloss.SetColorProfile(termenv.ANSI256) ansi256View := p.View() if !strings.Contains(ansi256View, "48;5;") && !strings.Contains(ansi256View, "48;2;") { t.Fatalf("ANSI256 selected row lost its adaptive background: %q", ansi256View) } lipgloss.SetColorProfile(termenv.TrueColor) trueView := p.View() if !strings.Contains(trueView, "48;2;") && !strings.Contains(trueView, "48;5;") { t.Fatalf("TrueColor selected row lost its adaptive background: %q", trueView) } } func TestPickerLayoutFitsRealTerminals(t *testing.T) { options := []string{ "opencode-go/deepseek-v4.1-flash", "anthropic/claude-sonnet-4-5-20250929", "openai/gpt-5.1-codex", } longQuery := strings.Repeat("very-long-no-match-query-", 20) cases := []struct { width, height int query string }{ {40, 16, ""}, {40, 16, longQuery}, {80, 24, ""}, {120, 36, ""}, } for _, tc := range cases { p := pickerWith(options, tc.query, options[1]) p.width, p.height = tc.width, tc.height p.step = "Step 1 of 2" p.title = "Choose the interactive OpenCode model" p.purpose = "Used when you open OpenCode for authoring and debugging sessions. It is stored separately from the new-pipeline-stage model." p.index = 1 view := p.View() lines := strings.Split(strings.TrimRight(view, "\n"), "\n") if len(lines) > tc.height { t.Fatalf("%dx%d rendered %d lines (max %d):\n%s", tc.width, tc.height, len(lines), tc.height, view) } for _, line := range lines { if got := ansi.StringWidth(line); got > tc.width { t.Fatalf("%dx%d line width %d > %d: %q", tc.width, tc.height, got, tc.width, line) } } if !strings.Contains(view, "Step 1 of 2") { t.Fatalf("%dx%d lost the step/heading:\n%s", tc.width, tc.height, view) } if len(p.matches()) > 0 && !strings.Contains(view, "› ") { t.Fatalf("%dx%d lost the selected row:\n%s", tc.width, tc.height, view) } } }