"""Tests para convert_github_to_raw_url."""

import sys
import os

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

from core.convert_github_to_raw_url import convert_github_to_raw_url


def test_url_github_blob():
    """URL de GitHub blob se convierte correctamente a raw.githubusercontent.com."""
    url = "https://github.com/openai/whisper/blob/main/README.md"
    result = convert_github_to_raw_url(url)
    assert result == "https://raw.githubusercontent.com/openai/whisper/main/README.md"


def test_url_github_blob_subdirectorio():
    """URL de GitHub blob con subdirectorio se convierte correctamente."""
    url = "https://github.com/org/repo/blob/main/src/utils/helper.py"
    result = convert_github_to_raw_url(url)
    assert result == "https://raw.githubusercontent.com/org/repo/main/src/utils/helper.py"


def test_url_github_blob_otra_rama():
    """URL de GitHub blob con rama distinta a main se convierte correctamente."""
    url = "https://github.com/org/repo/blob/develop/config.yaml"
    result = convert_github_to_raw_url(url)
    assert result == "https://raw.githubusercontent.com/org/repo/develop/config.yaml"


def test_url_gitlab_blob():
    """URL de GitLab blob se convierte a raw."""
    url = "https://gitlab.com/org/repo/-/blob/main/README.md"
    result = convert_github_to_raw_url(url)
    assert result == "https://gitlab.com/org/repo/-/raw/main/README.md"


def test_url_gitlab_blob_sin_guion():
    """URL de GitLab blob sin '/-/' tambien se convierte."""
    url = "https://gitlab.com/org/repo/blob/main/README.md"
    result = convert_github_to_raw_url(url)
    assert result == "https://gitlab.com/org/repo/raw/main/README.md"


def test_url_que_no_es_blob_retorna_sin_cambios():
    """URL de GitHub sin blob retorna sin cambios."""
    url = "https://github.com/org/repo"
    result = convert_github_to_raw_url(url)
    assert result == url


def test_url_github_tree_retorna_sin_cambios():
    """URL de GitHub tree (no blob) retorna sin cambios."""
    url = "https://github.com/org/repo/tree/main/src"
    result = convert_github_to_raw_url(url)
    assert result == url


def test_url_no_github_retorna_sin_cambios():
    """URL de otro dominio retorna sin cambios."""
    url = "https://example.com/org/repo/blob/main/file.py"
    result = convert_github_to_raw_url(url)
    assert result == url


def test_url_vacia_retorna_sin_cambios():
    """URL vacia retorna string vacio."""
    result = convert_github_to_raw_url("")
    assert result == ""


def test_url_raw_githubusercontent_retorna_sin_cambios():
    """URL ya en raw.githubusercontent.com no se modifica."""
    url = "https://raw.githubusercontent.com/org/repo/main/file.py"
    result = convert_github_to_raw_url(url)
    assert result == url
