"""Pure scoring helpers for the series continuation matcher (FC-6.3)."""

from backend.app.services.series_match_service import (
    normalize_title,
    pages_signal,
    tags_signal,
    title_signal,
    weighted_score,
)


def test_normalize_title_strips_installment_markers():
    assert normalize_title("Story pages 9-12") == "story"
    assert normalize_title("Bunker Diaries 04") == "bunker diaries"
    assert normalize_title("My Comic [3/8]") == "my comic"
    assert normalize_title("") == ""
    assert normalize_title(None) == ""


def test_title_signal():
    # Same stem after stripping page markers → full match.
    assert title_signal(["Story pages 1-4"], "Story pages 9-12") == 1.0
    # Unrelated → no overlap.
    assert title_signal(["My Comic"], "Other Thing") == 0.0
    # No data → 0.
    assert title_signal([], "x") == 0.0


def test_pages_signal():
    assert pages_signal(8, 9) == 1.0      # clean continuation
    assert pages_signal(8, 10) == 0.5     # near (diff 2)
    assert pages_signal(8, 11) == 0.5     # near (diff 3)
    assert pages_signal(8, 20) == 0.0     # far
    assert pages_signal(8, 5) == 0.0      # before / overlap
    assert pages_signal(None, 9) == 0.0


def test_tags_signal():
    assert tags_signal(0) == 0.0
    assert tags_signal(3) == 1.0
    assert tags_signal(6) == 1.0          # capped
    assert round(tags_signal(1), 3) == 0.333


def test_weighted_score():
    assert weighted_score({"title": 1, "artist": 1, "pages": 1, "tags": 1}) == 1.0
    assert weighted_score({}) == 0.0
    assert weighted_score({"title": 1.0, "artist": 1.0}) == 0.6
