Files
ssh-netbox-wrapper/internal/tui/model_test.go
T
Sebastian UnterschützandClaude Opus 5.5 a31aab4110 feat: netssh multi — several hosts side by side in tmux or zellij
netssh multi opens one pane per host in a tmux window or zellij tab.
Hosts come from names, tag:TAG (NetBox and static hosts), cluster:NAME
(NetBox virtualization clusters; devices by default, --kind vm|all),
and groups from the config (@name, nested, with cycle detection).
Every pane runs netssh HOST, so lookup, rules, and jump hosts apply.

- tmux: a new window inside tmux, else a new session that is attached;
  tiled layout, pane borders with the host and a [SYNC] marker.
- zellij: a generated grid layout; a new tab inside zellij, else a new
  session.
- --sync broadcasts typing to all panes; netssh sync toggles it in the
  current tmux window or zellij tab.
- TUI: Space marks hosts, Ctrl+A marks all, Enter opens them together.
- Configurator: multiplexer, panes per window, groups.

NetBox SearchAll gains a cluster filter (name to cluster IDs per
instance), and cache entries keep the host's cluster for completion
and an offline fallback. Panes skip the 24-hour background refresh
(checked once by multi) and get HOME/XDG paths but no NETSSH_* values,
which would expose tokens in the process list.

testenv/tests/15-multi.sh checks real tmux and zellij sessions,
including a broadcast command arriving on all three cluster nodes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 12:45:05 +01:00

160 lines
5.1 KiB
Go

