|
| 1 | +package analytics |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +type mockEnvChecker struct { |
| 11 | + envVars map[string]string |
| 12 | + stdinTerminal bool |
| 13 | + stdoutTerminal bool |
| 14 | +} |
| 15 | + |
| 16 | +func newMockEnv(envVars map[string]string, bothTerminal bool) mockEnvChecker { |
| 17 | + return mockEnvChecker{envVars: envVars, stdinTerminal: bothTerminal, stdoutTerminal: bothTerminal} |
| 18 | +} |
| 19 | + |
| 20 | +func (m mockEnvChecker) Getenv(key string) string { return m.envVars[key] } |
| 21 | +func (m mockEnvChecker) IsTerminal(fd int) bool { |
| 22 | + if fd == 0 { |
| 23 | + return m.stdinTerminal |
| 24 | + } |
| 25 | + return m.stdoutTerminal |
| 26 | +} |
| 27 | +func (m mockEnvChecker) StdinFd() int { return 0 } |
| 28 | +func (m mockEnvChecker) StdoutFd() int { return 1 } |
| 29 | + |
| 30 | +func TestDetectAgentContext(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + env mockEnvChecker |
| 34 | + expected string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "explicit LD_CLI_AGENT", |
| 38 | + env: newMockEnv(map[string]string{"LD_CLI_AGENT": "my-skill"}, false), |
| 39 | + expected: "explicit:my-skill", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "explicit LD_CLI_AGENT takes precedence over known env vars", |
| 43 | + env: newMockEnv(map[string]string{"LD_CLI_AGENT": "custom", "CURSOR_SESSION_ID": "abc"}, false), |
| 44 | + expected: "explicit:custom", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "CURSOR_SESSION_ID detected", |
| 48 | + env: newMockEnv(map[string]string{"CURSOR_SESSION_ID": "abc"}, false), |
| 49 | + expected: "cursor", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "CURSOR_TRACE_ID detected", |
| 53 | + env: newMockEnv(map[string]string{"CURSOR_TRACE_ID": "xyz"}, false), |
| 54 | + expected: "cursor", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "CLAUDE_CODE detected", |
| 58 | + env: newMockEnv(map[string]string{"CLAUDE_CODE": "1"}, false), |
| 59 | + expected: "claude-code", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "CLAUDE_CODE_SESSION detected", |
| 63 | + env: newMockEnv(map[string]string{"CLAUDE_CODE_SESSION": "sess"}, false), |
| 64 | + expected: "claude-code", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "CODEX_SESSION detected", |
| 68 | + env: newMockEnv(map[string]string{"CODEX_SESSION": "s"}, false), |
| 69 | + expected: "codex", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "CODEX_SANDBOX_ID detected", |
| 73 | + env: newMockEnv(map[string]string{"CODEX_SANDBOX_ID": "sb"}, false), |
| 74 | + expected: "codex", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "DEVIN_SESSION detected", |
| 78 | + env: newMockEnv(map[string]string{"DEVIN_SESSION": "d"}, false), |
| 79 | + expected: "devin", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "GITHUB_COPILOT detected", |
| 83 | + env: newMockEnv(map[string]string{"GITHUB_COPILOT": "1"}, false), |
| 84 | + expected: "copilot", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "WINDSURF_SESSION detected", |
| 88 | + env: newMockEnv(map[string]string{"WINDSURF_SESSION": "w"}, false), |
| 89 | + expected: "windsurf", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "CLINE_TASK_ID detected", |
| 93 | + env: newMockEnv(map[string]string{"CLINE_TASK_ID": "t"}, false), |
| 94 | + expected: "cline", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "AIDER_MODEL detected", |
| 98 | + env: newMockEnv(map[string]string{"AIDER_MODEL": "gpt-4"}, false), |
| 99 | + expected: "aider", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "no TTY and no env vars returns unknown-non-interactive", |
| 103 | + env: newMockEnv(map[string]string{}, false), |
| 104 | + expected: "unknown-non-interactive", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "CI env var returns ci", |
| 108 | + env: newMockEnv(map[string]string{"CI": "true"}, false), |
| 109 | + expected: "ci", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "GITHUB_ACTIONS env var returns ci", |
| 113 | + env: newMockEnv(map[string]string{"GITHUB_ACTIONS": "true"}, false), |
| 114 | + expected: "ci", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "GITLAB_CI env var returns ci", |
| 118 | + env: newMockEnv(map[string]string{"GITLAB_CI": "true"}, false), |
| 119 | + expected: "ci", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "agent env var takes precedence over CI env var", |
| 123 | + env: newMockEnv(map[string]string{"CURSOR_SESSION_ID": "abc", "CI": "true"}, false), |
| 124 | + expected: "cursor", |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "interactive terminal with no agent env vars returns empty", |
| 128 | + env: newMockEnv(map[string]string{}, true), |
| 129 | + expected: "", |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "CI env var in interactive terminal returns empty", |
| 133 | + env: newMockEnv(map[string]string{"CI": "true"}, true), |
| 134 | + expected: "", |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "first matching env var wins by priority order", |
| 138 | + env: newMockEnv(map[string]string{"CURSOR_SESSION_ID": "c", "CLAUDE_CODE": "1"}, false), |
| 139 | + expected: "cursor", |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "stdin is TTY but stdout is not still counts as interactive", |
| 143 | + env: mockEnvChecker{envVars: map[string]string{}, stdinTerminal: true, stdoutTerminal: false}, |
| 144 | + expected: "", |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "stdout is TTY but stdin is not still counts as interactive", |
| 148 | + env: mockEnvChecker{envVars: map[string]string{}, stdinTerminal: false, stdoutTerminal: true}, |
| 149 | + expected: "", |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + for _, tt := range tests { |
| 154 | + t.Run(tt.name, func(t *testing.T) { |
| 155 | + result := detectAgentContext(tt.env) |
| 156 | + assert.Equal(t, tt.expected, result) |
| 157 | + }) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestCmdRunEventProperties(t *testing.T) { |
| 162 | + t.Run("does not set agent_context itself", func(t *testing.T) { |
| 163 | + cmd := &cobra.Command{Use: "test"} |
| 164 | + cmd.SetArgs([]string{}) |
| 165 | + _ = cmd.Execute() |
| 166 | + |
| 167 | + props := CmdRunEventProperties(cmd, "test-resource", nil) |
| 168 | + |
| 169 | + _, hasAgentCtx := props["agent_context"] |
| 170 | + assert.False(t, hasAgentCtx, "agent_context is injected by sendEvent, not CmdRunEventProperties") |
| 171 | + }) |
| 172 | + |
| 173 | + t.Run("overrides can still set agent_context", func(t *testing.T) { |
| 174 | + cmd := &cobra.Command{Use: "test"} |
| 175 | + cmd.SetArgs([]string{}) |
| 176 | + _ = cmd.Execute() |
| 177 | + |
| 178 | + overrides := map[string]interface{}{"agent_context": "explicit:test-agent"} |
| 179 | + props := CmdRunEventProperties(cmd, "test-resource", overrides) |
| 180 | + |
| 181 | + assert.Equal(t, "explicit:test-agent", props["agent_context"]) |
| 182 | + }) |
| 183 | + |
| 184 | + t.Run("returns expected base properties", func(t *testing.T) { |
| 185 | + cmd := &cobra.Command{Use: "test"} |
| 186 | + cmd.SetArgs([]string{}) |
| 187 | + _ = cmd.Execute() |
| 188 | + |
| 189 | + props := CmdRunEventProperties(cmd, "flags", nil) |
| 190 | + |
| 191 | + assert.Equal(t, "flags", props["name"]) |
| 192 | + assert.Contains(t, props, "action") |
| 193 | + assert.Contains(t, props, "flags") |
| 194 | + }) |
| 195 | +} |
0 commit comments