返回 DeepSeek-Reasonix
remote_cmd_test.go
根目录 / internal / cli / remote_cmd_test.go
1 package cli
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "reasonix/internal/config"
10 )
11
12 func TestRemoteCommandUsageExit(t *testing.T) {
13 if got := remoteCommand(nil, "test"); got != 2 {
14 t.Errorf("no-arg remote exit = %d, want 2", got)
15 }
16 if got := remoteCommand([]string{"bogus"}, "test"); got != 2 {
17 t.Errorf("unknown subcommand exit = %d, want 2", got)
18 }
19 if got := remoteCommand([]string{"help"}, "test"); got != 0 {
20 t.Errorf("help exit = %d, want 0", got)
21 }
22 }
23
24 func TestRemovedRemoteWorkbenchCommandsFailWithMigrationHint(t *testing.T) {
25 for _, command := range []string{"attach-workspace", "runtime-workbench", "workbench-build-id"} {
26 t.Run(command, func(t *testing.T) {
27 stdout, stderr := captureCLIOutput(t, func() {
28 if got := remoteCommand([]string{command}, "v1.2.3"); got != 1 {
29 t.Fatalf("exit = %d, want 1", got)
30 }
31 })
32 if stdout != "" {
33 t.Fatalf("migration error wrote stdout: %q", stdout)
34 }
35 if !strings.Contains(stderr, "Remote Workbench") ||
36 !strings.Contains(stderr, "remote connect <host> --open") {
37 t.Fatalf("migration error = %q, want removal and replacement hints", stderr)
38 }
39 })
40 }
41 }
42
43 func TestRemoteAddListRemoveRoundTrip(t *testing.T) {
44 home := t.TempDir()
45 t.Setenv("REASONIX_HOME", home)
46 t.Setenv("HOME", home)
47
48 if got := remoteAddCLI([]string{"box", "dev@10.0.0.9:2222", "--workspace", "~/app"}); got != 0 {
49 t.Fatalf("add exit = %d", got)
50 }
51 cfg, err := config.Load()
52 if err != nil {
53 t.Fatal(err)
54 }
55 h, ok := cfg.RemoteHost("box")
56 if !ok {
57 t.Fatal("host not persisted")
58 }
59 if h.User != "dev" || h.Host != "10.0.0.9" || h.Port != 2222 || h.Workspace != "~/app" {
60 t.Fatalf("host fields wrong: %+v", h)
61 }
62 raw, _ := os.ReadFile(filepath.Join(home, "config.toml"))
63 if !strings.Contains(string(raw), "[[remote.hosts]]") || !strings.Contains(string(raw), `name = "box"`) {
64 t.Fatalf("config.toml missing remote host:\n%s", raw)
65 }
66
67 if got := remoteRemoveCLI([]string{"box"}); got != 0 {
68 t.Fatalf("remove exit = %d", got)
69 }
70 if got := remoteRemoveCLI([]string{"box"}); got != 1 {
71 t.Errorf("second remove exit = %d, want 1", got)
72 }
73 }
74
75 func TestRemoteRemoveCleansGeneratedCredentialsButKeepsUserManagedOnes(t *testing.T) {
76 home := t.TempDir()
77 t.Setenv("REASONIX_HOME", home)
78 t.Setenv("HOME", home)
79 t.Setenv("USERPROFILE", home)
80 passwordKey := config.RemotePasswordCredentialEnvName("secure-box")
81 passphraseKey := config.RemotePassphraseCredentialEnvName("secure-box")
82 const sharedKey = "TEAM_SHARED_SSH_PASSWORD"
83 for key, value := range map[string]string{
84 passwordKey: "generated-password", passphraseKey: "generated-passphrase", sharedKey: "shared-password",
85 } {
86 if _, err := config.SetCredential(key, value); err != nil {
87 t.Fatal(err)
88 }
89 key := key
90 t.Cleanup(func() { _ = config.RemoveCredential(key) })
91 }
92 if err := editUserConfig(func(c *config.Config) error {
93 if err := c.UpsertRemoteHost(config.RemoteHostEntry{
94 Name: "secure-box", Host: "192.0.2.20", PasswordEnv: passwordKey, PassphraseEnv: passphraseKey,
95 }); err != nil {
96 return err
97 }
98 return c.UpsertRemoteHost(config.RemoteHostEntry{
99 Name: "shared-box", Host: "192.0.2.21", PasswordEnv: sharedKey,
100 })
101 }); err != nil {
102 t.Fatal(err)
103 }
104
105 if got := remoteRemoveCLI([]string{"secure-box"}); got != 0 {
106 t.Fatalf("remove generated host exit = %d", got)
107 }
108 if got := config.ResolveCredentialForRootGlobalFirst(home, passwordKey); got.Set {
109 t.Fatal("generated password remained after CLI host removal")
110 }
111 if got := config.ResolveCredentialForRootGlobalFirst(home, passphraseKey); got.Set {
112 t.Fatal("generated passphrase remained after CLI host removal")
113 }
114 if got := remoteRemoveCLI([]string{"shared-box"}); got != 0 {
115 t.Fatalf("remove shared host exit = %d", got)
116 }
117 if got := config.ResolveCredentialForRootGlobalFirst(home, sharedKey); !got.Set || got.Value != "shared-password" {
118 t.Fatalf("user-managed credential was removed: %+v", got)
119 }
120 }
121
122 func TestRemoteAddReplacementCleansDroppedGeneratedCredentials(t *testing.T) {
123 home := t.TempDir()
124 t.Setenv("REASONIX_HOME", home)
125 t.Setenv("HOME", home)
126 t.Setenv("USERPROFILE", home)
127 passwordKey := config.RemotePasswordCredentialEnvName("box")
128 if _, err := config.SetCredential(passwordKey, "generated-password"); err != nil {
129 t.Fatal(err)
130 }
131 t.Cleanup(func() { _ = config.RemoveCredential(passwordKey) })
132 if err := editUserConfig(func(c *config.Config) error {
133 return c.UpsertRemoteHost(config.RemoteHostEntry{Name: "box", Host: "192.0.2.30", PasswordEnv: passwordKey})
134 }); err != nil {
135 t.Fatal(err)
136 }
137 if got := remoteAddCLI([]string{"box", "dev@192.0.2.31"}); got != 0 {
138 t.Fatalf("replace exit = %d", got)
139 }
140 if got := config.ResolveCredentialForRootGlobalFirst(home, passwordKey); got.Set {
141 t.Fatal("generated credential remained after CLI replacement dropped its reference")
142 }
143 }
144
145 func TestRemoteImportPreservesReasonixSettings(t *testing.T) {
146 home := t.TempDir()
147 t.Setenv("REASONIX_HOME", home)
148 t.Setenv("HOME", home)
149 t.Setenv("USERPROFILE", home)
150 sshDir := filepath.Join(home, ".ssh")
151 if err := os.MkdirAll(sshDir, 0o700); err != nil {
152 t.Fatal(err)
153 }
154 if err := os.WriteFile(filepath.Join(sshDir, "config"), []byte("Host box\n HostName 192.0.2.44\n"), 0o600); err != nil {
155 t.Fatal(err)
156 }
157 if err := editUserConfig(func(c *config.Config) error {
158 return c.UpsertRemoteHost(config.RemoteHostEntry{
159 Name: "box", Host: "old.example", Workspace: "/srv/app", ServeInstall: "never",
160 PasswordEnv: "REMOTE_BOX_PASSWORD",
161 Forwards: []config.RemoteForwardEntry{{Type: "local", Bind: "127.0.0.1:8080", Target: "127.0.0.1:80"}},
162 })
163 }); err != nil {
164 t.Fatal(err)
165 }
166 if got := remoteImportCLI([]string{"box"}); got != 0 {
167 t.Fatalf("import exit = %d", got)
168 }
169 cfg, err := config.Load()
170 if err != nil {
171 t.Fatal(err)
172 }
173 host, ok := cfg.RemoteHost("box")
174 if !ok || host.Host != "box" || !host.UseSSHConfig || host.Workspace != "/srv/app" || host.ServeInstall != "never" {
175 t.Fatalf("imported host = %+v, exists=%v", host, ok)
176 }
177 if host.PasswordEnv != "REMOTE_BOX_PASSWORD" || len(host.Forwards) != 1 {
178 t.Fatalf("import wiped hidden settings: %+v", host)
179 }
180 }
181
182 func TestRemoteForwardAddPersists(t *testing.T) {
183 home := t.TempDir()
184 t.Setenv("REASONIX_HOME", home)
185 t.Setenv("HOME", home)
186 if got := remoteAddCLI([]string{"box", "dev@10.0.0.9"}); got != 0 {
187 t.Fatalf("add exit = %d", got)
188 }
189 if got := remoteForwardAdd([]string{"box", "-L", "8080:127.0.0.1:80"}); got != 0 {
190 t.Fatalf("forward add exit = %d", got)
191 }
192 cfg, _ := config.Load()
193 h, _ := cfg.RemoteHost("box")
194 if len(h.Forwards) != 1 || h.Forwards[0].Type != "local" || h.Forwards[0].Bind != "127.0.0.1:8080" {
195 t.Fatalf("forward not persisted: %+v", h.Forwards)
196 }
197 }
198
199 func TestSplitHostPath(t *testing.T) {
200 cases := []struct {
201 in string
202 host, path string
203 ok bool
204 }{
205 {"box:/home/dev/file", "box", "/home/dev/file", true},
206 {"box:file", "box", "file", true},
207 {"nocolon", "", "", false},
208 {":path", "", "", false},
209 {"box:", "", "", false},
210 }
211 for _, c := range cases {
212 h, p, ok := splitHostPath(c.in)
213 if ok != c.ok || h != c.host || p != c.path {
214 t.Errorf("splitHostPath(%q) = (%q,%q,%v), want (%q,%q,%v)", c.in, h, p, ok, c.host, c.path, c.ok)
215 }
216 }
217 }
218
219 func TestParseRemoteConnectSyntaxFlagOrder(t *testing.T) {
220 tests := []struct {
221 name string
222 args []string
223 openAlias bool
224 wantName string
225 wantOpen bool
226 wantWS string
227 wantPort int
228 wantErr bool
229 }{
230 {name: "name then flags (documented / GUIDE order)", args: []string{"gpu-box", "--open", "--workspace", "/tmp/ws", "--local-port", "8080"}, wantName: "gpu-box", wantOpen: true, wantWS: "/tmp/ws", wantPort: 8080},
231 {name: "flags then name", args: []string{"--open", "--workspace", "/tmp/ws", "gpu-box"}, wantName: "gpu-box", wantOpen: true, wantWS: "/tmp/ws"},
232 {name: "single-dash open before name", args: []string{"-open", "gpu-box"}, wantName: "gpu-box", wantOpen: true},
233 {name: "name only", args: []string{"gpu-box"}, wantName: "gpu-box"},
234 {name: "open alias sets open without flag", args: []string{"gpu-box"}, openAlias: true, wantName: "gpu-box", wantOpen: true},
235 {name: "missing name", args: []string{"--open"}, wantErr: true},
236 {name: "extra positional after name-first flags", args: []string{"gpu-box", "extra"}, wantErr: true},
237 {name: "two names after flags", args: []string{"--open", "a", "b"}, wantErr: true},
238 }
239 for _, tt := range tests {
240 t.Run(tt.name, func(t *testing.T) {
241 got, err := parseRemoteConnectSyntax(tt.args, tt.openAlias)
242 if tt.wantErr {
243 if err == nil {
244 t.Fatalf("expected error, got %+v", got)
245 }
246 return
247 }
248 if err != nil {
249 t.Fatalf("unexpected error: %v", err)
250 }
251 if got.name != tt.wantName || got.open != tt.wantOpen || got.workspace != tt.wantWS || got.localPort != tt.wantPort {
252 t.Fatalf("got %+v, want name=%q open=%v workspace=%q localPort=%d", got, tt.wantName, tt.wantOpen, tt.wantWS, tt.wantPort)
253 }
254 })
255 }
256 }
257
258 func TestRemoteConnectSyntaxUsesSharedFlagContract(t *testing.T) {
259 tests := []struct {
260 name string
261 args []string
262 wantCode int
263 want string
264 noUsage bool
265 }{
266 {name: "help before name", args: []string{"connect", "--help"}, wantCode: 0, want: "Usage of remote connect:"},
267 {name: "help after name", args: []string{"connect", "gpu-box", "--help"}, wantCode: 0, want: "Usage of remote connect:"},
268 {name: "unknown flag before name", args: []string{"connect", "--unknown", "gpu-box"}, wantCode: 2, want: "flag provided but not defined: -unknown", noUsage: true},
269 {name: "unknown flag after name", args: []string{"connect", "gpu-box", "--unknown"}, wantCode: 2, want: "flag provided but not defined: -unknown", noUsage: true},
270 {name: "missing name", args: []string{"connect", "--open"}, wantCode: 2, want: remoteConnectUsage},
271 }
272
273 for _, tt := range tests {
274 t.Run(tt.name, func(t *testing.T) {
275 stdout, stderr := captureCLIOutput(t, func() {
276 if code := remoteConnectCLI(tt.args, "test-version"); code != tt.wantCode {
277 t.Fatalf("remoteConnectCLI(%q) = %d, want %d", tt.args, code, tt.wantCode)
278 }
279 })
280 output := stderr
281 if tt.wantCode == 0 {
282 output = stdout
283 if stderr != "" {
284 t.Fatalf("help wrote stderr: %q", stderr)
285 }
286 } else if stdout != "" {
287 t.Fatalf("error wrote stdout: %q", stdout)
288 }
289 if !strings.Contains(output, tt.want) {
290 t.Fatalf("output = %q, want %q", output, tt.want)
291 }
292 if tt.noUsage && strings.Contains(output, "Usage of") {
293 t.Fatalf("parse error should be concise, got usage:\n%s", output)
294 }
295 })
296 }
297 }
298
298 lines GO