| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "charm.land/lipgloss/v2" |
| 9 | "github.com/charmbracelet/x/ansi" |
| 10 | |
| 11 | "reasonix/internal/skill" |
| 12 | ) |
| 13 | |
| 14 | func TestPanelFocusSurvivesWrappingAndPresentation(t *testing.T) { |
| 15 | for _, width := range []int{20, 40, 80} { |
| 16 | for _, style := range []lipgloss.Style{lipgloss.NewStyle(), choicePanelStyle.Width(40), managerContentPanelStyle(40)} { |
| 17 | for _, marker := range []string{"", "› ", "❯ ", "任意符号 "} { |
| 18 | t.Run(fmt.Sprintf("width=%d/border=%d/marker=%q", width, style.GetBorderBottomSize(), marker), func(t *testing.T) { |
| 19 | var b panelBuilder |
| 20 | b.WriteString("› misleading text\n" + strings.Repeat("中文 é 1️⃣ text ", 12) + "\n\n") |
| 21 | b.MarkFocus(true, 7) |
| 22 | b.WriteString(accent(marker+"TARGET") + "\n" + strings.Repeat("tail\n", 5)) |
| 23 | p := b.render(style, width) |
| 24 | rows := strings.Split(wrapTerminalText(p.text, width), "\n") |
| 25 | if p.focus < 0 || p.focus >= len(rows) || !strings.Contains(ansi.Strip(rows[p.focus]), "TARGET") { |
| 26 | t.Fatalf("focus %d missed selected row:\n%s", p.focus, ansi.Strip(strings.Join(rows, "\n"))) |
| 27 | } |
| 28 | if p.selection != 7 { |
| 29 | t.Fatalf("lost logical selection: %d", p.selection) |
| 30 | } |
| 31 | if p.text != style.Render(b.String()) { |
| 32 | t.Fatal("focus metadata changed the displayed content") |
| 33 | } |
| 34 | }) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestNativeManagerReportsSelectedRow(t *testing.T) { |
| 41 | for _, width := range []int{20, 40, 80} { |
| 42 | for _, kind := range []string{"mcp-list", "mcp-confirm", "skills"} { |
| 43 | t.Run(fmt.Sprintf("%s/%d", kind, width), func(t *testing.T) { |
| 44 | m := chatTUI{width: width} |
| 45 | want := "TARGET" |
| 46 | switch kind { |
| 47 | case "mcp-list": |
| 48 | m.mcp = &mcpManager{sel: 1, snapshot: mcpSnapshot{servers: []mcpServerView{{Name: "Other"}, {Name: want}}}} |
| 49 | case "mcp-confirm": |
| 50 | m.mcp = &mcpManager{stage: mcpStageConfirmRemove, confirm: 1, snapshot: mcpSnapshot{servers: []mcpServerView{{Name: strings.Repeat("Long name ", 10)}}}} |
| 51 | want = "Cancel" |
| 52 | case "skills": |
| 53 | m.skillPick = &skillPicker{mode: pickerSkills, sel: 1, skills: []skill.Skill{{Name: "Other", Scope: skill.ScopeProject}, {Name: want, Scope: skill.ScopeGlobal}}} |
| 54 | } |
| 55 | p := m.renderMainManagerPanel() |
| 56 | rows := strings.Split(wrapTerminalText(p.text, width), "\n") |
| 57 | if p.focus < 0 || p.focus >= len(rows) || !strings.Contains(ansi.Strip(rows[p.focus]), want) { |
| 58 | t.Fatalf("focus %d missed %q:\n%s", p.focus, want, ansi.Strip(strings.Join(rows, "\n"))) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 |