-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathagent_test.go
More file actions
783 lines (689 loc) · 22.6 KB
/
Copy pathagent_test.go
File metadata and controls
783 lines (689 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
package agent
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/roborev/internal/config"
"go.kenn.io/roborev/internal/testenv"
)
func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "roborev-agy-settings-*")
if err == nil {
antigravitySettingsPathForTest = func() string {
return filepath.Join(dir, ".gemini", "antigravity-cli", "settings.json")
}
}
code := testenv.RunIsolatedMain(m)
if dir != "" {
_ = os.RemoveAll(dir)
}
os.Exit(code)
}
func TestAgentRegistry(t *testing.T) {
// Check that all agents are registered
agents := expectedAgents
for _, name := range agents {
a, err := Get(name)
require.NoError(t, err, "Failed to get %s agent", name)
assert.Equal(t, name, a.Name())
}
// Check unknown agent
_, err := Get("unknown-agent")
require.Error(t, err)
}
func TestCanonicalNameAliases(t *testing.T) {
tests := []struct {
input string
want string
}{
{input: "claude", want: "claude-code"},
{input: "agent", want: "cursor"},
{input: "cursor", want: "cursor"},
{input: "grok-build", want: "grok"},
{input: "grok", want: "grok"},
}
for _, tt := range tests {
assert.Equal(t, tt.want, CanonicalName(tt.input), "CanonicalName(%q)", tt.input)
}
}
func TestGetSupportsAgentAlias(t *testing.T) {
a, err := Get("agent")
require.NoError(t, err)
assert.Equal(t, "cursor", a.Name())
}
func TestAvailableAgents(t *testing.T) {
agents := Available()
assert.GreaterOrEqual(t, len(agents), len(expectedAgents), "agents=%v", agents)
for _, name := range expectedAgents {
assert.Contains(t, agents, name)
}
}
func TestSyncWriter(t *testing.T) {
t.Run("nil input returns nil", func(t *testing.T) {
sw := newSyncWriter(nil)
assert.Nil(t, sw)
})
t.Run("wraps writer correctly", func(t *testing.T) {
var buf bytes.Buffer
sw := newSyncWriter(&buf)
require.NotNil(t, sw)
n, err := sw.Write([]byte("hello"))
require.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, "hello", buf.String())
})
t.Run("concurrent writes are safe", func(t *testing.T) {
var buf bytes.Buffer
sw := newSyncWriter(&buf)
var wg sync.WaitGroup
for i := range 100 {
wg.Add(1)
go func(n int) {
defer wg.Done()
sw.Write([]byte("x"))
}(i)
}
wg.Wait()
assert.Equal(t, 100, buf.Len())
})
}
func TestTestAgentStreaming(t *testing.T) {
setup := func() *TestAgent {
a := NewTestAgent()
a.Delay = 1 * time.Millisecond
a.Output = "test output"
return a
}
t.Run("streams output to writer", func(t *testing.T) {
agent := setup()
var buf bytes.Buffer
result, err := agent.Review(context.Background(), "/tmp", "abc1234567", "prompt", &buf)
require.NoError(t, err)
// The streamed output is the returned body prefixed by a synthetic
// session JSONL line (consumed by SessionCaptureWriter). Verify the
// body is present in the stream rather than asserting exact equality.
assert.Contains(t, buf.String(), result, "streamed output should contain the returned body")
})
t.Run("nil output writer works", func(t *testing.T) {
agent := setup()
result, err := agent.Review(context.Background(), "/tmp", "abc1234567", "prompt", nil)
require.NoError(t, err)
assert.NotEmpty(t, result)
})
t.Run("returns write error", func(t *testing.T) {
agent := setup()
errWriter := &FailingWriter{Err: errors.New("write failed")}
_, err := agent.Review(context.Background(), "/tmp", "abc1234567", "prompt", errWriter)
require.Error(t, err)
assert.ErrorIs(t, err, errWriter.Err)
})
}
func TestParseReasoningLevel(t *testing.T) {
tests := []struct {
input string
want ReasoningLevel
}{
{"maximum", ReasoningMaximum},
{"max", ReasoningMaximum},
{"xhigh", ReasoningMaximum},
{"thorough", ReasoningThorough},
{"high", ReasoningThorough},
{"fast", ReasoningFast},
{"low", ReasoningFast},
{"medium", ReasoningMedium},
{"standard", ReasoningStandard},
{"", ReasoningStandard},
{"unknown", ReasoningStandard},
}
for _, tt := range tests {
assert.Equal(t, tt.want, ParseReasoningLevel(tt.input), "ParseReasoningLevel(%q)", tt.input)
}
}
func TestCodexReasoningEffortMapping(t *testing.T) {
tests := []struct {
level ReasoningLevel
want string
}{
{ReasoningMaximum, "xhigh"},
{ReasoningThorough, "high"},
{ReasoningFast, "low"},
{ReasoningStandard, ""},
}
for _, tt := range tests {
a := NewCodexAgent("").WithReasoning(tt.level)
codex, ok := a.(*CodexAgent)
require.True(t, ok, "expected CodexAgent, got %T", a)
assert.Equal(t, tt.want, codex.codexReasoningEffort(), "codexReasoningEffort(%q)", tt.level)
}
}
func TestClaudeEffortMapping(t *testing.T) {
tests := []struct {
level ReasoningLevel
want string
}{
{ReasoningMaximum, "max"},
{ReasoningThorough, "high"},
{ReasoningMedium, "medium"},
{ReasoningFast, "low"},
{ReasoningStandard, ""},
}
for _, tt := range tests {
a := NewClaudeAgent("").WithReasoning(tt.level)
claude, ok := a.(*ClaudeAgent)
require.True(t, ok, "expected ClaudeAgent, got %T", a)
assert.Equal(t, tt.want, claude.claudeEffort(), "claudeEffort(%q)", tt.level)
}
}
type agentTestDef struct {
name string
factory func(string) Agent
modelFlag string
defaultModel string
testModel string
supportsSmartReview bool
supportsPlainFlagEcho bool
}
var agentFixtures = []agentTestDef{
{"codex", func(s string) Agent { return NewCodexAgent(s) }, "-m", "", "o3", true, false},
{"claude", func(s string) Agent { return NewClaudeAgent(s) }, "--model", "", "opus", true, false},
{"gemini", func(s string) Agent { return NewGeminiAgent(s) }, "-m", "gemini-3.1-pro-preview", "gemini-1.5-pro", true, false},
{"copilot", func(s string) Agent { return NewCopilotAgent(s) }, "--model", "", "gpt-4o", false, true},
{"opencode", func(s string) Agent { return NewOpenCodeAgent(s) }, "--model", "", "anthropic/claude-sonnet-4", false, false},
{"cursor", func(s string) Agent { return NewCursorAgent(s) }, "--model", "auto", "claude-sonnet-4", false, false},
{"kilo", func(s string) Agent { return NewKiloAgent(s) }, "--model", "", "anthropic/claude-sonnet-4-20250514", false, false},
{"grok", func(s string) Agent { return NewGrokAgent(s) }, "-m", "", "grok-4.5", false, false},
}
func assertArgsNotContain(t *testing.T, cmdLine, flag string) {
t.Helper()
for token := range strings.FieldsSeq(cmdLine) {
assert.Condition(t, func() bool { return token != flag && !strings.HasPrefix(token, flag+"=") }, "command line %q unexpectedly contained flag %q", cmdLine, flag)
}
}
func TestAgentWithModelPersistence(t *testing.T) {
for _, tt := range agentFixtures {
t.Run(tt.name+"/WithModel sets model", func(t *testing.T) {
a := tt.factory("").WithModel(tt.testModel)
cmdLine := a.CommandLine()
assertArgsContain(t, cmdLine, tt.modelFlag, tt.testModel)
})
t.Run(tt.name+"/model persists through WithReasoning", func(t *testing.T) {
a := tt.factory("").WithModel(tt.testModel).WithReasoning(ReasoningThorough)
cmdLine := a.CommandLine()
assertArgsContain(t, cmdLine, tt.modelFlag, tt.testModel)
})
t.Run(tt.name+"/model persists through WithAgentic", func(t *testing.T) {
a := tt.factory("").WithModel(tt.testModel).WithAgentic(true)
cmdLine := a.CommandLine()
assertArgsContain(t, cmdLine, tt.modelFlag, tt.testModel)
})
t.Run(tt.name+"/model persists through chained calls", func(t *testing.T) {
a := tt.factory("").WithModel(tt.testModel).WithReasoning(ReasoningFast).WithAgentic(true)
cmdLine := a.CommandLine()
assertArgsContain(t, cmdLine, tt.modelFlag, tt.testModel)
})
}
}
func TestWithModelEmptyPreservesDefault(t *testing.T) {
for _, tt := range agentFixtures {
t.Run(tt.name, func(t *testing.T) {
a := tt.factory("")
b := a.WithModel("")
cmdLine := b.CommandLine()
if tt.defaultModel == "" {
assertArgsNotContain(t, cmdLine, tt.modelFlag)
} else {
assertArgsContain(t, cmdLine, tt.modelFlag, tt.defaultModel)
}
})
t.Run(tt.name+"/explicit then empty preserves explicit", func(t *testing.T) {
a := tt.factory("").WithModel("custom-model")
b := a.WithModel("")
cmdLine := b.CommandLine()
assertArgsContain(t, cmdLine, tt.modelFlag, "custom-model")
})
}
}
func TestClaudeBuildArgsEffort(t *testing.T) {
a := NewClaudeAgent("").WithModel("opus").WithReasoning(ReasoningMaximum)
cmdLine := a.CommandLine()
assert.Contains(t, cmdLine, "--effort max")
assert.Contains(t, cmdLine, "--model opus")
}
func TestClaudeBuildArgsNoEffortForStandard(t *testing.T) {
a := NewClaudeAgent("").WithReasoning(ReasoningStandard)
cmdLine := a.CommandLine()
assert.NotContains(t, cmdLine, "--effort")
}
func TestAgentBuildArgsWithModel(t *testing.T) {
for _, tt := range agentFixtures {
t.Run(tt.name+" with explicit model", func(t *testing.T) {
agent := tt.factory("").WithModel(tt.testModel)
cmdLine := agent.CommandLine()
assertArgsContain(t, cmdLine, tt.modelFlag, tt.testModel)
})
t.Run(tt.name+" without model", func(t *testing.T) {
agent := tt.factory("")
cmdLine := agent.CommandLine()
if tt.defaultModel == "" {
assertArgsNotContain(t, cmdLine, tt.modelFlag)
} else {
assertArgsContain(t, cmdLine, tt.modelFlag, tt.defaultModel)
}
})
}
}
func TestCodexBuildArgsModelWithReasoning(t *testing.T) {
a := NewCodexAgent("").WithModel("o4-mini").WithReasoning(ReasoningThorough)
cmdLine := a.CommandLine()
assert.Contains(t, cmdLine, "-m o4-mini")
assert.Contains(t, cmdLine, `-c model_reasoning_effort="high"`)
}
func TestSessionAgentsPreserveStateAcrossCloneMethods(t *testing.T) {
tests := []struct {
name string
agent Agent
verify func(*testing.T, Agent)
}{
{
name: "codex",
agent: NewCodexAgent("codex").
WithSessionID("session-123").
WithModel("o4-mini").
WithReasoning(ReasoningThorough).
WithAgentic(true),
verify: func(t *testing.T, a Agent) {
codex, ok := a.(*CodexAgent)
require.True(t, ok)
assert.Equal(t, "session-123", codex.SessionID)
assert.Equal(t, "o4-mini", codex.Model)
assert.Equal(t, ReasoningThorough, codex.Reasoning)
assert.True(t, codex.Agentic)
},
},
{
name: "claude",
agent: NewClaudeAgent("claude").
WithSessionID("session-123").
WithModel("opus").
WithReasoning(ReasoningThorough).
WithAgentic(true),
verify: func(t *testing.T, a Agent) {
claude, ok := a.(*ClaudeAgent)
require.True(t, ok)
assert.Equal(t, "session-123", claude.SessionID)
assert.Equal(t, "opus", claude.Model)
assert.Equal(t, ReasoningThorough, claude.Reasoning)
assert.True(t, claude.Agentic)
},
},
{
name: "opencode",
agent: NewOpenCodeAgent("opencode").
WithSessionID("session-123").
WithModel("anthropic/claude-sonnet-4").
WithReasoning(ReasoningThorough).
WithAgentic(true),
verify: func(t *testing.T, a Agent) {
opencode, ok := a.(*OpenCodeAgent)
require.True(t, ok)
assert.Equal(t, "session-123", opencode.SessionID)
assert.Equal(t, "anthropic/claude-sonnet-4", opencode.Model)
assert.Equal(t, ReasoningThorough, opencode.Reasoning)
assert.True(t, opencode.Agentic)
},
},
{
name: "kilo",
agent: NewKiloAgent("kilo").
WithSessionID("session-123").
WithModel("anthropic/claude-sonnet-4").
WithReasoning(ReasoningThorough).
WithAgentic(true),
verify: func(t *testing.T, a Agent) {
kilo, ok := a.(*KiloAgent)
require.True(t, ok)
assert.Equal(t, "session-123", kilo.SessionID)
assert.Equal(t, "anthropic/claude-sonnet-4", kilo.Model)
assert.Equal(t, ReasoningThorough, kilo.Reasoning)
assert.True(t, kilo.Agentic)
},
},
{
name: "pi",
agent: func() Agent {
pi := NewPiAgent("pi").WithProvider("anthropic").(*PiAgent)
return pi.
WithSessionID("session-123").
WithModel("claude-sonnet-4").
WithReasoning(ReasoningThorough).
WithAgentic(true)
}(),
verify: func(t *testing.T, a Agent) {
pi, ok := a.(*PiAgent)
require.True(t, ok)
assert.Equal(t, "anthropic", pi.Provider)
assert.Equal(t, "session-123", pi.SessionID)
assert.Equal(t, "claude-sonnet-4", pi.Model)
assert.Equal(t, ReasoningThorough, pi.Reasoning)
assert.True(t, pi.Agentic)
assert.Equal(t, config.DefaultPiJSONSchemaExtension, pi.JSONSchemaExtension)
},
},
{
name: "grok",
agent: NewGrokAgent("grok").
WithSessionID("session-123").
WithModel("grok-4.5").
WithReasoning(ReasoningThorough).
WithAgentic(true),
verify: func(t *testing.T, a Agent) {
grok, ok := a.(*GrokAgent)
require.True(t, ok)
assert.Equal(t, "session-123", grok.SessionID)
assert.Equal(t, "grok-4.5", grok.Model)
assert.Equal(t, ReasoningThorough, grok.Reasoning)
assert.True(t, grok.Agentic)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.verify(t, tt.agent)
})
}
}
func assertArgsContain(t *testing.T, cmdLine, flag, value string) {
t.Helper()
tokens := strings.Fields(cmdLine)
found := false
for i := 0; i < len(tokens)-1; i++ {
if tokens[i] == flag && tokens[i+1] == value {
found = true
break
}
}
assert.Condition(t, func() bool { return found }, "command line %q expected to contain flag %q followed by value %q", cmdLine, flag, value)
}
func TestSmartAgentReviewPassesModelFlag(t *testing.T) {
skipIfWindows(t)
for _, tt := range agentFixtures {
if !tt.supportsSmartReview {
continue
}
t.Run(tt.name, func(t *testing.T) {
helpOutput := ""
jsonOutput := `{"type": "result", "result": "review result"}`
if tt.name == "codex" {
helpOutput = "--sandbox"
jsonOutput = `{"type": "item.completed", "item": {"type": "agent_message", "text": "review result"}}`
}
opts := MockCLIOpts{
HelpOutput: helpOutput,
CaptureArgs: true,
StdoutLines: []string{jsonOutput},
}
mock := mockAgentCLI(t, opts)
agent := tt.factory(mock.CmdPath).WithModel(tt.testModel)
_, err := agent.Review(context.Background(), t.TempDir(), "head", "prompt", nil)
require.NoError(t, err)
argsBytes, err := os.ReadFile(mock.ArgsFile)
require.NoError(t, err)
args := string(argsBytes)
assertArgsContain(t, args, tt.modelFlag, tt.testModel)
})
}
}
func TestAgentReviewPassesModelFlag(t *testing.T) {
for _, tt := range agentFixtures {
// opencode uses JSON streaming so verifyAgentPassesFlag (plain text echo)
// doesn't work; model flag is verified in TestOpenCodeReviewModelFlag.
if !tt.supportsPlainFlagEcho {
continue
}
t.Run(tt.name, func(t *testing.T) {
verifyAgentPassesFlag(t, func(cmdPath string) Agent {
return tt.factory(cmdPath).WithModel(tt.testModel)
}, tt.modelFlag, tt.testModel)
})
}
}
func TestGetAvailableRejectsUnknownAgent(t *testing.T) {
_, err := GetAvailable("typo-agent")
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown agent")
}
func TestGetAvailableFallsBackForKnownUnavailable(t *testing.T) {
// Isolate registry: "codex" has a missing binary, "claude-code"
// has its binary on PATH. Request "codex" and verify fallback
// returns "claude-code" without an "unknown agent" error.
fakeBin := t.TempDir()
binName := "claude"
if runtime.GOOS == "windows" {
binName += ".exe"
}
claudeBin := filepath.Join(fakeBin, binName)
require.NoError(t, os.WriteFile(claudeBin, []byte("#!/bin/sh\nexit 0\n"), 0o755))
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"codex": NewCodexAgent("definitely-not-on-path"),
"claude-code": NewClaudeAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
resolved, err := GetAvailable("codex")
require.NoError(t, err)
assert.Equal(t, "claude-code", resolved.Name())
}
func TestGetAvailableTriesBackupBeforeChain(t *testing.T) {
// Setup: codex unavailable, gemini available, claude-code available.
// Without backup: GetAvailable("codex") → claude-code (first in chain).
// With backup "gemini": GetAvailable("codex", "gemini") → gemini.
fakeBin := t.TempDir()
for _, bin := range []string{"gemini", "claude"} {
name := bin
if runtime.GOOS == "windows" {
name += ".exe"
}
p := filepath.Join(fakeBin, name)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
}
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"codex": NewCodexAgent("definitely-not-on-path"),
"gemini": NewGeminiAgent(""),
"claude-code": NewClaudeAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
// With backup, should pick gemini (not claude-code from chain)
resolved, err := GetAvailable("codex", "gemini")
require.NoError(t, err)
assert.Equal(t, "gemini", resolved.Name())
// Without backup, should still fall through to claude-code
resolved, err = GetAvailable("codex")
require.NoError(t, err)
assert.Equal(t, "claude-code", resolved.Name())
}
func TestGetAvailablePrefersAntigravityForGemini(t *testing.T) {
fakeBin := t.TempDir()
for _, bin := range []string{"agy", "gemini"} {
name := bin
if runtime.GOOS == "windows" {
name += ".exe"
}
p := filepath.Join(fakeBin, name)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
}
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"gemini": NewGeminiAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
resolved, err := GetAvailable("gemini")
require.NoError(t, err)
require.IsType(t, &GeminiAgent{}, resolved)
gemini := resolved.(*GeminiAgent)
assert.Equal(t, "gemini", gemini.Name())
assert.Equal(t, "agy", gemini.CommandName())
assert.True(t, gemini.CommandAuto)
}
func TestGeminiWithModelKeepsAutoSelectedAntigravity(t *testing.T) {
fakeBin := t.TempDir()
for _, bin := range []string{"agy", "gemini"} {
name := bin
if runtime.GOOS == "windows" {
name += ".exe"
}
p := filepath.Join(fakeBin, name)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
}
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"gemini": NewGeminiAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
resolved, err := GetAvailable("gemini")
require.NoError(t, err)
withModel := resolved.WithModel("gemini-1.5-pro")
require.IsType(t, &GeminiAgent{}, withModel)
gemini := withModel.(*GeminiAgent)
assert.Equal(t, "agy", gemini.CommandName())
assert.True(t, gemini.ModelExplicit)
assert.True(t, gemini.CommandAuto)
assert.NotContains(t, gemini.CommandLine(), "-m")
}
func TestGetAvailableWithConfigGeminiCmdPinsLegacyCommand(t *testing.T) {
fakeBin := t.TempDir()
for _, bin := range []string{"agy", "gemini"} {
name := bin
if runtime.GOOS == "windows" {
name += ".exe"
}
p := filepath.Join(fakeBin, name)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
}
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"gemini": NewGeminiAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
resolved, err := GetAvailableWithConfig("", "gemini", &config.Config{GeminiCmd: "gemini"})
require.NoError(t, err)
require.IsType(t, &GeminiAgent{}, resolved)
gemini := resolved.(*GeminiAgent)
assert.Equal(t, "gemini", gemini.CommandName())
assert.False(t, gemini.CommandAuto)
}
func TestGetAvailableWithConfigGeminiCmdUnavailableDoesNotFallBackToAntigravity(t *testing.T) {
fakeBin := t.TempDir()
name := "agy"
if runtime.GOOS == "windows" {
name += ".exe"
}
p := filepath.Join(fakeBin, name)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"gemini": NewGeminiAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
_, err := GetAvailableWithConfig("", "gemini", &config.Config{GeminiCmd: "gemini"})
require.Error(t, err)
assert.Contains(t, err.Error(), "no agents available")
}
func TestGetAvailableFallsBackToGeminiWhenAntigravityMissing(t *testing.T) {
fakeBin := t.TempDir()
name := "gemini"
if runtime.GOOS == "windows" {
name += ".exe"
}
p := filepath.Join(fakeBin, name)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"gemini": NewGeminiAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
resolved, err := GetAvailable("gemini")
require.NoError(t, err)
require.IsType(t, &GeminiAgent{}, resolved)
gemini := resolved.(*GeminiAgent)
assert.Equal(t, "gemini", gemini.CommandName())
}
func TestGetAvailableBackupSkipsDuplicateAndEmpty(t *testing.T) {
// Backup that matches preferred or is empty should be skipped.
fakeBin := t.TempDir()
binName := "claude"
if runtime.GOOS == "windows" {
binName += ".exe"
}
p := filepath.Join(fakeBin, binName)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"codex": NewCodexAgent("definitely-not-on-path"),
"claude-code": NewClaudeAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
// Backup same as preferred → skipped, falls through to chain
resolved, err := GetAvailable("codex", "codex", "")
require.NoError(t, err)
assert.Equal(t, "claude-code", resolved.Name())
}
func TestGetAvailableBackupResolvesAliases(t *testing.T) {
// Backup "claude" should resolve to "claude-code" via alias.
fakeBin := t.TempDir()
binName := "claude"
if runtime.GOOS == "windows" {
binName += ".exe"
}
p := filepath.Join(fakeBin, binName)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"codex": NewCodexAgent("definitely-not-on-path"),
"claude-code": NewClaudeAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
resolved, err := GetAvailable("codex", "claude")
require.NoError(t, err)
assert.Equal(t, "claude-code", resolved.Name())
}
func TestGetAvailableBackupUnavailableFallsToChain(t *testing.T) {
// Backup agent that is unavailable should be skipped, chain used.
fakeBin := t.TempDir()
binName := "claude"
if runtime.GOOS == "windows" {
binName += ".exe"
}
p := filepath.Join(fakeBin, binName)
require.NoError(t, os.WriteFile(p, []byte("#!/bin/sh\nexit 0\n"), 0o755))
t.Setenv("PATH", fakeBin)
originalRegistry := registry
registry = map[string]Agent{
"codex": NewCodexAgent("definitely-not-on-path"),
"gemini": NewGeminiAgent("also-not-on-path"),
"claude-code": NewClaudeAgent(""),
}
t.Cleanup(func() { registry = originalRegistry })
// Backup gemini is registered but unavailable → falls to chain
resolved, err := GetAvailable("codex", "gemini")
require.NoError(t, err)
assert.Equal(t, "claude-code", resolved.Name())
}