"""High-value tests for benchmarks/metrics/visualization.py. Covers matplotlib unavailable path, figure creation, file saving, Pareto frontier edge cases, and parameter importance sorting. """ import pytest from unittest.mock import patch class TestPlotOptimizationHistory: """Tests for plot_optimization_history.""" def test_returns_none_when_matplotlib_unavailable(self): """Returns None when MATPLOTLIB_AVAILABLE is False.""" with patch( "local_deep_research.benchmarks.metrics.visualization.MATPLOTLIB_AVAILABLE", False, ): from local_deep_research.benchmarks.metrics.visualization import ( plot_optimization_history, ) result = plot_optimization_history([0.5, 0.6], [0.5, 0.6]) assert result is None def test_returns_figure_with_valid_data(self): """Returns a matplotlib Figure with valid inputs.""" from local_deep_research.benchmarks.metrics.visualization import ( plot_optimization_history, MATPLOTLIB_AVAILABLE, ) if not MATPLOTLIB_AVAILABLE: pytest.skip("matplotlib not installed") fig = plot_optimization_history([0.3, 0.5, 0.7], [0.3, 0.5, 0.7]) assert fig is not None import matplotlib.pyplot as plt plt.close(fig) def test_saves_to_file_when_output_file_given(self, tmp_path): """Saves figure to disk when output_file is provided.""" from local_deep_research.benchmarks.metrics.visualization import ( plot_optimization_history, MATPLOTLIB_AVAILABLE, ) if not MATPLOTLIB_AVAILABLE: pytest.skip("matplotlib not installed") out = str(tmp_path / "history.png") fig = plot_optimization_history([0.1, 0.2], [0.1, 0.2], output_file=out) assert fig is not None assert (tmp_path / "history.png").exists() import matplotlib.pyplot as plt plt.close(fig) def test_custom_title(self): """Custom title is set on the axes.""" from local_deep_research.benchmarks.metrics.visualization import ( plot_optimization_history, MATPLOTLIB_AVAILABLE, ) if not MATPLOTLIB_AVAILABLE: pytest.skip("matplotlib not installed") fig = plot_optimization_history([0.5], [0.5], title="Custom Title") assert fig is not None ax = fig.get_axes()[0] assert ax.get_title() == "Custom Title" import matplotlib.pyplot as plt plt.close(fig) def test_single_trial(self): """Works with a single trial value.""" from local_deep_research.benchmarks.metrics.visualization import ( plot_optimization_history, MATPLOTLIB_AVAILABLE, ) if not MATPLOTLIB_AVAILABLE: pytest.skip("matplotlib not installed") fig = plot_optimization_history([0.9], [0.9]) assert fig is not None import matplotlib.pyplot as plt plt.close(fig)