返回 DeepSeek-Reasonix
host_test.go
根目录 / internal / remote / host_test.go
1 package remote
2
3 import (
4 "testing"
5
6 "reasonix/internal/config"
7 )
8
9 func TestParseTarget(t *testing.T) {
10 cases := []struct {
11 in string
12 user, host string
13 port int
14 wantErr bool
15 }{
16 {"host", "", "host", 0, false},
17 {"user@host", "user", "host", 0, false},
18 {"user@host:2222", "user", "host", 2222, false},
19 {"host:22", "", "host", 22, false},
20 {"[::1]:22", "", "::1", 22, false},
21 {"dev@[2001:db8::1]:2200", "dev", "2001:db8::1", 2200, false},
22 {"2001:db8::1", "", "2001:db8::1", 0, false},
23 {"", "", "", 0, true},
24 {"user@", "", "", 0, true},
25 {"host:99999", "", "", 0, true},
26 {"host:abc", "", "", 0, true},
27 }
28 for _, c := range cases {
29 u, h, p, err := ParseTarget(c.in)
30 if c.wantErr {
31 if err == nil {
32 t.Errorf("ParseTarget(%q): expected error", c.in)
33 }
34 continue
35 }
36 if err != nil {
37 t.Errorf("ParseTarget(%q): %v", c.in, err)
38 continue
39 }
40 if u != c.user || h != c.host || p != c.port {
41 t.Errorf("ParseTarget(%q) = (%q,%q,%d), want (%q,%q,%d)", c.in, u, h, p, c.user, c.host, c.port)
42 }
43 }
44 }
45
46 func TestResolveHostFromConfig(t *testing.T) {
47 cfg := config.Default()
48 if err := cfg.UpsertRemoteHost(config.RemoteHostEntry{
49 Name: "box",
50 Host: "10.0.0.5",
51 Port: 2200,
52 User: "dev",
53 ProxyJump: "bastion, second",
54 Workspace: "~/app",
55 }); err != nil {
56 t.Fatal(err)
57 }
58 h, err := ResolveHost(cfg, "box", nil)
59 if err != nil {
60 t.Fatal(err)
61 }
62 if h.HostName != "10.0.0.5" || h.Port != 2200 || h.User != "dev" {
63 t.Fatalf("resolved wrong: %+v", h)
64 }
65 if len(h.ProxyJump) != 2 || h.ProxyJump[0] != "bastion" || h.ProxyJump[1] != "second" {
66 t.Fatalf("proxy jump chain wrong: %v", h.ProxyJump)
67 }
68 if h.Addr() != "10.0.0.5:2200" {
69 t.Fatalf("Addr = %q", h.Addr())
70 }
71 }
72
73 func TestResolveHostAdHocDefaultsPort(t *testing.T) {
74 h, err := ResolveHost(config.Default(), "user@example.com", nil)
75 if err != nil {
76 t.Fatal(err)
77 }
78 if h.Port != 22 {
79 t.Fatalf("default port = %d, want 22", h.Port)
80 }
81 if h.User != "user" {
82 t.Fatalf("user = %q", h.User)
83 }
84 }
85
85 lines GO