"""
Tests for OPML route

Tests the /opml.xml endpoint per v1.1.2 Phase 3.
"""

import pytest
from xml.etree import ElementTree as ET


def test_opml_endpoint_exists(client):
    """Test OPML endpoint is accessible"""
    response = client.get("/opml.xml")
    assert response.status_code == 200


def test_opml_no_auth_required(client):
    """Test OPML endpoint is public (no auth required per CQ8)"""
    # Should succeed without authentication
    response = client.get("/opml.xml")
    assert response.status_code == 200


def test_opml_content_type(client):
    """Test OPML endpoint returns correct content type"""
    response = client.get("/opml.xml")
    assert response.content_type == "application/xml; charset=utf-8"


def test_opml_cache_headers(client):
    """Test OPML endpoint includes cache headers"""
    response = client.get("/opml.xml")
    assert "Cache-Control" in response.headers
    assert "public" in response.headers["Cache-Control"]
    assert "max-age" in response.headers["Cache-Control"]


def test_opml_valid_xml(client):
    """Test OPML endpoint returns valid XML"""
    response = client.get("/opml.xml")

    try:
        root = ET.fromstring(response.data)
        assert root.tag == "opml"
        assert root.get("version") == "2.0"
    except ET.ParseError as e:
        pytest.fail(f"Invalid XML returned: {e}")


def test_opml_contains_all_feeds(client):
    """Test OPML contains all three feed formats"""
    response = client.get("/opml.xml")
    root = ET.fromstring(response.data)
    body = root.find("body")
    outlines = body.findall("outline")

    assert len(outlines) == 3

    # Check all feed URLs are present
    urls = [outline.get("xmlUrl") for outline in outlines]
    assert any("/feed.rss" in url for url in urls)
    assert any("/feed.atom" in url for url in urls)
    assert any("/feed.json" in url for url in urls)


def test_opml_site_name_in_title(client, app):
    """Test OPML includes site name in title"""
    response = client.get("/opml.xml")
    root = ET.fromstring(response.data)
    head = root.find("head")
    title = head.find("title")

    # Should contain site name from config
    site_name = app.config.get("SITE_NAME", "StarPunk")
    assert site_name in title.text


def test_opml_feed_discovery_link(client):
    """Test OPML feed discovery link exists in HTML head"""
    response = client.get("/")
    assert response.status_code == 200

    # Should have OPML discovery link
    assert b'type="application/xml+opml"' in response.data
    assert b'/opml.xml' in response.data
