Repository navigation
Expand file tree
/
Copy pathdetect_test.go
More file actions
244 lines (237 loc) · 6.65 KB
/
Copy pathdetect_test.go
File metadata and controls
244 lines (237 loc) · 6.65 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
package agents
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func lookup(vars map[string]string) func(string) (string, bool) {
return func(key string) (string, bool) {
v, ok := vars[key]
return v, ok
}
}
func TestParseAgentName(t *testing.T) {
tests := []struct {
name string
input string
want AgentName
wantErr bool
}{
{name: "valid lowercase", input: "my-agent", want: "my-agent"},
{name: "valid with underscore", input: "my_agent_v2", want: "my_agent_v2"},
{name: "valid uppercase", input: "MyAgent", want: "MyAgent"},
{name: "valid numbers", input: "agent123", want: "agent123"},
{name: "spaces rejected", input: "my agent", wantErr: true},
{name: "newline rejected", input: "my\nagent", wantErr: true},
{name: "carriage return rejected", input: "my\ragent", wantErr: true},
{name: "null byte rejected", input: "my\x00agent", wantErr: true},
{name: "dot rejected", input: "my.agent", wantErr: true},
{name: "slash rejected", input: "my/agent", wantErr: true},
{name: "empty rejected", input: "", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseAgentName(tt.input)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
func TestDetectWith(t *testing.T) {
tests := []struct {
name string
env map[string]string
wantAgent AgentName
}{
{
name: "clean environment",
env: map[string]string{},
wantAgent: "",
},
{
name: "empty var is not detected",
env: map[string]string{"GEMINI_CLI": ""},
wantAgent: "",
},
{
name: "AGENT=amp detected as amp",
env: map[string]string{"AGENT": "amp"},
wantAgent: "amp",
},
{
name: "AGENT with non-amp value is ignored",
env: map[string]string{"AGENT": "other"},
wantAgent: "",
},
{
name: "AI_AGENT returns value as agent name",
env: map[string]string{"AI_AGENT": "some-agent"},
wantAgent: "some-agent",
},
{
name: "AI_AGENT with invalid characters is ignored",
env: map[string]string{"AI_AGENT": "bad\nagent"},
wantAgent: "",
},
{
name: "AI_AGENT with spaces is ignored",
env: map[string]string{"AI_AGENT": "bad agent"},
wantAgent: "",
},
{
name: "AI_AGENT takes priority over AGENT",
env: map[string]string{"AGENT": "amp", "AI_AGENT": "other"},
wantAgent: "other",
},
{
name: "CODEX_SANDBOX",
env: map[string]string{"CODEX_SANDBOX": "seatbelt"},
wantAgent: "codex",
},
{
name: "CODEX_CI",
env: map[string]string{"CODEX_CI": "1"},
wantAgent: "codex",
},
{
name: "CODEX_THREAD_ID",
env: map[string]string{"CODEX_THREAD_ID": "abc"},
wantAgent: "codex",
},
{
name: "GEMINI_CLI",
env: map[string]string{"GEMINI_CLI": "1"},
wantAgent: "gemini-cli",
},
{
name: "COPILOT_CLI",
env: map[string]string{"COPILOT_CLI": "1"},
wantAgent: "copilot-cli",
},
{
name: "OPENCODE",
env: map[string]string{"OPENCODE": "1"},
wantAgent: "opencode",
},
{
name: "CLAUDECODE",
env: map[string]string{"CLAUDECODE": "1"},
wantAgent: "claude-code",
},
{
name: "AGENT=amp takes priority over CLAUDECODE",
env: map[string]string{"AGENT": "amp", "CLAUDECODE": "1"},
wantAgent: "amp",
},
{
name: "invalid AI_AGENT falls through to tool-specific detection",
env: map[string]string{"AI_AGENT": "bad agent", "GEMINI_CLI": "1"},
wantAgent: "gemini-cli",
},
{
name: "ANTIGRAVITY_AGENT",
env: map[string]string{"ANTIGRAVITY_AGENT": "1"},
wantAgent: "antigravity",
},
{
name: "AUGMENT_AGENT",
env: map[string]string{"AUGMENT_AGENT": "1"},
wantAgent: "augment-cli",
},
{
name: "REPL_ID",
env: map[string]string{"REPL_ID": "abc123"},
wantAgent: "replit",
},
{
name: "GOOSE_PROVIDER",
env: map[string]string{"GOOSE_PROVIDER": "anthropic"},
wantAgent: "goose",
},
{
name: "claude-code takes priority over goose",
env: map[string]string{"GOOSE_PROVIDER": "anthropic", "CLAUDECODE": "1"},
wantAgent: "claude-code",
},
{
name: "kiro takes priority over goose",
env: map[string]string{"GOOSE_PROVIDER": "anthropic", "TERM_PROGRAM": "kiro"},
wantAgent: "kiro",
},
{
name: "CLAUDE_CODE_IS_COWORK detected as cowork",
env: map[string]string{"CLAUDE_CODE_IS_COWORK": "1"},
wantAgent: "cowork",
},
{
name: "cowork takes priority over CLAUDECODE",
env: map[string]string{"CLAUDE_CODE_IS_COWORK": "1", "CLAUDECODE": "1"},
wantAgent: "cowork",
},
{
name: "CLAUDE_CODE",
env: map[string]string{"CLAUDE_CODE": "1"},
wantAgent: "claude-code",
},
{
name: "CURSOR_TRACE_ID detected as cursor",
env: map[string]string{"CURSOR_TRACE_ID": "abc"},
wantAgent: "cursor",
},
{
name: "CURSOR_AGENT detected as cursor-cli",
env: map[string]string{"CURSOR_AGENT": "1"},
wantAgent: "cursor-cli",
},
{
name: "CURSOR_EXTENSION_HOST_ROLE agent-exec detected as cursor-cli",
env: map[string]string{"CURSOR_EXTENSION_HOST_ROLE": "agent-exec"},
wantAgent: "cursor-cli",
},
{
name: "CURSOR_EXTENSION_HOST_ROLE with other value is ignored",
env: map[string]string{"CURSOR_EXTENSION_HOST_ROLE": "worker"},
wantAgent: "",
},
{
name: "CURSOR_TRACE_ID takes priority over CURSOR_AGENT",
env: map[string]string{"CURSOR_TRACE_ID": "abc", "CURSOR_AGENT": "1"},
wantAgent: "cursor",
},
{
name: "TERM_PROGRAM kiro detected as kiro",
env: map[string]string{"TERM_PROGRAM": "kiro"},
wantAgent: "kiro",
},
{
name: "TERM_PROGRAM with kiro as a substring is ignored",
env: map[string]string{"TERM_PROGRAM": "kirostudio"},
wantAgent: "",
},
{
name: "PATH containing .pi/agent detected as pi",
env: map[string]string{"PATH": "/usr/bin:/home/user/.pi/agent/bin"},
wantAgent: "pi",
},
{
name: "PATH with .pi/agent not on a path boundary is ignored",
env: map[string]string{"PATH": "/usr/bin:/home/user/x.pi/agent"},
wantAgent: "",
},
{
name: "PATH with Windows .pi\\agent separators detected as pi",
env: map[string]string{"PATH": `C:\Windows;C:\Users\user\.pi\agent\bin`},
wantAgent: "pi",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := detectWith(lookup(tt.env))
assert.Equal(t, tt.wantAgent, got)
})
}
}