From cc806d2cc6d735097018482159a4e4daa2ccc38b Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 28 Feb 2026 19:24:02 -0500 Subject: [PATCH] refactor(tests): Add strict type hints to fifth batch of test files --- conductor/tests/diag_subagent.py | 2 +- conductor/tests/test_infrastructure.py | 2 +- tests/conftest.py | 7 ++++--- tests/test_agent_tools_wiring.py | 7 ++----- tests/test_gui2_events.py | 5 +++-- tests/test_gui2_mcp.py | 5 +++-- tests/test_gui2_performance.py | 2 +- tests/test_gui_async_events.py | 5 ++--- tests/test_gui_events.py | 6 +++--- 9 files changed, 20 insertions(+), 21 deletions(-) diff --git a/conductor/tests/diag_subagent.py b/conductor/tests/diag_subagent.py index 0efb948..9c709e8 100644 --- a/conductor/tests/diag_subagent.py +++ b/conductor/tests/diag_subagent.py @@ -2,7 +2,7 @@ import subprocess import sys import os -def run_diag(role, prompt): +def run_diag(role: str, prompt: str) -> str: print(f"--- Running Diag for {role} ---") cmd = [sys.executable, "scripts/mma_exec.py", "--role", role, prompt] try: diff --git a/conductor/tests/test_infrastructure.py b/conductor/tests/test_infrastructure.py index e8094bd..e558fad 100644 --- a/conductor/tests/test_infrastructure.py +++ b/conductor/tests/test_infrastructure.py @@ -2,7 +2,7 @@ import subprocess import pytest import os -def run_ps_script(role, prompt): +def run_ps_script(role: str, prompt: str) -> subprocess.CompletedProcess: """Helper to run the run_subagent.ps1 script.""" # Using -File is safer and handles arguments better cmd = [ diff --git a/tests/conftest.py b/tests/conftest.py index 849f3b8..a799c1a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ import requests import os import signal import sys +from typing import Generator import os # Ensure project root is in path @@ -14,14 +15,14 @@ from api_hook_client import ApiHookClient import ai_client @pytest.fixture(autouse=True) -def reset_ai_client() -> None: +def reset_ai_client() -> Generator[None, None, None]: """Reset ai_client global state between every test to prevent state pollution.""" ai_client.reset_session() # Default to a safe model ai_client.set_provider("gemini", "gemini-2.5-flash-lite") yield -def kill_process_tree(pid): +def kill_process_tree(pid: int | None) -> None: """Robustly kills a process and all its children.""" if pid is None: return @@ -41,7 +42,7 @@ def kill_process_tree(pid): print(f"[Fixture] Error killing process tree {pid}: {e}") @pytest.fixture(scope="session") -def live_gui() -> None: +def live_gui() -> Generator[tuple[subprocess.Popen, str], None, None]: """ Session-scoped fixture that starts gui_2.py with --enable-test-hooks. """ diff --git a/tests/test_agent_tools_wiring.py b/tests/test_agent_tools_wiring.py index 2cea07b..61537fc 100644 --- a/tests/test_agent_tools_wiring.py +++ b/tests/test_agent_tools_wiring.py @@ -8,14 +8,11 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from ai_client import set_agent_tools, _build_anthropic_tools -def test_set_agent_tools(): -# Correct usage: pass a dict +def test_set_agent_tools() -> None: agent_tools = {"read_file": True, "list_directory": False} set_agent_tools(agent_tools) -def test_build_anthropic_tools_conversion(): -# _build_anthropic_tools takes no arguments and uses the global _agent_tools -# We set a tool to True and check if it appears in the output +def test_build_anthropic_tools_conversion() -> None: set_agent_tools({"read_file": True}) anthropic_tools = _build_anthropic_tools() tool_names = [t["name"] for t in anthropic_tools] diff --git a/tests/test_gui2_events.py b/tests/test_gui2_events.py index 5e386ea..c613e95 100644 --- a/tests/test_gui2_events.py +++ b/tests/test_gui2_events.py @@ -1,11 +1,12 @@ import pytest from unittest.mock import MagicMock, patch +from typing import Generator from gui_2 import App import ai_client from events import EventEmitter @pytest.fixture -def app_instance() -> None: +def app_instance() -> Generator[type[App], None, None]: """ Fixture to create an instance of the gui_2.App class for testing. It mocks functions that would render a window or block execution. @@ -25,7 +26,7 @@ def app_instance() -> None: ): yield App -def test_app_subscribes_to_events(app_instance): +def test_app_subscribes_to_events(app_instance: type[App]) -> None: """ This test checks that the App's __init__ method subscribes the necessary event handlers to the ai_client.events emitter. diff --git a/tests/test_gui2_mcp.py b/tests/test_gui2_mcp.py index cdf719d..fe51db8 100644 --- a/tests/test_gui2_mcp.py +++ b/tests/test_gui2_mcp.py @@ -1,11 +1,12 @@ import pytest from unittest.mock import patch, MagicMock +from typing import Generator from gui_2 import App import ai_client from events import EventEmitter @pytest.fixture -def app_instance() -> None: +def app_instance() -> Generator[App, None, None]: if not hasattr(ai_client, 'events') or ai_client.events is None: ai_client.events = EventEmitter() with ( @@ -21,7 +22,7 @@ def app_instance() -> None: ): yield App() -def test_mcp_tool_call_is_dispatched(app_instance): +def test_mcp_tool_call_is_dispatched(app_instance: App) -> None: """ This test verifies that when the AI returns a tool call for an MCP function, the ai_client correctly dispatches it to mcp_client. diff --git a/tests/test_gui2_performance.py b/tests/test_gui2_performance.py index 235bba4..6bde01a 100644 --- a/tests/test_gui2_performance.py +++ b/tests/test_gui2_performance.py @@ -11,7 +11,7 @@ from api_hook_client import ApiHookClient # Session-wide storage for comparing metrics across parameterized fixture runs _shared_metrics = {} -def test_performance_benchmarking(live_gui): +def test_performance_benchmarking(live_gui: tuple) -> None: """ Collects performance metrics for the current GUI script (parameterized as gui.py and gui_2.py). """ diff --git a/tests/test_gui_async_events.py b/tests/test_gui_async_events.py index 53da500..d964003 100644 --- a/tests/test_gui_async_events.py +++ b/tests/test_gui_async_events.py @@ -5,7 +5,7 @@ from gui_2 import App from events import UserRequestEvent @pytest.fixture -def mock_gui(): +def mock_gui() -> App: with ( patch('gui_2.load_config', return_value={ "ai": {"provider": "gemini", "model": "model-1"}, @@ -22,8 +22,7 @@ def mock_gui(): gui = App() return gui -def test_handle_generate_send_pushes_event(mock_gui): -# Mock _do_generate to return sample data +def test_handle_generate_send_pushes_event(mock_gui: App) -> None: mock_gui._do_generate = MagicMock(return_value=( "full_md", "path", [], "stable_md", "disc_text" )) diff --git a/tests/test_gui_events.py b/tests/test_gui_events.py index 06d9892..861ad1c 100644 --- a/tests/test_gui_events.py +++ b/tests/test_gui_events.py @@ -1,13 +1,14 @@ import pytest from unittest.mock import MagicMock, patch +from typing import Generator import dearpygui.dearpygui as dpg import gui_legacy from gui_legacy import App import ai_client @pytest.fixture -def app_instance() -> None: +def app_instance() -> Generator[App, None, None]: """ Fixture to create an instance of the App class for testing. It creates a real DPG context but mocks functions that would @@ -33,8 +34,7 @@ def app_instance() -> None: yield app dpg.destroy_context() -def test_gui_updates_on_event(app_instance): -# Patch dependencies for the test +def test_gui_updates_on_event(app_instance: App) -> None: with patch('dearpygui.dearpygui.set_value') as mock_set_value, \ patch('dearpygui.dearpygui.does_item_exist', return_value=True), \ patch('dearpygui.dearpygui.configure_item'), \