|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os/exec" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// mockAgentRunner is a test double for AgentRunnerInterface. |
| 12 | +type mockAgentRunner struct { |
| 13 | + output string |
| 14 | + err error |
| 15 | + called bool |
| 16 | + prompt string |
| 17 | + timeout time.Duration |
| 18 | +} |
| 19 | + |
| 20 | +func (m *mockAgentRunner) Run(_ context.Context, prompt string, timeout time.Duration, _ ...RunOption) (string, error) { |
| 21 | + m.called = true |
| 22 | + m.prompt = prompt |
| 23 | + m.timeout = timeout |
| 24 | + if m.err != nil { |
| 25 | + return "", m.err |
| 26 | + } |
| 27 | + return m.output, nil |
| 28 | +} |
| 29 | + |
| 30 | +// blockingAgentRunner blocks until released or context is cancelled. |
| 31 | +type blockingAgentRunner struct { |
| 32 | + output string |
| 33 | + err error |
| 34 | + release chan struct{} |
| 35 | + called chan struct{} |
| 36 | +} |
| 37 | + |
| 38 | +func newBlockingRunner(output string, err error) *blockingAgentRunner { |
| 39 | + return &blockingAgentRunner{ |
| 40 | + output: output, |
| 41 | + err: err, |
| 42 | + release: make(chan struct{}), |
| 43 | + called: make(chan struct{}), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (b *blockingAgentRunner) Run(ctx context.Context, _ string, _ time.Duration, _ ...RunOption) (string, error) { |
| 48 | + close(b.called) |
| 49 | + select { |
| 50 | + case <-b.release: |
| 51 | + if b.err != nil { |
| 52 | + return "", b.err |
| 53 | + } |
| 54 | + return b.output, nil |
| 55 | + case <-ctx.Done(): |
| 56 | + return "", ctx.Err() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestDetectAgents_Priority(t *testing.T) { |
| 61 | + agents := DetectAgents() |
| 62 | + if len(agents) == 0 { |
| 63 | + t.Skip("no AI CLIs found on PATH") |
| 64 | + } |
| 65 | + // Verify priority: claude < gemini < codex by index. |
| 66 | + order := map[AgentKind]int{AgentClaude: 0, AgentGemini: 1, AgentCodex: 2} |
| 67 | + prev := -1 |
| 68 | + for _, a := range agents { |
| 69 | + idx, ok := order[a.Kind] |
| 70 | + if !ok { |
| 71 | + t.Errorf("unexpected agent kind: %s", a.Kind) |
| 72 | + continue |
| 73 | + } |
| 74 | + if idx <= prev { |
| 75 | + t.Errorf("agent %s (idx %d) came after idx %d — wrong priority", a.Kind, idx, prev) |
| 76 | + } |
| 77 | + prev = idx |
| 78 | + if a.Binary == "" { |
| 79 | + t.Errorf("agent %s has empty binary path", a.Kind) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestAgentRunner_BuildsClaudeArgs(t *testing.T) { |
| 85 | + r := NewAgentRunner(AgentInfo{Kind: AgentClaude, Binary: "/usr/local/bin/claude"}) |
| 86 | + args := r.buildArgs(runOptions{}) |
| 87 | + want := []string{"--print", "--output-format", "text"} |
| 88 | + if len(args) != len(want) { |
| 89 | + t.Fatalf("args = %v, want %v", args, want) |
| 90 | + } |
| 91 | + for i, a := range args { |
| 92 | + if a != want[i] { |
| 93 | + t.Errorf("args[%d] = %q, want %q", i, a, want[i]) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestAgentRunner_BuildsGeminiArgs(t *testing.T) { |
| 99 | + r := NewAgentRunner(AgentInfo{Kind: AgentGemini, Binary: "/usr/local/bin/gemini"}) |
| 100 | + args := r.buildArgs(runOptions{}) |
| 101 | + want := []string{"-p"} |
| 102 | + if len(args) != len(want) { |
| 103 | + t.Fatalf("args = %v, want %v", args, want) |
| 104 | + } |
| 105 | + if args[0] != "-p" { |
| 106 | + t.Errorf("args[0] = %q, want %q", args[0], "-p") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestAgentRunner_StripsCLAUDECODE(t *testing.T) { |
| 111 | + t.Setenv("CLAUDECODE", "1") |
| 112 | + r := NewAgentRunner(AgentInfo{Kind: AgentClaude, Binary: "/usr/local/bin/claude"}) |
| 113 | + env := r.buildEnv() |
| 114 | + for _, e := range env { |
| 115 | + if e == "CLAUDECODE=1" { |
| 116 | + t.Error("CLAUDECODE should be stripped from child env") |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestAgentRunner_GeminiKeepsCLAUDECODE(t *testing.T) { |
| 122 | + t.Setenv("CLAUDECODE", "1") |
| 123 | + r := NewAgentRunner(AgentInfo{Kind: AgentGemini, Binary: "/usr/local/bin/gemini"}) |
| 124 | + env := r.buildEnv() |
| 125 | + found := false |
| 126 | + for _, e := range env { |
| 127 | + if e == "CLAUDECODE=1" { |
| 128 | + found = true |
| 129 | + break |
| 130 | + } |
| 131 | + } |
| 132 | + if !found { |
| 133 | + t.Error("CLAUDECODE should NOT be stripped for gemini") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestAgentRunner_Timeout(t *testing.T) { |
| 138 | + r := NewAgentRunner(AgentInfo{Kind: AgentClaude, Binary: "sleep"}) |
| 139 | + _, err := r.Run(context.Background(), "", 100*time.Millisecond) |
| 140 | + if err == nil { |
| 141 | + t.Fatal("expected timeout error") |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestFilterEnv(t *testing.T) { |
| 146 | + env := []string{"HOME=/home/user", "CLAUDECODE=1", "PATH=/usr/bin"} |
| 147 | + got := filterEnv(env, "CLAUDECODE") |
| 148 | + if len(got) != 2 { |
| 149 | + t.Fatalf("got %d entries, want 2", len(got)) |
| 150 | + } |
| 151 | + for _, e := range got { |
| 152 | + if e == "CLAUDECODE=1" { |
| 153 | + t.Error("CLAUDECODE not filtered") |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestAgentRunner_RealClaude(t *testing.T) { |
| 159 | + if _, err := exec.LookPath("claude"); err != nil { |
| 160 | + t.Skip("claude not on PATH") |
| 161 | + } |
| 162 | + agents := DetectAgents() |
| 163 | + var info AgentInfo |
| 164 | + for _, a := range agents { |
| 165 | + if a.Kind == AgentClaude { |
| 166 | + info = a |
| 167 | + break |
| 168 | + } |
| 169 | + } |
| 170 | + r := NewAgentRunner(info) |
| 171 | + out, err := r.Run(context.Background(), "Say hello in exactly one word.", 30*time.Second) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("Run: %v", err) |
| 174 | + } |
| 175 | + if out == "" { |
| 176 | + t.Error("expected non-empty output") |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func TestAgentRunner_RealGemini(t *testing.T) { |
| 181 | + if _, err := exec.LookPath("gemini"); err != nil { |
| 182 | + t.Skip("gemini not on PATH") |
| 183 | + } |
| 184 | + agents := DetectAgents() |
| 185 | + var info AgentInfo |
| 186 | + for _, a := range agents { |
| 187 | + if a.Kind == AgentGemini { |
| 188 | + info = a |
| 189 | + break |
| 190 | + } |
| 191 | + } |
| 192 | + r := NewAgentRunner(info) |
| 193 | + out, err := r.Run(context.Background(), "Say hello in exactly one word.", 30*time.Second) |
| 194 | + if err != nil { |
| 195 | + if strings.Contains(err.Error(), "Permission") || strings.Contains(err.Error(), "denied") || strings.Contains(err.Error(), "auth") { |
| 196 | + t.Skipf("gemini auth not configured: %v", err) |
| 197 | + } |
| 198 | + t.Fatalf("Run: %v", err) |
| 199 | + } |
| 200 | + if out == "" { |
| 201 | + t.Error("expected non-empty output") |
| 202 | + } |
| 203 | +} |
0 commit comments