"""Tests para generate_pwa_manifest."""

import sys
import os

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

from generate_pwa_manifest import generate_pwa_manifest


def test_valores_por_defecto_generan_manifest_valido_con_icons_placeholder():
    result = generate_pwa_manifest(name="Mi App", short_name="App")

    assert result["name"] == "Mi App"
    assert result["short_name"] == "App"
    assert result["start_url"] == "/"
    assert result["scope"] == "/"
    assert result["display"] == "standalone"
    assert result["orientation"] == "portrait"
    assert result["theme_color"] == "#000000"
    assert result["background_color"] == "#000000"
    assert result["lang"] == "es"
    assert result["description"] == ""
    assert result["categories"] == []

    icons = result["icons"]
    assert len(icons) == 2
    assert icons[0]["src"] == "/icons/icon-192x192.png"
    assert icons[0]["sizes"] == "192x192"
    assert icons[0]["type"] == "image/png"
    assert icons[0]["purpose"] == "any maskable"
    assert icons[1]["src"] == "/icons/icon-512x512.png"
    assert icons[1]["sizes"] == "512x512"


def test_icons_custom_se_respetan():
    custom_icons = [
        {"src": "/my-icon.png", "sizes": "256x256", "type": "image/png", "purpose": "any"},
    ]
    result = generate_pwa_manifest(name="App", short_name="A", icons=custom_icons)

    assert result["icons"] == custom_icons
    assert len(result["icons"]) == 1
    assert result["icons"][0]["src"] == "/my-icon.png"
    assert result["icons"][0]["sizes"] == "256x256"


def test_todos_los_campos_se_incluyen_correctamente():
    result = generate_pwa_manifest(
        name="Voice Guide",
        short_name="VoiceGuide",
        description="Guia por voz con POIs",
        start_url="/app",
        display="fullscreen",
        orientation="landscape",
        theme_color="#12b886",
        background_color="#1a1b1e",
        categories=["navigation", "travel"],
        lang="en",
    )

    assert result["name"] == "Voice Guide"
    assert result["short_name"] == "VoiceGuide"
    assert result["description"] == "Guia por voz con POIs"
    assert result["start_url"] == "/app"
    assert result["scope"] == "/"
    assert result["display"] == "fullscreen"
    assert result["orientation"] == "landscape"
    assert result["theme_color"] == "#12b886"
    assert result["background_color"] == "#1a1b1e"
    assert result["categories"] == ["navigation", "travel"]
    assert result["lang"] == "en"
    # icons son placeholders por defecto
    assert len(result["icons"]) == 2


def test_categories_none_produce_lista_vacia():
    result = generate_pwa_manifest(name="App", short_name="A", categories=None)
    assert result["categories"] == []


def test_json_dumps_compatible():
    import json

    result = generate_pwa_manifest(
        name="Voice Guide",
        short_name="VG",
        theme_color="#12b886",
        background_color="#1a1b1e",
    )
    serialized = json.dumps(result)
    parsed = json.loads(serialized)
    assert parsed["name"] == "Voice Guide"
    assert parsed["theme_color"] == "#12b886"
