"""Unit tests for axm_bib.download — fallback_bib_key + doi_fallback_urls."""

from __future__ import annotations

from axm_bib.download import doi_fallback_urls, fallback_bib_key


class TestFallbackBibKey:
    """AXM-263: fallback_bib_key edge cases."""

    def test_doi_with_slashes_and_dots(self) -> None:
        """DOI with slashes/dots returns sanitized key."""
        key = fallback_bib_key("10.1234/some.paper-2024")
        assert "/" not in key
        assert "." not in key
        assert key == "10_1234_some_paper_2024"

    def test_arxiv_id(self) -> None:
        """arXiv ID sanitized to safe key."""
        key = fallback_bib_key("2210.02406")
        assert key == "2210_02406"

    def test_empty_string_returns_unknown(self) -> None:
        """Empty string returns 'unknown'."""
        assert fallback_bib_key("") == "unknown"

    def test_only_special_chars_returns_unknown(self) -> None:
        """String of only special chars returns 'unknown'."""
        assert fallback_bib_key("///...---") == "unknown"

    def test_long_ref_truncated(self) -> None:
        """Very long ref is truncated to 80 chars."""
        key = fallback_bib_key("a" * 200)
        assert len(key) <= 80

    def test_result_is_lowercase(self) -> None:
        """Key is always lowercase."""
        key = fallback_bib_key("ABC_DEF")
        assert key == "abc_def"


class TestDoiFallbackUrls:
    """AXM-263: doi_fallback_urls edge cases."""

    def test_arxiv_doi(self) -> None:
        """arXiv DOI returns arxiv.org PDF URL."""
        urls = doi_fallback_urls("10.48550/arXiv.2411.19275")
        assert urls == ["https://arxiv.org/pdf/2411.19275"]

    def test_acl_doi(self) -> None:
        """ACL Anthology DOI returns PDF URL."""
        urls = doi_fallback_urls("10.18653/v1/2024.acl-long.172")
        assert urls == ["https://aclanthology.org/2024.acl-long.172.pdf"]

    def test_unknown_doi_returns_empty(self) -> None:
        """Random DOI returns empty list."""
        assert doi_fallback_urls("10.1000/xyz") == []

    def test_non_doi_returns_empty(self) -> None:
        """Non-DOI string returns empty list."""
        assert doi_fallback_urls("not-a-doi") == []
