import pytest
from unittest.mock import patch, MagicMock
from preview_utils import generate_stl_preview

def test_generate_stl_preview_success():
    with patch("stl.mesh.Mesh.from_file") as mock_from_file, \
         patch("matplotlib.pyplot.figure") as mock_figure, \
         patch("matplotlib.pyplot.savefig") as mock_savefig, \
         patch("matplotlib.pyplot.close") as mock_close:
        
        # Setup mocks
        mock_mesh = MagicMock()
        mock_mesh.vectors = []
        mock_mesh.points.flatten.return_value = []
        mock_from_file.return_value = mock_mesh
        
        mock_fig_instance = MagicMock()
        mock_figure.return_value = mock_fig_instance
        
        result = generate_stl_preview("test.stl", "output.png")
        
        assert result is True
        mock_from_file.assert_called_once_with("test.stl")
        mock_savefig.assert_called_once()
        mock_close.assert_called_once()

def test_generate_stl_preview_failure():
    with patch("stl.mesh.Mesh.from_file", side_effect=Exception("File error")):
        result = generate_stl_preview("test.stl", "output.png")
        assert result is False
