"""Tests for CLI formatting helpers."""

from __future__ import annotations

from axm_bib.cli import _format_paper
from axm_bib.models import Paper


class TestFormatPaper:
    """_format_paper() — terminal display formatting."""

    def test_basic_output(self) -> None:
        """Includes index, title, authors, year, DOI."""
        paper = Paper(
            title="Deep Learning",
            authors=["John Smith"],
            year=2024,
            doi="10.1234/test",
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=False, abstract_len=0)
        assert "[1] [semantic_scholar]" in out
        assert "Deep Learning" in out
        assert "John Smith" in out
        assert "2024" in out
        assert "DOI:10.1234/test" in out

    def test_truncates_authors(self) -> None:
        """More than 3 authors → truncated with 'et al.'."""
        paper = Paper(
            title="T",
            authors=["Alice", "Bob", "Charlie", "Zara"],
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=False, abstract_len=0)
        assert "et al." in out
        assert "Zara" not in out  # 4th author excluded

    def test_abstract_displayed(self) -> None:
        """Abstract shown when flag is True."""
        paper = Paper(
            title="T",
            authors=["A"],
            abstract="This is a test abstract.",
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=True, abstract_len=0)
        assert "This is a test abstract." in out

    def test_abstract_truncated(self) -> None:
        """Abstract truncated to abstract_len chars."""
        paper = Paper(
            title="T",
            authors=["A"],
            abstract="A" * 100,
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=True, abstract_len=10)
        assert "..." in out
        assert len(out.split("\n")[-1].strip()) < 20

    def test_no_doi_shows_arxiv(self) -> None:
        """arXiv ID used when DOI is absent."""
        paper = Paper(
            title="T",
            authors=["A"],
            arxiv_id="2411.19275",
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=False, abstract_len=0)
        assert "arXiv:2411.19275" in out

    def test_no_doi_no_arxiv(self) -> None:
        """N/A shown when no DOI and no arXiv."""
        paper = Paper(
            title="T",
            authors=["A"],
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=False, abstract_len=0)
        assert "N/A" in out

    def test_missing_year(self) -> None:
        """Year shown as '?' when missing."""
        paper = Paper(
            title="T",
            authors=["A"],
            source="semantic_scholar",
        )
        out = _format_paper(paper, 1, abstract=False, abstract_len=0)
        assert "?" in out
