| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/sylvinus/agent-vm/internal/cage" |
| 13 | ) |
| 14 | |
| 15 | func TestClaudeTerminalRunsSavedCommandOnceAndReturnsToShell(t *testing.T) { |
| 16 | if runtime.GOOS == "windows" { |
| 17 | t.Skip("guest Bash integration runs on Unix") |
| 18 | } |
| 19 | dir := filepath.Join(t.TempDir(), "project with ' quotes") |
| 20 | if err := os.Mkdir(dir, 0700); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | command := `printf '%s\n' 'argument with spaces; $(not-executed)' >> command-result` |
| 24 | cmd := exec.Command("bash", "-c", terminalScript(dir, "claude", command)) |
| 25 | cmd.Env = append(os.Environ(), "HOME="+t.TempDir()) |
| 26 | cmd.Stdin = strings.NewReader("printf shell-returned > shell-result\nexit\n") |
| 27 | if out, err := cmd.CombinedOutput(); err != nil { |
| 28 | t.Fatalf("%v: %s", err, out) |
| 29 | } |
| 30 | b, err := os.ReadFile(filepath.Join(dir, "command-result")) |
| 31 | if err != nil || string(b) != "argument with spaces; $(not-executed)\n" { |
| 32 | t.Fatalf("startup command quoting/repetition: %q, %v", b, err) |
| 33 | } |
| 34 | b, err = os.ReadFile(filepath.Join(dir, "shell-result")) |
| 35 | if err != nil || string(b) != "shell-returned" { |
| 36 | t.Fatalf("terminal did not return to shell: %q, %v", b, err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestClaudeTerminalPreferenceValidationAndPersistence(t *testing.T) { |
| 41 | s := testServer(t) |
| 42 | cfg, _ := cage.Load(s.configPath) |
| 43 | if settingsView(cfg, s.preferences).ClaudeTerminalCommand != "claude" { |
| 44 | t.Fatal("missing default") |
| 45 | } |
| 46 | input := settingsView(cfg, s.preferences) |
| 47 | input.ClaudeTerminalCommand = "claude --continue --model sonnet" |
| 48 | _, p, err := proposedSettings(input, cfg, s.profile) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err = s.apply(savedProfile{cfg, p}, true); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | data, err := os.ReadFile(filepath.Join(s.profile, "workbench.json")) |
| 56 | if err != nil || !strings.Contains(string(data), input.ClaudeTerminalCommand) { |
| 57 | t.Fatalf("command not saved: %v", err) |
| 58 | } |
| 59 | after, _ := cage.Load(s.configPath) |
| 60 | if after != cfg { |
| 61 | t.Fatal("terminal preference modified environment configuration") |
| 62 | } |
| 63 | for _, bad := range []string{"claude\nexit", "claude\r", "claude\x00", strings.Repeat("x", 4097)} { |
| 64 | input.ClaudeTerminalCommand = bad |
| 65 | if _, _, err := proposedSettings(input, cfg, s.profile); err == nil { |
| 66 | t.Fatal("invalid command accepted") |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestClaudeLoginOutputNeverEntersReconnectHistory(t *testing.T) { |
| 72 | var manager terminalManager |
| 73 | t.Cleanup(manager.close) |
| 74 | selection := claudeSelection{ID: "aaaaaaaaaaaaaaaaaaaaaaaa", GuestID: "aaaaaaaaaaaaaaaaaaaaaaaa"} |
| 75 | session, err := manager.startSession(terminalChildCommand("echo"), "/project", "claude-login", selection, nil) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | view := session.subscribe() |
| 80 | defer view.Close() |
| 81 | if err = session.enqueueInput(context.Background(), terminalInputLine("private-login-code")); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | readTerminalUntil(t, view, "child:private-login-code") |
| 85 | observation := session.subscribe() |
| 86 | defer observation.Close() |
| 87 | if strings.Contains(string(observation.History), "private-login-code") || len(session.history) != 0 { |
| 88 | t.Fatal("login output was retained for reconnect") |
| 89 | } |
| 90 | if session.summary()["claude_account_id"] != selection.ID || session.summary()["claude_account_source"] != "managed" { |
| 91 | t.Fatal("terminal lost its original account binding") |
| 92 | } |
| 93 | if again, err := manager.existingClaudeLogin(session.claudeAccountID, true); err != nil || again != session { |
| 94 | t.Fatal("repeated login did not reattach to the same PTY") |
| 95 | } |
| 96 | if _, err := manager.existingClaudeLogin(session.claudeAccountID, false); err == nil { |
| 97 | t.Fatal("started a Claude terminal while its profile is changing identity") |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestManagedTerminalPreservesTheSelectedProfileInTheGuestShell(t *testing.T) { |
| 102 | if runtime.GOOS == "windows" { |
| 103 | t.Skip("guest Bash integration runs on Unix") |
| 104 | } |
| 105 | home := t.TempDir() |
| 106 | const managed = "111111111111111111111111" |
| 107 | for _, tc := range []struct{ guestID, want string }{ |
| 108 | {"default", home + "/custom-config"}, |
| 109 | {managed, home + "/.local/share/cage/claude-accounts/" + managed + "/config"}, |
| 110 | } { |
| 111 | work := t.TempDir() |
| 112 | cmd := exec.Command("bash", "-c", managedClaudeTerminalScript(work, `printf '%s' "$CLAUDE_CONFIG_DIR/$CAGE_CLAUDE_ACCOUNT_ID/$CAGE_CLAUDE_SUBJECT" > observed`, tc.guestID, "expected", false)) |
| 113 | cmd.Env = []string{"HOME=" + home, "PATH=" + os.Getenv("PATH"), "CLAUDE_CONFIG_DIR=" + home + "/custom-config"} |
| 114 | cmd.Stdin = strings.NewReader("exit\n") |
| 115 | if b, err := cmd.CombinedOutput(); err != nil { |
| 116 | t.Fatalf("guest launch: %v: %s", err, b) |
| 117 | } |
| 118 | b, err := os.ReadFile(filepath.Join(work, "observed")) |
| 119 | if err != nil || string(b) != tc.want+"/"+tc.guestID+"/expected" { |
| 120 | t.Fatalf("profile override or subject lost: %s, %v", b, err) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestClaudeTerminalSourceSurvivesAdoptingAnIndependentProfile(t *testing.T) { |
| 126 | var manager terminalManager |
| 127 | t.Cleanup(manager.close) |
| 128 | account := claudeSelection{ID: "aaaaaaaaaaaaaaaaaaaaaaaa", GuestID: "default"} |
| 129 | original, err := manager.startSession(terminalChildCommand("echo"), "/project", "claude", account, nil) |
| 130 | if err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | account.GuestID = "bbbbbbbbbbbbbbbbbbbbbbbb" |
| 134 | adopted, err := manager.startSession(terminalChildCommand("echo"), "/project", "claude", account, nil) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | if original.summary()["claude_account_source"] != "default" || adopted.summary()["claude_account_source"] != "managed" || |
| 139 | original.summary()["claude_account_id"] != adopted.summary()["claude_account_id"] { |
| 140 | t.Fatal("adoption changed an existing terminal's account source") |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestDefaultClaudeProbeUsesRegularLoginEnvironment(t *testing.T) { |
| 145 | if runtime.GOOS == "windows" { |
| 146 | t.Skip("guest Bash integration runs on Unix") |
| 147 | } |
| 148 | home := t.TempDir() |
| 149 | profile := "echo startup-banner\nexport CAGE_FIXTURE_AUTH=shell-route\n" |
| 150 | if err := os.WriteFile(filepath.Join(home, ".bash_profile"), []byte(profile), 0600); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if err := os.WriteFile(filepath.Join(home, "show-environment"), []byte("#!/bin/sh\nprintf '%s' \"$CAGE_FIXTURE_AUTH|$PWD|$1|$2\"\n"), 0700); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | script := strings.Replace(claudeSnapshotCommand("default"), "/usr/local/bin/cage-claude-account", `"$HOME/show-environment"`, 1) |
| 157 | cmd := exec.Command("bash", "-c", script) |
| 158 | cmd.Dir = t.TempDir() |
| 159 | cmd.Env = []string{"HOME=" + home, "PATH=" + os.Getenv("PATH")} |
| 160 | b, err := cmd.Output() |
| 161 | realHome, _ := filepath.EvalSymlinks(home) |
| 162 | if err != nil || (string(b) != "shell-route|"+home+"|snapshot|default" && string(b) != "shell-route|"+realHome+"|snapshot|default") { |
| 163 | t.Fatalf("login env or guest JSON stream changed: %q, %v", b, err) |
| 164 | } |
| 165 | } |