"""Tests for Nexus package info extractors."""

from guarddog_nexus.core.nexus import (
    extract_go_info,
    extract_npm_info,
    extract_package_info,
    extract_pypi_info,
)


class TestPyPIExtractor:
    def test_simple(self):
        assert extract_pypi_info("/packages/requests/2.31.0/requests-2.31.0.tar.gz") == (
            "requests",
            "2.31.0",
        )

    def test_leading_slash(self):
        assert extract_pypi_info("packages/numpy/1.24.0/numpy-1.24.0.tar.gz") == (
            "numpy",
            "1.24.0",
        )

    def test_non_package(self):
        assert extract_pypi_info("/some/path") is None

    def test_too_short(self):
        assert extract_pypi_info("/packages/pkg") is None


class TestGoExtractor:
    def test_simple(self):
        assert extract_go_info("packages/github.com/gorilla/mux/@v/v1.8.0.zip") == (
            "github.com/gorilla/mux",
            "v1.8.0",
        )

    def test_long_module(self):
        assert extract_go_info("/packages/github.com/gin-gonic/gin/@v/v1.9.0.zip") == (
            "github.com/gin-gonic/gin",
            "v1.9.0",
        )

    def test_no_at_v(self):
        assert extract_go_info("packages/some/pkg/v1.0.0.zip") is None

    def test_empty(self):
        assert extract_go_info("") is None


class TestNpmExtractor:
    def test_simple(self):
        assert extract_npm_info("packages/lodash/-/lodash-4.17.21.tgz") == (
            "lodash",
            "4.17.21",
        )

    def test_scoped_package(self):
        # Note: scoped packages have a different path in Nexus
        assert extract_npm_info("packages/@scope/name/-/name-1.0.0.tgz") is None

    def test_not_packages(self):
        assert extract_npm_info("/other/lodash/-/lodash-4.17.21.tgz") is None

    def test_short_path(self):
        assert extract_npm_info("packages/lodash") is None


class TestDispatchExtractor:
    def test_pypi(self):
        assert extract_package_info("/packages/requests/2.31.0/requests-2.31.0.tar.gz", "pypi") == (
            "requests",
            "2.31.0",
        )

    def test_go(self):
        assert extract_package_info("github.com/gorilla/mux/@v/v1.8.0.zip", "go") == (
            "github.com/gorilla/mux",
            "v1.8.0",
        )

    def test_npm(self):
        assert extract_package_info("packages/lodash/-/lodash-4.17.21.tgz", "npm") == (
            "lodash",
            "4.17.21",
        )

    def test_unknown_ecosystem(self):
        assert extract_package_info("/packages/pkg/1.0/file.tar.gz", "unknown") == ("pkg", "1.0")
