refactor(tests): Add strict type hints to first batch of test files

This commit is contained in:
2026-02-28 19:06:50 -05:00
parent e8833b6656
commit f0415a40aa
10 changed files with 59 additions and 70 deletions

View File

@@ -8,8 +8,7 @@ from pathlib import Path
from fastapi.testclient import TestClient
class TestHeadlessAPI(unittest.TestCase):
def setUp(self):
# We need an App instance to initialize the API, but we want to avoid GUI stuff
def setUp(self) -> None:
with patch('gui_2.session_logger.open_session'), \
patch('gui_2.ai_client.set_provider'), \
patch('gui_2.session_logger.close_session'):
@@ -29,14 +28,12 @@ class TestHeadlessAPI(unittest.TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {"status": "ok"})
def test_status_endpoint_unauthorized(self):
# Ensure a key is required
def test_status_endpoint_unauthorized(self) -> None:
with patch.dict(self.app_instance.config, {"headless": {"api_key": "some-required-key"}}):
response = self.client.get("/status")
self.assertEqual(response.status_code, 403)
def test_status_endpoint_authorized(self):
# We'll use a test key
def test_status_endpoint_authorized(self) -> None:
headers = {"X-API-KEY": "test-secret-key"}
with patch.dict(self.app_instance.config, {"headless": {"api_key": "test-secret-key"}}):
response = self.client.get("/status", headers=headers)
@@ -63,8 +60,7 @@ class TestHeadlessAPI(unittest.TestCase):
self.assertIn("metadata", data)
self.assertEqual(data["usage"]["input_tokens"], 10)
def test_pending_actions_endpoint(self):
# Manually add a pending action
def test_pending_actions_endpoint(self) -> None:
with patch('gui_2.uuid.uuid4', return_value="test-action-id"):
dialog = gui_2.ConfirmDialog("dir", ".")
self.app_instance._pending_actions[dialog._uid] = dialog
@@ -74,8 +70,7 @@ class TestHeadlessAPI(unittest.TestCase):
self.assertEqual(len(data), 1)
self.assertEqual(data[0]["action_id"], "test-action-id")
def test_confirm_action_endpoint(self):
# Manually add a pending action
def test_confirm_action_endpoint(self) -> None:
with patch('gui_2.uuid.uuid4', return_value="test-confirm-id"):
dialog = gui_2.ConfirmDialog("dir", ".")
self.app_instance._pending_actions[dialog._uid] = dialog
@@ -85,8 +80,7 @@ class TestHeadlessAPI(unittest.TestCase):
self.assertTrue(dialog._done)
self.assertTrue(dialog._approved)
def test_list_sessions_endpoint(self):
# Ensure logs directory exists
def test_list_sessions_endpoint(self) -> None:
Path("logs").mkdir(exist_ok=True)
# Create a dummy log
dummy_log = Path("logs/test_session_api.log")
@@ -108,8 +102,7 @@ class TestHeadlessAPI(unittest.TestCase):
self.assertIn("screenshots", data)
self.assertIn("files_base_dir", data)
def test_endpoint_no_api_key_configured(self):
# Test the security fix specifically
def test_endpoint_no_api_key_configured(self) -> None:
with patch.dict(self.app_instance.config, {"headless": {"api_key": ""}}):
response = self.client.get("/status", headers=self.headers)
self.assertEqual(response.status_code, 403)
@@ -122,8 +115,7 @@ class TestHeadlessStartup(unittest.TestCase):
@patch('gui_2.save_config')
@patch('gui_2.ai_client.cleanup')
@patch('uvicorn.run') # Mock uvicorn.run to prevent hanging
def test_headless_flag_prevents_gui_run(self, mock_uvicorn_run, mock_cleanup, mock_save_config, mock_hook_server, mock_immapp_run):
# Setup mock argv with --headless
def test_headless_flag_prevents_gui_run(self, mock_uvicorn_run: MagicMock, mock_cleanup: MagicMock, mock_save_config: MagicMock, mock_hook_server: MagicMock, mock_immapp_run: MagicMock) -> None:
test_args = ["gui_2.py", "--headless"]
with patch.object(sys, 'argv', test_args):
with patch('gui_2.session_logger.close_session'), \
@@ -138,7 +130,7 @@ class TestHeadlessStartup(unittest.TestCase):
mock_uvicorn_run.assert_called_once()
@patch('gui_2.immapp.run')
def test_normal_startup_calls_gui_run(self, mock_immapp_run):
def test_normal_startup_calls_gui_run(self, mock_immapp_run: MagicMock) -> None:
test_args = ["gui_2.py"]
with patch.object(sys, 'argv', test_args):
# In normal mode, it should still call immapp.run