|
| 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 | + ] |
| 21 | + ) |
| 22 | + def test_is_agent(self, agent, env_var, env_var_value): |
| 23 | + self.assertTrue(_is_agent(agent, {env_var: env_var_value})) |
| 24 | + |
| 25 | + @parameterized.expand( |
| 26 | + [ |
| 27 | + (Agent.ClaudeCode, "NOT_CLAUDECODE", "1"), |
| 28 | + (Agent.Codex, "NOT_CODEX_SANDBOX", "seatbelt"), |
| 29 | + (Agent.Cursor, "NOT_CURSOR_AGENT", "1"), |
| 30 | + # exact-name rule: GEMINI_CLI_HOME must not match Agent.GeminiCLI |
| 31 | + (Agent.GeminiCLI, "GEMINI_CLI_HOME", "1"), |
| 32 | + # shared-variable rule: a Lambda AWS_EXECUTION_ENV must not match Agent.Kiro |
| 33 | + (Agent.Kiro, "AWS_EXECUTION_ENV", "AWS_Lambda_python3.12"), |
| 34 | + # too-generic var: AGENT alone (no OPENCODE) must not match Agent.OpenCode |
| 35 | + (Agent.OpenCode, "AGENT", "1"), |
| 36 | + ] |
| 37 | + ) |
| 38 | + def test_is_not_agent(self, agent, env_var, env_var_value): |
| 39 | + self.assertFalse(_is_agent(agent, {env_var: env_var_value})) |
| 40 | + |
| 41 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"CLAUDECODE": "1"}, clear=True) |
| 42 | + def test_detector_identifies_claude_code(self): |
| 43 | + self.assertEqual(AgentDetector().agent(), Agent.ClaudeCode) |
| 44 | + |
| 45 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"CODEX_SANDBOX": "seatbelt"}, clear=True) |
| 46 | + def test_detector_identifies_codex(self): |
| 47 | + self.assertEqual(AgentDetector().agent(), Agent.Codex) |
| 48 | + |
| 49 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"CURSOR_AGENT": "1"}, clear=True) |
| 50 | + def test_detector_identifies_cursor(self): |
| 51 | + self.assertEqual(AgentDetector().agent(), Agent.Cursor) |
| 52 | + |
| 53 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"GEMINI_CLI": "1"}, clear=True) |
| 54 | + def test_detector_identifies_gemini_cli(self): |
| 55 | + self.assertEqual(AgentDetector().agent(), Agent.GeminiCLI) |
| 56 | + |
| 57 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"TERM_PROGRAM": "kiro"}, clear=True) |
| 58 | + def test_detector_identifies_kiro_ide(self): |
| 59 | + self.assertEqual(AgentDetector().agent(), Agent.Kiro) |
| 60 | + |
| 61 | + @patch.dict( |
| 62 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 63 | + {"AWS_EXECUTION_ENV": "AmazonQ-For-CLI Version/1.13.3"}, |
| 64 | + clear=True, |
| 65 | + ) |
| 66 | + def test_detector_identifies_kiro_cli(self): |
| 67 | + self.assertEqual(AgentDetector().agent(), Agent.Kiro) |
| 68 | + |
| 69 | + @patch.dict( |
| 70 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 71 | + {"AWS_EXECUTION_ENV": "AWS_Lambda_python3.12"}, |
| 72 | + clear=True, |
| 73 | + ) |
| 74 | + def test_detector_does_not_identify_lambda_as_kiro(self): |
| 75 | + self.assertIsNone(AgentDetector().agent()) |
| 76 | + |
| 77 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {"OPENCODE": "1"}, clear=True) |
| 78 | + def test_detector_identifies_opencode(self): |
| 79 | + self.assertEqual(AgentDetector().agent(), Agent.OpenCode) |
| 80 | + |
| 81 | + @patch.dict( |
| 82 | + "samcli.lib.telemetry.agent_detector.os.environ", |
| 83 | + {"COPILOT_AGENT_SESSION_ID": "0b8f6e2c-1a3d-4c9e-8f7a-2b1c0d9e8f7a"}, |
| 84 | + clear=True, |
| 85 | + ) |
| 86 | + def test_detector_identifies_github_copilot(self): |
| 87 | + self.assertEqual(AgentDetector().agent(), Agent.GitHubCopilot) |
| 88 | + |
| 89 | + @patch.dict("samcli.lib.telemetry.agent_detector.os.environ", {}, clear=True) |
| 90 | + def test_detector_returns_none_when_no_agent(self): |
| 91 | + self.assertIsNone(AgentDetector().agent()) |
0 commit comments