|
| 1 | +package agents |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func lookup(vars map[string]string) func(string) (string, bool) { |
| 11 | + return func(key string) (string, bool) { |
| 12 | + v, ok := vars[key] |
| 13 | + return v, ok |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func TestParseAgentName(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + input string |
| 21 | + want AgentName |
| 22 | + wantErr bool |
| 23 | + }{ |
| 24 | + {name: "valid lowercase", input: "my-agent", want: "my-agent"}, |
| 25 | + {name: "valid with underscore", input: "my_agent_v2", want: "my_agent_v2"}, |
| 26 | + {name: "valid uppercase", input: "MyAgent", want: "MyAgent"}, |
| 27 | + {name: "valid numbers", input: "agent123", want: "agent123"}, |
| 28 | + {name: "spaces rejected", input: "my agent", wantErr: true}, |
| 29 | + {name: "newline rejected", input: "my\nagent", wantErr: true}, |
| 30 | + {name: "carriage return rejected", input: "my\ragent", wantErr: true}, |
| 31 | + {name: "null byte rejected", input: "my\x00agent", wantErr: true}, |
| 32 | + {name: "dot rejected", input: "my.agent", wantErr: true}, |
| 33 | + {name: "slash rejected", input: "my/agent", wantErr: true}, |
| 34 | + {name: "empty rejected", input: "", wantErr: true}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tt := range tests { |
| 38 | + t.Run(tt.name, func(t *testing.T) { |
| 39 | + got, err := parseAgentName(tt.input) |
| 40 | + if tt.wantErr { |
| 41 | + require.Error(t, err) |
| 42 | + } else { |
| 43 | + require.NoError(t, err) |
| 44 | + assert.Equal(t, tt.want, got) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestDetectWith(t *testing.T) { |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + env map[string]string |
| 54 | + wantAgent AgentName |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "clean environment", |
| 58 | + env: map[string]string{}, |
| 59 | + wantAgent: "", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "empty var is not detected", |
| 63 | + env: map[string]string{"GEMINI_CLI": ""}, |
| 64 | + wantAgent: "", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "AGENT=amp detected as amp", |
| 68 | + env: map[string]string{"AGENT": "amp"}, |
| 69 | + wantAgent: "amp", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "AGENT with non-amp value is ignored", |
| 73 | + env: map[string]string{"AGENT": "other"}, |
| 74 | + wantAgent: "", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "AI_AGENT returns value as agent name", |
| 78 | + env: map[string]string{"AI_AGENT": "some-agent"}, |
| 79 | + wantAgent: "some-agent", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "AI_AGENT with invalid characters is ignored", |
| 83 | + env: map[string]string{"AI_AGENT": "bad\nagent"}, |
| 84 | + wantAgent: "", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "AI_AGENT with spaces is ignored", |
| 88 | + env: map[string]string{"AI_AGENT": "bad agent"}, |
| 89 | + wantAgent: "", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "AI_AGENT takes priority over AGENT", |
| 93 | + env: map[string]string{"AGENT": "amp", "AI_AGENT": "other"}, |
| 94 | + wantAgent: "other", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "CODEX_SANDBOX", |
| 98 | + env: map[string]string{"CODEX_SANDBOX": "seatbelt"}, |
| 99 | + wantAgent: "codex", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "CODEX_CI", |
| 103 | + env: map[string]string{"CODEX_CI": "1"}, |
| 104 | + wantAgent: "codex", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "CODEX_THREAD_ID", |
| 108 | + env: map[string]string{"CODEX_THREAD_ID": "abc"}, |
| 109 | + wantAgent: "codex", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "GEMINI_CLI", |
| 113 | + env: map[string]string{"GEMINI_CLI": "1"}, |
| 114 | + wantAgent: "gemini-cli", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "COPILOT_CLI", |
| 118 | + env: map[string]string{"COPILOT_CLI": "1"}, |
| 119 | + wantAgent: "copilot-cli", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "OPENCODE", |
| 123 | + env: map[string]string{"OPENCODE": "1"}, |
| 124 | + wantAgent: "opencode", |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "CLAUDECODE", |
| 128 | + env: map[string]string{"CLAUDECODE": "1"}, |
| 129 | + wantAgent: "claude-code", |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "AGENT=amp takes priority over CLAUDECODE", |
| 133 | + env: map[string]string{"AGENT": "amp", "CLAUDECODE": "1"}, |
| 134 | + wantAgent: "amp", |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "invalid AI_AGENT falls through to tool-specific detection", |
| 138 | + env: map[string]string{"AI_AGENT": "bad agent", "GEMINI_CLI": "1"}, |
| 139 | + wantAgent: "gemini-cli", |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + got := detectWith(lookup(tt.env)) |
| 146 | + assert.Equal(t, tt.wantAgent, got) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments