import pytest from unittest.mock import MagicMock, patch from pathlib import Path from src.app_controller import AppController from src.events import UserRequestEvent @pytest.fixture def controller(): with ( patch('src.models.load_config', return_value={ "ai": {"provider": "gemini", "model": "model-1"}, "projects": {"paths": [], "active": ""}, "gui": {"show_windows": {}} }), patch('src.project_manager.load_project', return_value={}), patch('src.project_manager.migrate_from_legacy_config', return_value={}), patch('src.project_manager.save_project'), patch('src.session_logger.open_session'), patch('src.app_controller.AppController._init_ai_and_hooks'), patch('src.app_controller.AppController._fetch_models') ): c = AppController() # Mock necessary state c.ui_files_base_dir = "." c.event_queue = MagicMock() return c def test_handle_generate_send_appends_definitions(controller): # Setup file_items = [{"path": "src/models.py", "entry": "src/models.py"}] controller._do_generate = MagicMock(return_value=( "full_md", Path("output.md"), file_items, "stable_md", "disc_text" )) controller.ui_ai_input = "Explain @Track object" # Mock symbol helpers with ( patch('src.app_controller.parse_symbols', return_value=["Track"]) as mock_parse, patch('src.app_controller.get_symbol_definition', return_value=("src/models.py", "class Track: pass")) as mock_get_def, patch('threading.Thread') as mock_thread ): # Execute controller._handle_generate_send() # Run worker manually worker = mock_thread.call_args[1]['target'] worker() # Verify mock_parse.assert_called_once_with("Explain @Track object") mock_get_def.assert_called_once() controller.event_queue.put.assert_called_once() event_name, event_payload = controller.event_queue.put.call_args[0] assert event_name == "user_request" assert isinstance(event_payload, UserRequestEvent) # Check if definition was appended expected_suffix = "\n\n[Definition: Track from src/models.py]\n```python\nclass Track: pass\n```" assert event_payload.prompt == "Explain @Track object" + expected_suffix def test_handle_generate_send_no_symbols(controller): # Setup file_items = [{"path": "src/models.py", "entry": "src/models.py"}] controller._do_generate = MagicMock(return_value=( "full_md", Path("output.md"), file_items, "stable_md", "disc_text" )) controller.ui_ai_input = "Just a normal prompt" with ( patch('src.app_controller.parse_symbols', return_value=[]) as mock_parse, patch('threading.Thread') as mock_thread ): # Execute controller._handle_generate_send() # Run worker manually worker = mock_thread.call_args[1]['target'] worker() # Verify controller.event_queue.put.assert_called_once() _, event_payload = controller.event_queue.put.call_args[0] assert event_payload.prompt == "Just a normal prompt"