Files
Sebastian UnterschützandClaude Opus 5.5 b7fa71fe45 feat: sftp, scp, and named port forwards
Add netssh sftp and netssh scp. Both resolve the host like a connection
does (cache, shortcuts and rewrites, instance/host, default user and
port, SSH options and rules, --jump/--no-jump, --dry-run) and get shell
completion. The per-client flag tables handle the differences from ssh:
the port is -P, -l is a bandwidth limit (the default user goes in as
-o User=), and IPv6 addresses are bracketed in host:path. All remote
scp operands must name the same host.

Add named port forwards under ssh.forwards (global, per instance, per
rule; a more specific layer replaces a forward of the same name),
netssh tunnel HOST NAME|SPEC... (ssh -N with ExitOnForwardFailure) and
netssh --forward NAME. Specs such as 8080:localhost:80 work without
configuration. Forwards are editable in netssh configure and completed
in the shell; the TUI opens SFTP with ctrl+o and a tunnel with ctrl+t.

Host resolution moves out of runSSHWrapper into resolveTarget, shared
by ssh, sftp, scp, tunnel, and the TUI.

Options added by netssh are now inserted before the first operand or
"--": after "--", ssh read them as the destination, and scp stops
reading options at the first operand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:05:07 +01:00

125 lines
3.8 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")
}
}