internal/tui/view/filterbar/filterbar_test.go

package filterbar

import (
	"testing"

	tea "github.com/charmbracelet/bubbletea"

	"mercemay.top/httptap/internal/tui/theme"
)

func typeRunes(m Model, s string) Model {
	for _, r := range s {
		n, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
		m = n
	}
	return m
}

func TestFilterbarCaptureValue(t *testing.T) {
	m := New(theme.Default())
	m.SetWidth(40)
	_ = m.Focus()
	m = typeRunes(m, "host=x")
	if got := m.Expr(); got != "host=x" {
		t.Fatalf("expr = %q", got)
	}
}

func TestFilterbarEscClears(t *testing.T) {
	m := New(theme.Default())
	m.SetWidth(40)
	_ = m.Focus()
	m = typeRunes(m, "status>=400")
	next, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
	m = next
	if m.Expr() != "" {
		t.Errorf("expected cleared, got %q", m.Expr())
	}
}

func TestFilterbarEnterBlurs(t *testing.T) {
	m := New(theme.Default())
	_ = m.Focus()
	if !m.Focused() {
		t.Fatal("focus failed")
	}
	next, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	if next.Focused() {
		t.Fatal("enter should blur")
	}
}