diff --git a/src/app_controller.py b/src/app_controller.py index 6d6159ff..9d5b3218 100644 --- a/src/app_controller.py +++ b/src/app_controller.py @@ -790,7 +790,8 @@ class AppController: self._api_event_queue_lock: threading.Lock = threading.Lock() self._rag_engine_lock: threading.Lock = threading.Lock() self._project_switch_lock: threading.Lock = threading.Lock() - + self._project_switch_in_progress: bool = False + self._project_switch_pending_path: Optional[str] = None # --- Shared background pool + proactive warmup (startup_speedup_20260606) --- self._io_pool = make_io_pool() _install_sigint_exit_handler(self) @@ -2841,10 +2842,9 @@ class AppController: def is_project_stale(self) -> bool: """True when a project switch is queued or running; UI should tint to signal the controller state lags the user's last click.""" - with self._project_switch_lock: - if self._project_switch_in_progress: - return True - pending = self._project_switch_pending_path + if getattr(self, "_project_switch_in_progress", False): + return True + pending = getattr(self, "_project_switch_pending_path", None) if pending and pending != self.active_project_path: return True return False diff --git a/tests/test_gui_events_v2.py b/tests/test_gui_events_v2.py index bc59510b..5503bdf0 100644 --- a/tests/test_gui_events_v2.py +++ b/tests/test_gui_events_v2.py @@ -22,7 +22,6 @@ def mock_gui() -> App: gui = App() return gui -@pytest.mark.skip(reason="Pre-existing test bug: patches 'threading.Thread' but production code uses self._io_pool.submit_io() (see src/app_controller.py:_handle_generate_send). Test needs to patch the io_pool, not threading.Thread. Tracked as pre-existing.") def test_handle_generate_send_pushes_event(mock_gui: App) -> None: mock_gui.controller._do_generate = MagicMock(return_value=( "full_md", "path", [], "stable_md", "disc_text" @@ -33,14 +32,16 @@ def test_handle_generate_send_pushes_event(mock_gui: App) -> None: mock_gui.controller.event_queue.put = MagicMock() # No need to mock asyncio.run_coroutine_threadsafe now, it's a standard thread - with patch('threading.Thread') as mock_thread: + # Production code: self._io_pool.submit_io(worker) (see + # src/app_controller.py:_handle_generate_send). Patch submit_io + # to capture the worker function instead of the io_pool's + # background thread, so we can run the worker synchronously. + with patch.object(mock_gui.controller, 'submit_io') as mock_submit: + # Verify a job was submitted to the io_pool mock_gui.controller._handle_generate_send() - # Verify thread was started - assert mock_thread.called - # To test the worker logic inside, we'd need to invoke the target function - # But the controller logic itself now just starts a thread. - # Let's extract the worker and run it. - target_worker = mock_thread.call_args[1]['target'] + mock_gui.controller._handle_generate_send() + print(f"[DBG] mock_submit.called: {mock_submit.called}", file=__import__('sys').stderr) + target_worker = mock_submit.call_args[0][0] target_worker() # Verify the call to event_queue.put occurred.