"""Edge case tests for journal quality scoring — gaps not covered by test_scoring.py."""

from local_deep_research.journal_quality.scoring import (
    derive_quality_score,
    normalize_name,
)
from local_deep_research.constants import (
    JOURNAL_QUALITY_STRONG,
)


class TestNegativeHIndex:
    def test_negative_h_index_returns_none(self):
        """Negative h-index is a data error — treated as no signal (None)."""
        result = derive_quality_score(h_index=-1)
        assert result is None

    def test_negative_large_h_index_returns_none(self):
        """Very negative h-index also returns None."""
        result = derive_quality_score(h_index=-999)
        assert result is None

    def test_negative_h_index_with_doaj_uses_doaj_floor(self):
        """Negative h-index with DOAJ should still honour DOAJ fallback."""
        result = derive_quality_score(h_index=-1, is_in_doaj=True)
        # Falls through to is_in_doaj branch since h-index is invalid
        assert result is not None
        assert result >= 5


class TestInvalidQuartile:
    def test_q5_falls_through_to_h_index(self):
        """Q5 is not a valid quartile — falls to h-index branch."""
        result = derive_quality_score(quartile="Q5", h_index=80)
        # Q5 not matched, falls through to h_index > 75 → STRONG
        assert result == JOURNAL_QUALITY_STRONG

    def test_q0_falls_through(self):
        """Q0 is not valid — returns None when no other signal."""
        result = derive_quality_score(quartile="Q0")
        assert result is None


class TestQ1WithHIndexZero:
    def test_q1_h_index_zero_returns_strong_not_elite(self):
        """Q1 with h_index=0 → STRONG (not elite, because 0 is falsy)."""
        result = derive_quality_score(quartile="Q1", h_index=0)
        assert result == JOURNAL_QUALITY_STRONG


class TestNormalizeName:
    def test_empty_string(self):
        assert normalize_name("") == ""

    def test_ligature_expansion(self):
        """NFKC expands fi-ligature (U+FB01) to 'fi'."""
        assert normalize_name("\ufb01eld") == "field"

    def test_fullwidth_characters(self):
        """NFKC normalizes fullwidth latin to ASCII."""
        assert (
            normalize_name("\uff2e\uff41\uff54\uff55\uff52\uff45") == "nature"
        )


class TestThreeWayPriority:
    def test_predatory_plus_quartile_plus_doaj(self):
        """predatory=True + Q1 + doaj=True → quartile wins (DOAJ rescues)."""
        result = derive_quality_score(
            quartile="Q1",
            is_predatory=True,
            is_in_doaj=True,
        )
        # DOAJ rescues from predatory, then quartile takes precedence
        assert result == JOURNAL_QUALITY_STRONG
