vcom/internal/testutil/teadriver/driver_test.go
vrubelroman 1f6f400276 test: add teadriver and behavioural smoke tests for the old UI
teadriver feeds keys/mouse to Update, runs returned commands concurrently
(unwrapping Batch and Sequence), feeds results back until quiet, skips
long timers, records Quit and ExecProcess requests, and renders View
without ANSI under an Ascii profile (WithProfile for others).

Smoke tests cover navigation, mkdir, copy, rename, filter and delete on
t.TempDir(); PgDn and Cyrillic input are skipped until task 1.6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:01:22 +03:00

107 lines
2.4 KiB
Go

package teadriver
import (
"strings"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type note string
type toy struct {
got []string
keys []string
}
func (m toy) Init() tea.Cmd { return func() tea.Msg { return note("init") } }
func emit(s string) tea.Cmd { return func() tea.Msg { return note(s) } }
func (m toy) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case note:
m.got = append(m.got, string(msg))
if msg == "a" {
return m, emit("a-followup")
}
case tea.KeyMsg:
m.keys = append(m.keys, msg.String())
switch msg.String() {
case "x":
return m, tea.Batch(
emit("a"),
tea.Batch(emit("b"), nil, emit("c")),
tea.Sequence(emit("s1"), emit("s2")),
tea.Tick(time.Hour, func(time.Time) tea.Msg { return note("never") }),
)
case "q":
return m, tea.Quit
}
}
return m, nil
}
func (m toy) View() string { return "\x1b[1mgot:\x1b[0m " + strings.Join(m.got, ",") }
func TestDriverSettleBatch(t *testing.T) {
d := New(t, toy{}, 80, 24, WithLongCmd(100*time.Millisecond))
d.Settle(time.Second)
d.Press("x")
t0 := time.Now()
d.Settle(5 * time.Second)
if el := time.Since(t0); el > 2*time.Second {
t.Fatalf("Settle waited for the long tick: %v", el)
}
m := d.Model().(toy)
have := map[string]bool{}
for _, g := range m.got {
have[g] = true
}
for _, want := range []string{"init", "a", "a-followup", "b", "c", "s1", "s2"} {
if !have[want] {
t.Errorf("missing %q in %v", want, m.got)
}
}
if have["never"] {
t.Error("hour-long tick must not be awaited")
}
idx := func(s string) int {
for i, g := range m.got {
if g == s {
return i
}
}
return -1
}
if idx("s1") > idx("s2") {
t.Errorf("sequence out of order: %v", m.got)
}
if s := d.Screen(); strings.Contains(s, "\x1b") || !strings.HasPrefix(s, "got: init") {
t.Errorf("screen not stripped: %q", s)
}
d.Press("q")
d.Settle(time.Second)
if !d.Quit() {
t.Error("tea.Quit not detected")
}
}
func TestDriverKeys(t *testing.T) {
for _, k := range []string{"enter", "esc", "tab", "f5", "f12", "pgdown", "ctrl+r", "shift+up", "alt+left", "backspace", "delete", " ", "j", "п"} {
got := Key(t, k).String()
want := k
if k == "alt+left" {
want = "alt+left"
}
if got != want {
t.Errorf("Key(%q).String() = %q", k, got)
}
}
d := New(t, toy{}, 80, 24, WithoutInit())
d.Type("ab п")
if got := strings.Join(d.Model().(toy).keys, "|"); got != "a|b| |п" {
t.Errorf("typed keys %q", got)
}
}