package tui import ( "strings" "testing" ) // Guards against the bubbles bug where a text input with width 0 renders only the // first rune of its placeholder. NewFormStep must set a non-zero width. func TestPlaceholderRendersBeyondFirstRune(t *testing.T) { const placeholder = "postgres://user@host/db" f := NewFormStep("t", []FormField{ {Id: "url", Label: "URL", Placeholder: placeholder, Type: FieldTypeText}, }) view := f.Fields[0].input.View() if !strings.Contains(view, "ostgres://user@host/db") { t.Errorf("placeholder appears truncated; rendered view = %q", view) } } func TestFormStepValues(t *testing.T) { f := NewFormStep("Test", []FormField{ { Id: "name", Label: "Name", Default: "john", Type: FieldTypeText, }, { Id: "secret", Label: "Secret", Default: "hunter2", Type: FieldTypePassword, }, { Id: "proto", Label: "Protocol", Default: "TCP", Type: FieldTypeSelect, Options: []string{"UDP", "TCP"}, }, { Id: "mode", Label: "Mode", Type: FieldTypeSelect, Options: []string{"a", "b", "c"}, }, }) values := f.Values() want := map[string]string{ "name": "john", // text default "secret": "hunter2", "proto": "TCP", // select default resolves to the matching option "mode": "a", // select with no default falls back to first option } for id, exp := range want { if got := values[id]; got != exp { t.Errorf("Values()[%q] = %q, want %q", id, got, exp) } } } func TestFormStepValues_SelectInvalidDefaultFallsBackToFirst(t *testing.T) { f := NewFormStep("Test", []FormField{ { Id: "proto", Label: "Protocol", Default: "NOPE", // not among options Type: FieldTypeSelect, Options: []string{"UDP", "TCP"}, }, }) if got := f.Values()["proto"]; got != "UDP" { t.Errorf("Values()[proto] = %q, want first option %q", got, "UDP") } }