|
| 1 | +"""Tests for the agent detection module.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from runpod import agent |
| 8 | + |
| 9 | + |
| 10 | +def _clean_env(): |
| 11 | + """Returns an environment dict with every known agent signal removed.""" |
| 12 | + env = dict(os.environ) |
| 13 | + for key in agent.known_env_vars(): |
| 14 | + env.pop(key, None) |
| 15 | + return env |
| 16 | + |
| 17 | + |
| 18 | +class TestDetect(unittest.TestCase): |
| 19 | + """Test the agent.detect function.""" |
| 20 | + |
| 21 | + def test_no_agent(self): |
| 22 | + """No agent env vars set means no detection.""" |
| 23 | + with patch.dict(os.environ, _clean_env(), clear=True): |
| 24 | + self.assertEqual(agent.detect(), "") |
| 25 | + self.assertEqual(agent.suffix(), "") |
| 26 | + |
| 27 | + def test_claude_code(self): |
| 28 | + """CLAUDECODE detects claude-code.""" |
| 29 | + env = _clean_env() |
| 30 | + env["CLAUDECODE"] = "1" |
| 31 | + with patch.dict(os.environ, env, clear=True): |
| 32 | + self.assertEqual(agent.detect(), "claude-code") |
| 33 | + self.assertEqual(agent.suffix(), " (via claude-code)") |
| 34 | + |
| 35 | + def test_claude_code_alternate_var(self): |
| 36 | + """CLAUDE_CODE also detects claude-code.""" |
| 37 | + env = _clean_env() |
| 38 | + env["CLAUDE_CODE"] = "1" |
| 39 | + with patch.dict(os.environ, env, clear=True): |
| 40 | + self.assertEqual(agent.detect(), "claude-code") |
| 41 | + |
| 42 | + def test_cursor(self): |
| 43 | + """CURSOR_TRACE_ID detects cursor.""" |
| 44 | + env = _clean_env() |
| 45 | + env["CURSOR_TRACE_ID"] = "abc123" |
| 46 | + with patch.dict(os.environ, env, clear=True): |
| 47 | + self.assertEqual(agent.detect(), "cursor") |
| 48 | + |
| 49 | + def test_cursor_cli_before_cursor(self): |
| 50 | + """cursor-cli is more specific and wins over cursor when both are set.""" |
| 51 | + env = _clean_env() |
| 52 | + env["CURSOR_AGENT"] = "1" |
| 53 | + env["CURSOR_TRACE_ID"] = "abc123" |
| 54 | + with patch.dict(os.environ, env, clear=True): |
| 55 | + self.assertEqual(agent.detect(), "cursor-cli") |
| 56 | + |
| 57 | + def test_cowork_before_claude_code(self): |
| 58 | + """cowork is more specific and wins over claude-code when both are set.""" |
| 59 | + env = _clean_env() |
| 60 | + env["CLAUDE_CODE_IS_COWORK"] = "1" |
| 61 | + env["CLAUDECODE"] = "1" |
| 62 | + with patch.dict(os.environ, env, clear=True): |
| 63 | + self.assertEqual(agent.detect(), "cowork") |
| 64 | + |
| 65 | + def test_codex(self): |
| 66 | + """A Codex marker detects codex.""" |
| 67 | + env = _clean_env() |
| 68 | + env["CODEX_THREAD_ID"] = "t-1" |
| 69 | + with patch.dict(os.environ, env, clear=True): |
| 70 | + self.assertEqual(agent.detect(), "codex") |
| 71 | + |
| 72 | + def test_gemini_cli(self): |
| 73 | + """GEMINI_CLI detects gemini-cli.""" |
| 74 | + env = _clean_env() |
| 75 | + env["GEMINI_CLI"] = "1" |
| 76 | + with patch.dict(os.environ, env, clear=True): |
| 77 | + self.assertEqual(agent.detect(), "gemini-cli") |
| 78 | + |
| 79 | + def test_empty_value_not_detected(self): |
| 80 | + """An env var set to empty string does not count as detection.""" |
| 81 | + env = _clean_env() |
| 82 | + env["CLAUDECODE"] = "" |
| 83 | + with patch.dict(os.environ, env, clear=True): |
| 84 | + self.assertEqual(agent.detect(), "") |
| 85 | + |
| 86 | + def test_whitespace_value_not_detected(self): |
| 87 | + """An env var set to whitespace only does not count as detection.""" |
| 88 | + env = _clean_env() |
| 89 | + env["CLAUDECODE"] = " " |
| 90 | + with patch.dict(os.environ, env, clear=True): |
| 91 | + self.assertEqual(agent.detect(), "") |
| 92 | + |
| 93 | + def test_ai_agent_generic_fallback(self): |
| 94 | + """The generic AI_AGENT signal is used when no harness matches.""" |
| 95 | + env = _clean_env() |
| 96 | + env["AI_AGENT"] = "my-custom-agent" |
| 97 | + with patch.dict(os.environ, env, clear=True): |
| 98 | + self.assertEqual(agent.detect(), "my-custom-agent") |
| 99 | + |
| 100 | + def test_harness_wins_over_ai_agent(self): |
| 101 | + """A specific harness marker takes priority over the generic AI_AGENT.""" |
| 102 | + env = _clean_env() |
| 103 | + env["AI_AGENT"] = "my-custom-agent" |
| 104 | + env["CLAUDECODE"] = "1" |
| 105 | + with patch.dict(os.environ, env, clear=True): |
| 106 | + self.assertEqual(agent.detect(), "claude-code") |
| 107 | + |
| 108 | + def test_ai_agent_sanitized(self): |
| 109 | + """AI_AGENT values are sanitized to User-Agent-safe characters.""" |
| 110 | + env = _clean_env() |
| 111 | + env["AI_AGENT"] = "bad value (with) chars!" |
| 112 | + with patch.dict(os.environ, env, clear=True): |
| 113 | + self.assertEqual(agent.detect(), "badvaluewithchars") |
| 114 | + |
| 115 | + def test_ai_agent_length_capped(self): |
| 116 | + """AI_AGENT values are capped at 64 characters.""" |
| 117 | + env = _clean_env() |
| 118 | + env["AI_AGENT"] = "a" * 200 |
| 119 | + with patch.dict(os.environ, env, clear=True): |
| 120 | + self.assertEqual(agent.detect(), "a" * 64) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + unittest.main() |
0 commit comments