|
| 1 | +from unittest import TestCase |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from parameterized import parameterized |
| 5 | + |
| 6 | +from samcli.lib.telemetry.agent_detector import Agent, AgentDetector, _is_agent |
| 7 | + |
| 8 | + |
| 9 | +class TestAgentDetector(TestCase): |
| 10 | + @parameterized.expand( |
| 11 | + [ |
| 12 | + (Agent.ClaudeCode, "CLAUDECODE", "1"), |
| 13 | + (Agent.Codex, "CODEX_SANDBOX", "seatbelt"), |
| 14 | + (Agent.Cursor, "CURSOR_AGENT", "1"), |
| 15 | + (Agent.GeminiCLI, "GEMINI_CLI", "1"), |
| 16 | + (Agent.Kiro, "TERM_PROGRAM", "kiro"), |
| 17 | + (Agent.Kiro, "AWS_EXECUTION_ENV", "AmazonQ-For-CLI Version/1.13.3"), |
| 18 | + (Agent.OpenCode, "OPENCODE", "1"), |
| 19 | + (Agent.GitHubCopilot, "COPILOT_AGENT_SESSION_ID", "0b8f6e2c-1a3d-4c9e-8f7a-2b1c0d9e8f7a"), |
| 20 | + (Agent.GitHubCopilot, "GITHUB_COPILOT_CLI_MODE", "true"), |
| 21 | + ] |
| 22 | + ) |
| 23 | + def test_is_agent(self, agent, env_var, env_var_value): |
| 24 | + self.assertTrue(_is_agent(agent, {env_var: env_var_value})) |
| 25 | + |
| 26 | + @parameterized.expand( |
| 27 | + [ |
| 28 | + (Agent.ClaudeCode, "NOT_CLAUDECODE", "1"), |
| 29 | + (Agent.Codex, "NOT_CODEX_SANDBOX", "seatbelt"), |
| 30 | + (Agent.Cursor, "NOT_CURSOR_AGENT", "1"), |
| 31 | + # exact-name rule: GEMINI_CLI_HOME must not match Agent.GeminiCLI |
| 32 | + (Agent.GeminiCLI, "GEMINI_CLI_HOME", "1"), |
| 33 | + # shared-variable rule: a Lambda AWS_EXECUTION_ENV must not match Agent.Kiro |
| 34 | + (Agent.Kiro, "AWS_EXECUTION_ENV", "AWS_Lambda_python3.12"), |
| 35 | + # too-generic var: AGENT alone (no OPENCODE) must not match Agent.OpenCode |
| 36 | + (Agent.OpenCode, "AGENT", "1"), |
| 37 | + # CI-collision guard: plain GitHub Actions CI must not match Agent.GitHubCopilot |
| 38 | + (Agent.GitHubCopilot, "GITHUB_ACTION", "run1"), |
| 39 | + ] |
| 40 | + ) |
| 41 | + def test_is_not_agent(self, agent, env_var, env_var_value): |
| 42 | + self.assertFalse(_is_agent(agent, {env_var: env_var_value})) |
| 43 | + |
| 44 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"CLAUDECODE": "1"}, clear=True) |
| 45 | + def test_detector_identifies_claude_code(self): |
| 46 | + self.assertEqual(AgentDetector().agent(), Agent.ClaudeCode) |
| 47 | + |
| 48 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"CODEX_SANDBOX": "seatbelt"}, clear=True) |
| 49 | + def test_detector_identifies_codex(self): |
| 50 | + self.assertEqual(AgentDetector().agent(), Agent.Codex) |
| 51 | + |
| 52 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"CURSOR_AGENT": "1"}, clear=True) |
| 53 | + def test_detector_identifies_cursor(self): |
| 54 | + self.assertEqual(AgentDetector().agent(), Agent.Cursor) |
| 55 | + |
| 56 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"GEMINI_CLI": "1"}, clear=True) |
| 57 | + def test_detector_identifies_gemini_cli(self): |
| 58 | + self.assertEqual(AgentDetector().agent(), Agent.GeminiCLI) |
| 59 | + |
| 60 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"TERM_PROGRAM": "kiro"}, clear=True) |
| 61 | + def test_detector_identifies_kiro_ide(self): |
| 62 | + self.assertEqual(AgentDetector().agent(), Agent.Kiro) |
| 63 | + |
| 64 | + @patch.dict( |
| 65 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 66 | + {"AWS_EXECUTION_ENV": "AmazonQ-For-CLI Version/1.13.3"}, |
| 67 | + clear=True, |
| 68 | + ) |
| 69 | + def test_detector_identifies_kiro_cli(self): |
| 70 | + self.assertEqual(AgentDetector().agent(), Agent.Kiro) |
| 71 | + |
| 72 | + @patch.dict( |
| 73 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 74 | + {"AWS_EXECUTION_ENV": "AWS_Lambda_python3.12"}, |
| 75 | + clear=True, |
| 76 | + ) |
| 77 | + def test_detector_does_not_identify_lambda_as_kiro(self): |
| 78 | + self.assertIsNone(AgentDetector().agent()) |
| 79 | + |
| 80 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"OPENCODE": "1"}, clear=True) |
| 81 | + def test_detector_identifies_opencode(self): |
| 82 | + self.assertEqual(AgentDetector().agent(), Agent.OpenCode) |
| 83 | + |
| 84 | + @patch.dict( |
| 85 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 86 | + {"COPILOT_AGENT_SESSION_ID": "0b8f6e2c-1a3d-4c9e-8f7a-2b1c0d9e8f7a"}, |
| 87 | + clear=True, |
| 88 | + ) |
| 89 | + def test_detector_identifies_github_copilot(self): |
| 90 | + self.assertEqual(AgentDetector().agent(), Agent.GitHubCopilot) |
| 91 | + |
| 92 | + @patch.dict( |
| 93 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 94 | + {"GITHUB_COPILOT_CLI_MODE": "true"}, |
| 95 | + clear=True, |
| 96 | + ) |
| 97 | + def test_detector_identifies_github_copilot_via_cli_mode(self): |
| 98 | + self.assertEqual(AgentDetector().agent(), Agent.GitHubCopilot) |
| 99 | + |
| 100 | + @patch.dict( |
| 101 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 102 | + {"GITHUB_ACTION": "run1"}, |
| 103 | + clear=True, |
| 104 | + ) |
| 105 | + def test_detector_does_not_identify_github_actions_as_copilot(self): |
| 106 | + # Plain GitHub Actions CI (no Copilot session var) must never be attributed to |
| 107 | + # GitHub Copilot; agent detection returns None so cicd.py can own GITHUB_ACTION. |
| 108 | + self.assertIsNone(AgentDetector().agent()) |
| 109 | + |
| 110 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {}, clear=True) |
| 111 | + def test_detector_returns_none_when_no_agent(self): |
| 112 | + self.assertIsNone(AgentDetector().agent()) |
0 commit comments