"""Unit tests for ffmpeg health check in fastapi_app.py."""

import asyncio
from unittest.mock import MagicMock, patch

import pytest


class TestFfmpegHealthCheck:
    """Test ffmpeg health check warns when not in PATH."""

    @pytest.mark.asyncio
    async def test_ffmpeg_missing_warns(self):
        """Should log warning when ffmpeg not found in PATH."""
        with patch("shutil.which", return_value=None):
            with patch("src.server.fastapi_app.setup_logging") as mock_log:
                mock_logger = MagicMock()
                mock_log.return_value = mock_logger

                from src.server.fastapi_app import lifespan
                app = MagicMock()

                with pytest.raises(StopIteration):
                    async with lifespan(app):
                        pass

                # Should have logged a warning about ffmpeg
                warning_calls = [
                    c for c in mock_logger.warning.call_args_list
                    if "ffmpeg" in str(c)
                ]
                assert len(warning_calls) >= 1

    @pytest.mark.asyncio
    async def test_ffmpeg_present_no_warning(self):
        """Should not log warning when ffmpeg is found."""
        with patch("shutil.which", return_value="/usr/bin/ffmpeg"):
            with patch("src.server.fastapi_app.setup_logging") as mock_log:
                mock_logger = MagicMock()
                mock_log.return_value = mock_logger

                from src.server.fastapi_app import lifespan
                app = MagicMock()

                with pytest.raises(StopIteration):
                    async with lifespan(app):
                        pass

                # Should NOT have logged a warning about ffmpeg
                warning_calls = [
                    c for c in mock_logger.warning.call_args_list
                    if "ffmpeg" in str(c)
                ]
                assert len(warning_calls) == 0