"""Tests para valhalla_route."""

from __future__ import annotations

import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

import httpx
import pytest

from valhalla_route import valhalla_route


def _valhalla_alive(url: str = "http://localhost:8002") -> bool:
    try:
        r = httpx.get(f"{url}/status", timeout=2.0)
        return r.status_code < 500
    except Exception:
        return False


VALHALLA_OK = _valhalla_alive()
skip_if_no_valhalla = pytest.mark.skipif(
    not VALHALLA_OK, reason="Valhalla no activo en :8002"
)


@skip_if_no_valhalla
def test_ruta_madrid_barcelona_supera_500_km():
    """ruta Madrid-Barcelona supera 500 km"""
    result = valhalla_route(
        locations=[
            {"lat": 40.4168, "lon": -3.7038},
            {"lat": 41.3874, "lon": 2.1686},
        ]
    )
    assert result is not None, "Esperaba respuesta, obtuvo None"
    summary = result["trip"]["summary"]
    assert summary["length"] > 500, f"Distancia {summary['length']} km < 500 km"
