Files
barnard/uiterm/ui_regression_test.go

64 lines
1.6 KiB
Go

package uiterm
import "testing"
// Regression: Close sent to a bounded channel and could block or enqueue
// duplicate shutdowns when called more than once.
// Regression: network callbacks modified terminal state directly. Post gives
// them a bounded handoff to the UI-owning Run goroutine instead of blocking.
func TestPostIsBoundedAndRejectsClosedUI(t *testing.T) {
ui := New(nil)
for i := 0; i < cap(ui.events); i++ {
if !ui.Post(func() {}) {
t.Fatal("queue filled too early")
}
}
if ui.Post(func() {}) {
t.Fatal("Post accepted work past queue capacity")
}
ui.Close()
if ui.Post(func() {}) {
t.Fatal("Post accepted work after close")
}
}
func TestCloseIsNonblockingAndIdempotent(t *testing.T) {
ui := New(nil)
ui.Close()
ui.Close()
select {
case <-ui.close:
default:
t.Fatal("Close did not signal shutdown")
}
}
func TestSafeRuneRemovesTerminalControlCharacters(t *testing.T) {
for _, r := range []rune{'\x1b', '\x7f', '\u202e'} {
if got := safeRune(r); got != ' ' {
t.Errorf("safeRune(%U) = %U, want space", r, got)
}
}
if got := safeRune('A'); got != 'A' {
t.Fatalf("safeRune altered printable text: %U", got)
}
}
func TestDispatchCommandReportsMatchesAndPreservesArguments(t *testing.T) {
ui := New(nil)
got := ""
ui.AddCommandListener(func(_ *Ui, command string) {
got = command
}, "record")
if !ui.DispatchCommand("record start") {
t.Fatal("registered command was not handled")
}
if got != "start" {
t.Fatalf("command argument = %q, want start", got)
}
if ui.DispatchCommand("unknown") {
t.Fatal("unknown command was reported as handled")
}
}