"""Tests para render_template."""

from render_template import render_template


def test_sustitucion_simple():
    result = render_template("Hola {{nombre}}!", {"nombre": "Ana"})
    assert result == "Hola Ana!"


def test_dot_path_en_nested_dicts():
    ctx = {"user": {"name": "Bob", "address": {"city": "Madrid"}}}
    result = render_template("{{user.name}} vive en {{user.address.city}}", ctx)
    assert result == "Bob vive en Madrid"


def test_for_loop_con_lista_de_strings():
    ctx = {"items": ["a", "b", "c"]}
    result = render_template("{% for x in items %}{{x}},{% endfor %}", ctx)
    assert result == "a,b,c,"


def test_for_loop_con_lista_de_dicts():
    ctx = {"rows": [{"name": "Ana"}, {"name": "Bob"}]}
    result = render_template("{% for row in rows %}{{row.name}} {% endfor %}", ctx)
    assert result == "Ana Bob "


def test_if_endif_condicional():
    result = render_template("{% if show %}visible{% endif %}", {"show": True})
    assert result == "visible"

    result = render_template("{% if show %}visible{% endif %}", {"show": False})
    assert result == ""


def test_variable_missing_retorna_string_vacio():
    result = render_template("{{inexistente}}", {})
    assert result == ""


def test_variable_missing_configurable():
    result = render_template("{{inexistente}}", {}, missing="N/A")
    assert result == "N/A"


def test_html_escaping_por_defecto():
    ctx = {"code": "<script>alert('xss')</script>"}
    result = render_template("{{code}}", ctx)
    assert "<script>" not in result
    assert "&lt;script&gt;" in result


def test_triple_braces_sin_escape():
    ctx = {"html": "<b>bold</b>"}
    result = render_template("{{{html}}}", ctx)
    assert result == "<b>bold</b>"
