"""Tests para detect_url_type (tests que no requieren red)."""

import sys
import os

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

from core.detect_url_type import detect_url_type, _type_from_extension, _type_from_content_type, _is_ssh_git_url


def test_url_pdf_por_extension():
    """URL .pdf se detecta por extension sin hacer request HTTP."""
    url_type, metadata = detect_url_type("https://example.com/report.pdf")
    assert url_type == "pdf"
    assert metadata["detection"] == "extension"


def test_url_github_repo():
    """URL de GitHub org/repo se detecta como code_repository por patron URL."""
    url_type, metadata = detect_url_type("https://github.com/openai/whisper")
    assert url_type == "code_repository"
    assert metadata["detection"] == "url_pattern"


def test_url_github_con_git_suffix():
    """URL github terminada en .git se detecta como code_repository."""
    url_type, metadata = detect_url_type("https://github.com/openai/whisper.git")
    assert url_type == "code_repository"


def test_url_markdown_por_extension():
    """URL .md se detecta como markdown por extension."""
    url_type, metadata = detect_url_type("https://example.com/README.md")
    assert url_type == "markdown"
    assert metadata["detection"] == "extension"


def test_url_ssh_git():
    """URL SSH git@ se detecta como code_repository."""
    url_type, metadata = detect_url_type("git@github.com:openai/whisper.git")
    assert url_type == "code_repository"
    assert metadata["detection"] == "ssh_pattern"


def test_url_html_por_extension():
    """URL .html se detecta como webpage por extension."""
    url_type, metadata = detect_url_type("https://example.com/page.html")
    assert url_type == "webpage"
    assert metadata["detection"] == "extension"


def test_url_txt_por_extension():
    """URL .txt se detecta como text por extension."""
    url_type, metadata = detect_url_type("https://example.com/data.txt")
    assert url_type == "text"


def test_github_subrepo_no_es_repo():
    """URL de GitHub apuntando a un issue/blob no se trata como code_repository."""
    # Debe intentar HEAD request (que fallara sin red) — verificamos que no clasifica como repo
    # Solo comprobamos que no devuelve code_repository por patron URL
    url = "https://github.com/openai/whisper/blob/main/README.md"
    # Extension .md deberia detectarse primero
    url_type, metadata = detect_url_type(url)
    assert url_type == "markdown"


def test_helper_type_from_extension():
    """_type_from_extension funciona para extensiones conocidas."""
    assert _type_from_extension("/doc.pdf") == "pdf"
    assert _type_from_extension("/README.md") == "markdown"
    assert _type_from_extension("/notes.txt") == "text"
    assert _type_from_extension("/unknown.xyz") is None


def test_helper_type_from_content_type():
    """_type_from_content_type mapea headers correctamente."""
    assert _type_from_content_type("application/pdf; charset=utf-8") == "pdf"
    assert _type_from_content_type("text/html; charset=utf-8") == "webpage"
    assert _type_from_content_type("text/plain") == "text"
    assert _type_from_content_type("text/markdown") == "markdown"
    assert _type_from_content_type("application/octet-stream") == "webpage"


def test_helper_is_ssh_git_url():
    """_is_ssh_git_url detecta formato git@."""
    assert _is_ssh_git_url("git@github.com:org/repo.git") is True
    assert _is_ssh_git_url("https://github.com/org/repo") is False
    assert _is_ssh_git_url("ssh://git@github.com/org/repo") is False
