"""Tests para osm2pgsql_ingest."""
from __future__ import annotations

import shutil
import tempfile
from pathlib import Path
from unittest.mock import patch

import pytest


def test_lanza_FileNotFoundError_con_path_inexistente():
    """lanza FileNotFoundError con path inexistente"""
    import sys
    sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
    from infra.osm2pgsql_ingest import osm2pgsql_ingest

    with pytest.raises(FileNotFoundError):
        osm2pgsql_ingest("/tmp/non_existent_file_that_does_not_exist.osm.pbf")


def test_lanza_RuntimeError_si_osm2pgsql_no_esta_en_PATH():
    """lanza RuntimeError si osm2pgsql no esta en PATH"""
    import sys
    sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
    from infra.osm2pgsql_ingest import osm2pgsql_ingest

    with tempfile.TemporaryDirectory() as tmpdir:
        pbf_path = Path(tmpdir) / "fake.osm.pbf"
        # Create a dummy file so FileNotFoundError is not raised first
        pbf_path.write_bytes(b"PBF")

        # Skip test if osm2pgsql is actually in PATH (CI environment may have it)
        if shutil.which("osm2pgsql") is not None:
            pytest.skip("osm2pgsql is available in PATH; skipping RuntimeError test")

        with pytest.raises(RuntimeError, match="osm2pgsql"):
            osm2pgsql_ingest(pbf_path)
