|
| 1 | +"""Unit tests for MCPClientAdapter._should_skip_env_prompts.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from apm_cli.adapters.client.base import MCPClientAdapter |
| 8 | + |
| 9 | + |
| 10 | +class TestShouldSkipEnvPrompts(unittest.TestCase): |
| 11 | + """Verify the three-branch TTY/CI/managed-mode policy.""" |
| 12 | + |
| 13 | + def test_returns_true_when_env_overrides_provided(self): |
| 14 | + """Managed mode: caller already collected env vars.""" |
| 15 | + self.assertTrue(MCPClientAdapter._should_skip_env_prompts({"TOKEN": "val"})) |
| 16 | + |
| 17 | + @patch.dict(os.environ, {"APM_E2E_TESTS": "1"}) |
| 18 | + def test_returns_true_when_e2e_tests_flag_set(self): |
| 19 | + """CI mode: APM_E2E_TESTS=1 disables prompts.""" |
| 20 | + self.assertTrue(MCPClientAdapter._should_skip_env_prompts({})) |
| 21 | + |
| 22 | + @patch.dict(os.environ, {}, clear=True) |
| 23 | + def test_returns_true_when_stdin_not_tty(self): |
| 24 | + """Non-interactive: stdin is not a TTY.""" |
| 25 | + with patch("sys.stdin") as mock_stdin, patch("sys.stdout") as mock_stdout: |
| 26 | + mock_stdin.isatty.return_value = False |
| 27 | + mock_stdout.isatty.return_value = True |
| 28 | + self.assertTrue(MCPClientAdapter._should_skip_env_prompts({})) |
| 29 | + |
| 30 | + @patch.dict(os.environ, {}, clear=True) |
| 31 | + def test_returns_true_when_stdout_not_tty(self): |
| 32 | + """Non-interactive: stdout is not a TTY.""" |
| 33 | + with patch("sys.stdin") as mock_stdin, patch("sys.stdout") as mock_stdout: |
| 34 | + mock_stdin.isatty.return_value = True |
| 35 | + mock_stdout.isatty.return_value = False |
| 36 | + self.assertTrue(MCPClientAdapter._should_skip_env_prompts({})) |
| 37 | + |
| 38 | + @patch.dict(os.environ, {}, clear=True) |
| 39 | + def test_returns_false_when_interactive_tty(self): |
| 40 | + """Interactive: both stdin and stdout are TTYs, no overrides, no CI flag.""" |
| 41 | + with patch("sys.stdin") as mock_stdin, patch("sys.stdout") as mock_stdout: |
| 42 | + mock_stdin.isatty.return_value = True |
| 43 | + mock_stdout.isatty.return_value = True |
| 44 | + self.assertFalse(MCPClientAdapter._should_skip_env_prompts({})) |
| 45 | + |
| 46 | + def test_returns_true_with_empty_overrides_is_false(self): |
| 47 | + """Empty dict is falsy — should NOT skip on overrides alone.""" |
| 48 | + # With an empty dict, only TTY/CI determines the result. |
| 49 | + # This test just confirms {} is treated as "no overrides". |
| 50 | + with patch("sys.stdin") as mock_stdin, patch("sys.stdout") as mock_stdout: |
| 51 | + mock_stdin.isatty.return_value = True |
| 52 | + mock_stdout.isatty.return_value = True |
| 53 | + with patch.dict(os.environ, {}, clear=True): |
| 54 | + self.assertFalse(MCPClientAdapter._should_skip_env_prompts({})) |
0 commit comments