package main import ( "testing" "git.tukangketik.id/swanadiva/hostkeeper/internal/models" ) func TestConnectCommandExists(t *testing.T) { if connectCmd == nil { t.Fatal("connectCmd should not be nil") } if connectCmd.Use != "connect " { t.Errorf("expected Use 'connect ', got '%s'", connectCmd.Use) } if connectCmd.Short == "" { t.Error("Short description should not be empty") } } func TestConnectCommandFlags(t *testing.T) { expectedFlags := []string{"timeout", "native"} for _, flagName := range expectedFlags { if connectCmd.Flags().Lookup(flagName) == nil { t.Errorf("flag '%s' should be defined", flagName) } } } func TestConnectArgs(t *testing.T) { if connectCmd.Args == nil { t.Error("Args validator should not be nil") } } func TestBuildSSHArgs(t *testing.T) { tests := []struct { name string host *models.Host want []string }{ { name: "default port 22", host: &models.Host{ Name: "test", Hostname: "192.168.1.1", Port: 22, Username: "admin", }, want: []string{"admin@192.168.1.1"}, }, { name: "non-default port 2222", host: &models.Host{ Name: "test", Hostname: "192.168.1.1", Port: 2222, Username: "admin", }, want: []string{"-p", "2222", "admin@192.168.1.1"}, }, { name: "key auth with KeyID", host: &models.Host{ Name: "test", Hostname: "10.0.0.1", Port: 22, Username: "root", Auth: models.AuthConfig{ Type: "key", KeyID: "~/.ssh/id_rsa", }, }, want: []string{"root@10.0.0.1"}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := buildSSHArgs(tt.host) if len(got) != len(tt.want) { t.Errorf("buildSSHArgs() = %v, want %v", got, tt.want) return } for i := range got { if got[i] != tt.want[i] { t.Errorf("buildSSHArgs() = %v, want %v", got, tt.want) return } } }) } }