1package domain
2
3import (
4 "slices"
5 "testing"
6)
7
8func TestRemoteHostTargetValidation(t *testing.T) {
9 tests := []struct {
10 name string
11 target string
12 wantErr bool
13 }{
14 {name: "alias", target: "arch"},
15 {name: "user at host", target: "build@mule"},
16 {name: "bracketed ipv6 with port", target: "user@[2001:db8::1]:2222"},
17 {name: "empty", target: "", wantErr: true},
18 {name: "leading whitespace", target: " arch", wantErr: true},
19 {name: "trailing whitespace", target: "arch ", wantErr: true},
20 {name: "internal whitespace", target: "build @mule", wantErr: true},
21 {name: "newline", target: "arch\n", wantErr: true},
22 {name: "tab", target: "arch\t", wantErr: true},
23 {name: "control byte", target: "arch\x00", wantErr: true},
24 }
25
26 for _, tt := range tests {
27 t.Run(tt.name, func(t *testing.T) {
28 err := ValidateRemoteHostTarget(tt.target)
29 if tt.wantErr && err == nil {
30 t.Fatalf("ValidateRemoteHostTarget(%q) = nil, want error", tt.target)
31 }
32 if !tt.wantErr && err != nil {
33 t.Fatalf("ValidateRemoteHostTarget(%q) = %v, want nil", tt.target, err)
34 }
35 })
36 }
37}
38
39func TestRemoteHostUniqueTargets(t *testing.T) {
40 tests := []struct {
41 name string
42 input []string
43 want []string
44 }{
45 {
46 name: "preserves first occurrence order",
47 input: []string{"arch", "build@mule", "arch", "mule"},
48 want: []string{"arch", "build@mule", "mule"},
49 },
50 {
51 name: "empty input",
52 input: nil,
53 want: []string{},
54 },
55 {
56 name: "no duplicates",
57 input: []string{"alpha", "beta"},
58 want: []string{"alpha", "beta"},
59 },
60 }
61
62 for _, tt := range tests {
63 t.Run(tt.name, func(t *testing.T) {
64 got := UniqueRemoteHostTargets(tt.input)
65 if got == nil {
66 t.Fatalf("UniqueRemoteHostTargets(%v) = nil, want non-nil slice", tt.input)
67 }
68 if !slices.Equal(got, tt.want) {
69 t.Fatalf("UniqueRemoteHostTargets(%v) = %v, want %v", tt.input, got, tt.want)
70 }
71 })
72 }
73}