"""Tests para valhalla_isochrones_async."""

from __future__ import annotations

import asyncio
import sys
import os

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

import httpx
import pytest

from valhalla_isochrones_async import valhalla_isochrones_async


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_3_puntos_madrid_retornan_lista_de_3():
    """3 puntos Madrid retornan lista de 3"""
    pts = [
        {"lat": 40.4168, "lon": -3.7038, "minutes": 10, "id": "sol"},
        {"lat": 40.4530, "lon": -3.6883, "minutes": 10, "id": "retiro"},
        {"lat": 40.4005, "lon": -3.7057, "minutes": 10, "id": "atocha"},
    ]
    results = asyncio.run(valhalla_isochrones_async(pts))
    assert len(results) == 3, f"Esperaba 3 resultados, obtuvo {len(results)}"
    for i, gj in enumerate(results):
        assert gj is not None, f"Resultado {i} es None"
        assert "features" in gj, f"Resultado {i} no contiene 'features'"
