from unittest.mock import patch

import pytest

from conversion_service import ConversionError, convert


def test_image_conversion_does_not_use_libreoffice():
    with patch('conversion_service._image_image', return_value=b'converted') as handler:
        result = convert('png', 'jpg', b'\x89PNG\r\n\x1a\nimage')

    handler.assert_called_once()
    assert result == b'converted'


def test_missing_office_renderer_is_server_error():
    with patch('conversion_service.shutil.which', return_value=None):
        with patch('conversion_service._validate'):
            with pytest.raises(ConversionError) as error:
                convert('docx', 'pdf', b'PK\x03\x04')

    assert error.value.status == 503


def test_unsupported_pair_is_rejected():
    with pytest.raises(ConversionError, match='not supported'):
        convert('zip', 'pdf', b'PK\x03\x04')


def test_pdf_to_docx_uses_page_renderer():
    with patch('conversion_service._validate'):
        with patch('conversion_service._pdf_docx', return_value=b'docx') as handler:
                result = convert('pdf', 'docx', b'%PDF-1.7')

    handler.assert_called_once()
    assert result == b'docx'