package tui
import (
"reflect"
"strings"
"testing"
tea "charm.land/bubbletea/v2"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/notes"
)
func TestTypingEDoesNotOpenConnectionEditor(t *testing.T) {
m := New(nil, nil, "")
m.updateListItems([]netbox.HostEntry{{Name: "server"}})
m.Update(tea.KeyPressMsg{Code: 'e', Text: "e"})
if m.editMode || m.input.Value() != "e" {
t.Fatal("e intercepted")
}
}
func TestStaleResultsAndDebounceAreIgnored(t *testing.T) {
m := New(nil, nil, "")
m.input.SetValue("web")
m.seq = 3
m.Update(searchResultMsg{query: "web", seq: 1, entries: []netbox.HostEntry{{Name: "old"}}})
if len(m.list.Items()) != 0 {
t.Fatal("stale result accepted")
}
_, cmd := m.Update(debounceMsg{query: "w", seq: 1})
if cmd != nil {
t.Fatal("stale debounce accepted")
}
m.Update(searchResultMsg{query: "web", seq: 3, entries: []netbox.HostEntry{{Name: "web01"}}})
if len(m.list.Items()) != 1 {
t.Fatal("current result ignored")
}
}
func TestEditPreservesDefaultAndExplicitPort22(t *testing.T) {
m := New(nil, nil, "", 2222)
item := hostItem{name: "web01", ip: "10.0.0.1"}
m.openEdit(item)
if m.editInput.Value() != "web01:2222" {
t.Fatal(m.editInput.Value())
}
m.editInput.SetValue("ops@web01:22")
selected := m.buildSelected(item)
if selected.Port != "22" || selected.User != "ops" {
t.Fatalf("bad override: %+v", selected)
}
}
func TestFailedNoteLoadCannotBeSaved(t *testing.T) {
m := New(nil, nil, "")
m.SetNotes(&notes.Service{Dir: t.TempDir()})
m.noteMode = true
_, cmd := m.Update(tea.KeyPressMsg{Code: 's', Mod: tea.ModCtrl})
if cmd != nil {
t.Fatal("save allowed before successful load")
}
}
func TestConnectionEditorKeepsOriginalHost(t *testing.T) {
m := New(nil, nil, "admin")
first := hostItem{name: "first", ip: "10.0.0.1"}
m.openEdit(first)
m.updateListItems([]netbox.HostEntry{{Name: "second", PrimaryIP4: "10.0.0.2"}})
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
if m.selected == nil || m.selected.Name != "first" {
t.Fatalf("edited host changed: %+v", m.selected)
}
}
func TestCtrlOSelectsSFTP(t *testing.T) {
m := New(nil, nil, "")
m.updateListItems([]netbox.HostEntry{{Name: "web01", PrimaryIP4: "10.0.0.1"}})
m.Update(tea.KeyPressMsg{Code: 'o', Mod: tea.ModCtrl})
if m.selected == nil || m.selected.Action != ActionSFTP || m.selected.Name != "web01" {
t.Fatalf("got %+v", m.selected)
}
}
func TestCtrlTStartsTunnelWithForwards(t *testing.T) {
m := New(nil, nil, "")
m.SetForwards(func(name, kind string, tags []string) []string {
if name != "web01" || len(tags) != 1 {
t.Errorf("forwards asked for %s %v", name, tags)
}
return []string{"web"}
})
m.updateListItems([]netbox.HostEntry{{Name: "web01", PrimaryIP4: "10.0.0.1", Tags: []string{"dmz"}}})
m.Update(tea.KeyPressMsg{Code: 't', Mod: tea.ModCtrl})
if !m.forwardMode || m.selected != nil {
t.Fatal("forward input not opened")
}
if !strings.Contains(m.viewContent(), "forwards: web") {
t.Fatalf("configured forwards not shown:\n%s", m.viewContent())
}
m.forwardInput.SetValue("nope")
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
if m.selected != nil || m.err == nil {
t.Fatal("unknown forward accepted")
}
m.forwardInput.SetValue("WEB, 9000:localhost:9000")
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
want := []string{"WEB", "9000:localhost:9000"}
if m.selected == nil || m.selected.Action != ActionTunnel || !reflect.DeepEqual(m.selected.Forwards, want) {
t.Fatalf("got %+v", m.selected)
}
}
func TestForwardInputEscCancels(t *testing.T) {
m := New(nil, nil, "")
m.updateListItems([]netbox.HostEntry{{Name: "web01"}})
m.Update(tea.KeyPressMsg{Code: 't', Mod: tea.ModCtrl})
m.Update(tea.KeyPressMsg{Code: tea.KeyEscape})
if m.forwardMode || m.selected != nil {
t.Fatal("esc did not cancel")
}
}
func TestSpaceMarksHostsAndEnterOpensThem(t *testing.T) {
m := New(nil, nil, "")
m.updateListItems([]netbox.HostEntry{{Name: "a", Kind: "device"}, {Name: "b", Kind: "device"}, {Name: "c", Kind: "vm"}})
m.Update(tea.KeyPressMsg{Code: tea.KeySpace, Text: " "})
if len(m.markOrder) != 1 || m.markOrder[0] != "a" || m.list.Index() != 1 {
t.Fatalf("space: marks %v, cursor %d", m.markOrder, m.list.Index())
}
if m.input.Value() != "" {
t.Fatal("space went into the search field")
}
m.list.Select(2)
m.Update(tea.KeyPressMsg{Code: tea.KeySpace, Text: " "})
if !strings.Contains(m.viewContent(), "2 marked") {
t.Fatalf("no marked count:\n%s", m.viewContent())
}
m.Update(tea.KeyPressMsg{Code: tea.KeyEnter})
if m.selected == nil || m.selected.Action != ActionMulti || !reflect.DeepEqual(m.selected.Targets, []string{"a", "c"}) {
t.Fatalf("got %+v", m.selected)
}
}
func TestCtrlATogglesAllVisible(t *testing.T) {
m := New(nil, nil, "")
m.updateListItems([]netbox.HostEntry{{Name: "a"}, {Name: "b"}})
m.toggleMark(hostItem{name: "a"})
m.Update(tea.KeyPressMsg{Code: 'a', Mod: tea.ModCtrl})
if !reflect.DeepEqual(m.markOrder, []string{"a", "b"}) {
t.Fatalf("mark all: %v", m.markOrder)
}
m.Update(tea.KeyPressMsg{Code: 'a', Mod: tea.ModCtrl})
if len(m.markOrder) != 0 {
t.Fatalf("unmark all: %v", m.markOrder)
}
}