import os import types import explorer def test_load_positions_empty_sets_diagnostics(monkeypatch): # Monkeypatch load_positions to return empty positions monkeypatch.setattr( explorer, "load_positions", lambda db_path, window_size: ({}, {}) ) monkeypatch.setenv("EXPLORER_DEBUG_TRAJECTORIES", "1") # Call build_trajectories_tab; it should set diagnostics and return without exception explorer.build_trajectories_tab(db_path="unused", window_size="annual") assert ( explorer._last_trajectories_diagnostics.get("stage") == "load_positions_empty" ) def test_select_helper_exception_is_captured(monkeypatch): # Provide a minimal non-empty positions_by_window positions = {"W1": {"mp1": (0.1, 0.2)}} def fake_load_positions(db_path, window_size): return positions, {} monkeypatch.setattr(explorer, "load_positions", fake_load_positions) # Ensure party_map maps the mp so centroids/path that invoke select_trajectory_plot_data monkeypatch.setattr(explorer, "load_party_map", lambda db_path: {"mp1": "P1"}) # Patch select_trajectory_plot_data to raise def bad_helper(*args, **kwargs): raise ValueError("boom") monkeypatch.setattr(explorer, "select_trajectory_plot_data", bad_helper) monkeypatch.setenv("EXPLORER_DEBUG_TRAJECTORIES", "1") explorer.build_trajectories_tab(db_path="unused", window_size="annual") # Ensure the helper function has diagnostics attached and module diagnostics updated assert getattr(explorer.select_trajectory_plot_data, "_last_diagnostics", None) assert "exception" in explorer.select_trajectory_plot_data._last_diagnostics assert ( explorer._last_trajectories_diagnostics.get("stage") == "select_helper_exception" ) assert "ValueError" in explorer._last_trajectories_diagnostics.get("exception", "")