"""Tests for atrun.publish — filename and URL parsing."""

from __future__ import annotations

import pytest

from atrun.publish import _name_version_from_dist_filename, _name_version_from_dist_url


# --- _name_version_from_dist_filename ---


def test_filename_wheel():
    assert _name_version_from_dist_filename("atrun-0.5.0-py3-none-any.whl") == ("atrun", "0.5.0")


def test_filename_tar_gz():
    assert _name_version_from_dist_filename("foo-1.2.3.tar.gz") == ("foo", "1.2.3")


def test_filename_tgz():
    assert _name_version_from_dist_filename("cowsay-1.6.0.tgz") == ("cowsay", "1.6.0")


def test_filename_hyphenated_name_tar_gz():
    assert _name_version_from_dist_filename("my-package-1.2.3.tar.gz") == ("my-package", "1.2.3")


def test_filename_hyphenated_name_tgz():
    assert _name_version_from_dist_filename("string-width-4.2.3.tgz") == ("string-width", "4.2.3")


def test_filename_multi_hyphen_name():
    assert _name_version_from_dist_filename("my-cool-package-2.0.0.tgz") == ("my-cool-package", "2.0.0")


def test_filename_v_prefixed_version():
    assert _name_version_from_dist_filename("my-package-v1.2.3.tar.gz") == ("my-package", "v1.2.3")


def test_filename_v_prefixed_tgz():
    assert _name_version_from_dist_filename("my-tool-v0.9.0.tgz") == ("my-tool", "v0.9.0")


def test_filename_no_version():
    with pytest.raises(SystemExit, match="Cannot parse"):
        _name_version_from_dist_filename("noversion")


# --- _name_version_from_dist_url ---


def test_url_crates_io():
    assert _name_version_from_dist_url(
        "https://crates.io/api/v1/crates/ripgrep/14.1.0/download"
    ) == ("ripgrep", "14.1.0")


def test_url_golang_proxy():
    assert _name_version_from_dist_url(
        "https://proxy.golang.org/golang.org/x/text/@v/v0.14.0.zip"
    ) == ("golang.org/x/text", "v0.14.0")


def test_url_npm_fallback_to_filename():
    name, version = _name_version_from_dist_url(
        "https://registry.npmjs.org/cowsay/-/cowsay-1.6.0.tgz"
    )
    assert name == "cowsay"
    assert version == "1.6.0"


def test_url_npm_scoped_package():
    name, version = _name_version_from_dist_url(
        "https://registry.npmjs.org/@babel/core/-/core-7.24.0.tgz"
    )
    assert name == "@babel/core"
    assert version == "7.24.0"


def test_url_golang_uppercase_escape():
    name, version = _name_version_from_dist_url(
        "https://proxy.golang.org/github.com/!azure/azure-sdk/@v/v1.0.0.zip"
    )
    assert name == "github.com/Azure/azure-sdk"
    assert version == "v1.0.0"


# --- oci:// URL parsing ---


def test_url_oci_full():
    name, version = _name_version_from_dist_url("oci://ghcr.io/user/app:1.0.0")
    assert name == "ghcr.io/user/app"
    assert version == "1.0.0"


def test_url_oci_bare():
    name, version = _name_version_from_dist_url("oci://docker.io/library/nginx:1.25")
    assert name == "docker.io/library/nginx"
    assert version == "1.25"


def test_url_oci_no_tag():
    name, version = _name_version_from_dist_url("oci://ghcr.io/user/app")
    assert name == "ghcr.io/user/app"
    assert version == "latest"


# --- docker shorthand ---


def test_docker_shorthand_full():
    from atrun.publish import _resolve_docker_shorthand
    assert _resolve_docker_shorthand("docker:ghcr.io/user/app:1.0.0") == "oci://ghcr.io/user/app:1.0.0"


def test_docker_shorthand_bare():
    from atrun.publish import _resolve_docker_shorthand
    result = _resolve_docker_shorthand("docker:nginx:1.25")
    assert result == "oci://docker.io/library/nginx:1.25"


def test_docker_shorthand_no_tag():
    from atrun.publish import _resolve_docker_shorthand
    result = _resolve_docker_shorthand("docker:user/app")
    assert result == "oci://docker.io/user/app:latest"
