Make the F11 action menu close through a shared close action so Escape and the explicit Close actions menu item follow the same path. Treat Escape-prefixed Up and Down events as close inputs while the action menu is active, which handles termbox InputAlt behavior after pressing Escape. Preserve and restore the user/channel tree selection across action menu open and close, and preserve selection across live tree rebuilds. Tested with: GOCACHE=/tmp/barnard-go-cache go test ./...
91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.stormux.org/storm/barnard/gumble/gumble"
|
|
"git.stormux.org/storm/barnard/uiterm"
|
|
)
|
|
|
|
func TestParseToggleState(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
text string
|
|
current bool
|
|
want bool
|
|
wantOK bool
|
|
}{
|
|
{name: "toggle true", text: "toggle", current: true, want: false, wantOK: true},
|
|
{name: "empty toggles", text: "", current: false, want: true, wantOK: true},
|
|
{name: "on", text: "on", current: false, want: true, wantOK: true},
|
|
{name: "off", text: "off", current: true, want: false, wantOK: true},
|
|
{name: "bad", text: "maybe", current: true, wantOK: false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, ok := parseToggleState(tt.text, tt.current)
|
|
if ok != tt.wantOK {
|
|
t.Fatalf("expected ok %v, got %v", tt.wantOK, ok)
|
|
}
|
|
if ok && got != tt.want {
|
|
t.Fatalf("expected %v, got %v", tt.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParsePermission(t *testing.T) {
|
|
tests := []struct {
|
|
text string
|
|
want gumble.Permission
|
|
}{
|
|
{text: "write", want: gumble.PermissionWrite},
|
|
{text: "mute_deafen", want: gumble.PermissionMuteDeafen},
|
|
{text: "kick", want: gumble.PermissionKick},
|
|
{text: "register_self", want: gumble.PermissionRegisterSelf},
|
|
}
|
|
for _, tt := range tests {
|
|
got, ok := parsePermission(tt.text)
|
|
if !ok {
|
|
t.Fatalf("expected %s to parse", tt.text)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("expected %v, got %v", tt.want, got)
|
|
}
|
|
}
|
|
if _, ok := parsePermission("bogus"); ok {
|
|
t.Fatal("expected bogus permission to fail")
|
|
}
|
|
}
|
|
|
|
func TestPermissionList(t *testing.T) {
|
|
got := permissionList(gumble.PermissionWrite | gumble.PermissionBan)
|
|
if got != "write,ban" {
|
|
t.Fatalf("expected write,ban, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAdminEscapeInputs(t *testing.T) {
|
|
if !isAdminEscapeKey(uiterm.KeyEsc) {
|
|
t.Fatal("expected escape key to close admin menu")
|
|
}
|
|
if !isAdminEscapeKey(uiterm.KeyAltEsc) {
|
|
t.Fatal("expected alt escape key to close admin menu")
|
|
}
|
|
if !isAdminEscapeKey(uiterm.KeyAltArrowUp) {
|
|
t.Fatal("expected escape-prefixed up arrow to close admin menu")
|
|
}
|
|
if !isAdminEscapeKey(uiterm.KeyAltArrowDown) {
|
|
t.Fatal("expected escape-prefixed down arrow to close admin menu")
|
|
}
|
|
if isAdminEscapeKey(uiterm.KeyEnter) {
|
|
t.Fatal("expected enter key not to close admin menu")
|
|
}
|
|
if !isAdminEscapeCharacter(rune(uiterm.KeyEsc)) {
|
|
t.Fatal("expected escape character to close admin menu")
|
|
}
|
|
if isAdminEscapeCharacter('x') {
|
|
t.Fatal("expected non-escape character not to close admin menu")
|
|
}
|
|
}
|