"""Tests para render_caddyfile."""

from render_caddyfile import render_caddyfile

HEADER = (
    "# Generado por render_caddyfile_py_infra — NO editar a mano. "
    "Fuente: apps/local_hub/local_services.yaml\n"
)


def test_golden_dos_servicios_ordenados():
    services = [
        {"subdomain": "metabase", "port": 3030},
        {"subdomain": "grafana", "port": 3000},
    ]
    result = render_caddyfile(services)
    expected = (
        HEADER
        + "http://grafana.localhost {\n"
        + "    reverse_proxy 127.0.0.1:3000\n"
        + "}\n"
        + "http://metabase.localhost {\n"
        + "    reverse_proxy 127.0.0.1:3030\n"
        + "}\n"
    )
    assert result == expected
    # Orden alfabetico: grafana antes que metabase pese al orden de entrada.
    assert result.index("grafana.localhost") < result.index("metabase.localhost")
    # Termina con un unico newline.
    assert result.endswith("}\n")
    assert not result.endswith("\n\n")


def test_dashboard_va_primero():
    services = [{"subdomain": "metabase", "port": 3030}]
    dashboard = {"subdomain": "home", "port": 8585}
    result = render_caddyfile(services, dashboard=dashboard)
    expected = (
        HEADER
        + "http://home.localhost {\n"
        + "    reverse_proxy 127.0.0.1:8585\n"
        + "}\n"
        + "http://metabase.localhost {\n"
        + "    reverse_proxy 127.0.0.1:3030\n"
        + "}\n"
    )
    assert result == expected
    # El dashboard aparece antes que cualquier servicio.
    assert result.index("home.localhost") < result.index("metabase.localhost")


def test_lista_vacia_solo_cabecera():
    result = render_caddyfile([])
    assert result == HEADER


def test_servicio_sin_port_se_ignora():
    services = [
        {"subdomain": "valido", "port": 9000},
        {"subdomain": "sin_port"},
    ]
    result = render_caddyfile(services)
    expected = (
        HEADER
        + "http://valido.localhost {\n"
        + "    reverse_proxy 127.0.0.1:9000\n"
        + "}\n"
    )
    assert result == expected
    assert "sin_port" not in result


def test_servicio_sin_subdomain_se_ignora():
    services = [
        {"subdomain": "valido", "port": 9000},
        {"port": 1234},
    ]
    result = render_caddyfile(services)
    assert "1234" not in result
    assert result.count("reverse_proxy") == 1


def test_rewrite_host_emite_header_up():
    services = [{"subdomain": "jupyter", "port": 8888, "rewrite_host": True}]
    result = render_caddyfile(services)
    expected = (
        HEADER
        + "http://jupyter.localhost {\n"
        + "    reverse_proxy 127.0.0.1:8888 {\n"
        + "        header_up Host 127.0.0.1:8888\n"
        + "    }\n"
        + "}\n"
    )
    assert result == expected


def test_rewrite_host_ausente_o_falso_no_reescribe():
    # Sin la clave -> bloque simple.
    assert "header_up" not in render_caddyfile([{"subdomain": "a", "port": 1}])
    # Con rewrite_host falsy -> bloque simple.
    assert "header_up" not in render_caddyfile(
        [{"subdomain": "a", "port": 1, "rewrite_host": False}]
    )
