"""Tests für Issue #1: Preflight-Check bei fehlendem Tesseract.""" from __future__ import annotations import sys from unittest.mock import patch import pytest from pdf_ocr_hotfolder.service import ( HotfolderService, PreflightError, check_preflight, ) def test_preflight_passes_when_all_binaries_present() -> None: """Wenn tesseract + gs im PATH sind, darf kein Fehler fliegen.""" with patch("pdf_ocr_hotfolder.service.shutil.which", return_value="/usr/bin/fake"): check_preflight() # darf nicht werfen def test_preflight_fails_when_tesseract_missing() -> None: """Fehlendes tesseract → PreflightError mit passender Meldung.""" def fake_which(name: str) -> str | None: return None if name == "tesseract" else "/usr/bin/fake" with patch("pdf_ocr_hotfolder.service.shutil.which", side_effect=fake_which): with pytest.raises(PreflightError, match="tesseract"): check_preflight() def test_preflight_fails_when_ghostscript_missing() -> None: def fake_which(name: str) -> str | None: return None if name == "gs" else "/usr/bin/fake" with patch("pdf_ocr_hotfolder.service.shutil.which", side_effect=fake_which): with pytest.raises(PreflightError, match="gs"): check_preflight() def test_preflight_lists_all_missing_binaries() -> None: """Bei mehreren fehlenden Binaries werden alle genannt.""" with patch("pdf_ocr_hotfolder.service.shutil.which", return_value=None): with pytest.raises(PreflightError) as exc_info: check_preflight() msg = str(exc_info.value) assert "tesseract" in msg assert "gs" in msg def test_run_once_raises_preflight_error(tmp_config) -> None: """HotfolderService.run_once() wirft PreflightError, wenn tesseract fehlt.""" service = HotfolderService(tmp_config) try: with patch("pdf_ocr_hotfolder.service.shutil.which", return_value=None): with pytest.raises(PreflightError): service.run_once() finally: service._executor.shutdown(wait=False) def test_main_returns_2_on_preflight_error(tmp_config, tmp_path, monkeypatch) -> None: """CLI liefert Exit-Code 2 bei Preflight-Fehler (Issue #1 Szenario).""" cfg_file = tmp_path / "cfg.toml" cfg_file.write_text(f""" [paths] incoming = "{tmp_config.paths.incoming}" outgoing = "{tmp_config.paths.outgoing}" working = "{tmp_config.paths.working}" error = "{tmp_config.paths.error}" """) monkeypatch.setattr(sys, "argv", ["pdf-ocr-hotfolder", "--config", str(cfg_file), "--once"]) with patch("pdf_ocr_hotfolder.service.shutil.which", return_value=None): from pdf_ocr_hotfolder.__main__ import main assert main() == 2