|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func fakeEnv(vars map[string]string) func(string) string { |
| 10 | + return func(key string) string { return vars[key] } |
| 11 | +} |
| 12 | + |
| 13 | +func TestDetectCLIContext_KnownAgents(t *testing.T) { |
| 14 | + cases := []struct { |
| 15 | + envVar string |
| 16 | + want string |
| 17 | + }{ |
| 18 | + {"CLAUDECODE", "agent:claude-code"}, |
| 19 | + {"CLAUDE_CODE", "agent:claude-code"}, |
| 20 | + {"CLAUDE_CODE_SESSION_ID", "agent:claude-code"}, |
| 21 | + {"CURSOR_AGENT", "agent:cursor"}, |
| 22 | + {"CODEX_THREAD_ID", "agent:codex"}, |
| 23 | + {"CODEX_SANDBOX", "agent:codex"}, |
| 24 | + {"CODEX_CI", "agent:codex"}, |
| 25 | + {"GEMINI_CLI", "agent:gemini"}, |
| 26 | + {"COPILOT_CLI", "agent:github-copilot"}, |
| 27 | + {"OPENCODE", "agent:opencode"}, |
| 28 | + {"OPENCODE_CLIENT", "agent:opencode"}, |
| 29 | + {"AMP_CURRENT_THREAD_ID", "agent:amp"}, |
| 30 | + {"AUGMENT_AGENT", "agent:augment"}, |
| 31 | + {"GOOSE_TERMINAL", "agent:goose"}, |
| 32 | + {"CLINE_ACTIVE", "agent:cline"}, |
| 33 | + {"ANTIGRAVITY_AGENT", "agent:antigravity"}, |
| 34 | + {"PI_CODING_AGENT", "agent:pi"}, |
| 35 | + {"KIRO_AGENT_PATH", "agent:kiro"}, |
| 36 | + } |
| 37 | + for _, c := range cases { |
| 38 | + t.Run(c.envVar, func(t *testing.T) { |
| 39 | + got := detectCLIContext(fakeEnv(map[string]string{c.envVar: "1"}), true, true, false) |
| 40 | + assert.Equal(t, c.want, got) |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestDetectCLIContext_GenericAIAgentVar(t *testing.T) { |
| 46 | + got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "SomeAgent"}), true, true, false) |
| 47 | + assert.Equal(t, "agent:someagent", got) |
| 48 | +} |
| 49 | + |
| 50 | +func TestDetectCLIContext_GenericAgentVar(t *testing.T) { |
| 51 | + got := detectCLIContext(fakeEnv(map[string]string{"AGENT": "goose"}), true, true, false) |
| 52 | + assert.Equal(t, "agent:goose", got) |
| 53 | +} |
| 54 | + |
| 55 | +func TestDetectCLIContext_AIAgentWinsOverAgent(t *testing.T) { |
| 56 | + env := fakeEnv(map[string]string{"AI_AGENT": "v0", "AGENT": "goose"}) |
| 57 | + assert.Equal(t, "agent:v0", detectCLIContext(env, true, true, false)) |
| 58 | +} |
| 59 | + |
| 60 | +func TestDetectCLIContext_WhitespaceOnlyGenericVar(t *testing.T) { |
| 61 | + got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": " "}), true, true, false) |
| 62 | + assert.Equal(t, "human", got) |
| 63 | +} |
| 64 | + |
| 65 | +func TestDetectCLIContext_VersionStampedGenericVar(t *testing.T) { |
| 66 | + got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "claude-code_2-1-210_agent"}), true, true, false) |
| 67 | + assert.Equal(t, "agent:claude-code", got) |
| 68 | +} |
| 69 | + |
| 70 | +func TestDetectCLIContext_LeadingUnderscoreGenericVar(t *testing.T) { |
| 71 | + got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "_foo"}), true, true, false) |
| 72 | + assert.Equal(t, "human", got) |
| 73 | +} |
| 74 | + |
| 75 | +func TestDetectCLIContext_NumericGenericVar(t *testing.T) { |
| 76 | + got := detectCLIContext(fakeEnv(map[string]string{"AGENT": "1"}), true, true, false) |
| 77 | + assert.Equal(t, "human", got) |
| 78 | +} |
| 79 | + |
| 80 | +func TestDetectCLIContext_BooleanishGenericVar(t *testing.T) { |
| 81 | + got := detectCLIContext(fakeEnv(map[string]string{"AGENT": "true"}), true, true, false) |
| 82 | + assert.Equal(t, "human", got) |
| 83 | +} |
| 84 | + |
| 85 | +func TestDetectCLIContext_OverlongGenericVar(t *testing.T) { |
| 86 | + got := detectCLIContext(fakeEnv(map[string]string{"AI_AGENT": "a-very-long-agent-name-exceeding-32-characters"}), true, true, false) |
| 87 | + assert.Equal(t, "human", got) |
| 88 | +} |
| 89 | + |
| 90 | +func TestDetectCLIContext_NamedVarWinsOverGeneric(t *testing.T) { |
| 91 | + env := fakeEnv(map[string]string{"CLAUDECODE": "1", "AI_AGENT": "other"}) |
| 92 | + assert.Equal(t, "agent:claude-code", detectCLIContext(env, true, true, false)) |
| 93 | +} |
| 94 | + |
| 95 | +func TestDetectCLIContext_NoTTYNoCI(t *testing.T) { |
| 96 | + assert.Equal(t, "agent:unknown", detectCLIContext(fakeEnv(nil), false, false, false)) |
| 97 | +} |
| 98 | + |
| 99 | +func TestDetectCLIContext_NoTTYInCI(t *testing.T) { |
| 100 | + assert.Equal(t, "human", detectCLIContext(fakeEnv(nil), false, false, true)) |
| 101 | +} |
| 102 | + |
| 103 | +func TestDetectCLIContext_PipedOutputOnly(t *testing.T) { |
| 104 | + assert.Equal(t, "human", detectCLIContext(fakeEnv(nil), true, false, false)) |
| 105 | +} |
| 106 | + |
| 107 | +func TestDetectCLIContext_Interactive(t *testing.T) { |
| 108 | + assert.Equal(t, "human", detectCLIContext(fakeEnv(nil), true, true, false)) |
| 109 | +} |
0 commit comments