"""Backlog setlist recovery — mechanical guards (parsers-over-copy).

The parser cleans noisy backlog lines and resolves to canonical .tidal via the
catalog alias index. Test the load-bearing bits (leet fold, label strip, transition
tails) on synthetic lines, plus a coverage regression guard on the real backlog."""
import backlog_setlists as B


def test_leet_fold():
    assert B.leet_fold(B.norm_name("Burn this B00K")) == "burnthisbook"
    assert B.leet_fold(B.norm_name("Ghosts in the T01l3ts")) == "ghostsinthetoilets"
    assert B.leet_fold(B.norm_name("So G00D")) == "sogood"


def test_clean_strips_labels_and_keeps_track():
    # "Intro: X" / "Finale: X" → keep X, not the label
    name, *_ , ok = B.clean_track_line("- Intro    _🚪 Contre-Visite     👁️_")
    assert ok and name == "Contre-Visite"
    name, *_, ok = B.clean_track_line("- Finale: Lady Perplexity          [138bpm]")
    assert ok and name == "Lady Perplexity"


def test_clean_drops_gloss_after_colon():
    # "Name: gloss" (lowercase gloss) → keep Name
    name, *_, ok = B.clean_track_line("- Café tiède: au plus chaud")
    assert ok and name.lower().startswith("café tiède")


def test_clean_captures_bpm_and_transition():
    name, bpm, trans, ok = B.clean_track_line("- Quand on Décolle               [120] {11+12->11}")
    assert ok and bpm == 120 and trans == "11+12->11"


def test_clean_skips_ascii_and_comments():
    assert B.clean_track_line("⣿-----⣿")[-1] is False
    assert B.clean_track_line("<!-- - It's about Time -->")[-1] is False


def test_real_backlog_coverage_does_not_regress():
    """Guard: the cleanly-recovered gigs keep resolving. If the backlog or parser
    changes and these drop, that's a regression to look at (not a silent loss)."""
    out = B.build()
    by = {**out["gigs"], **out["pending"]}
    assert out["n_gigs"] >= 8
    # opal-2024 is a clean, fully-resolvable block — it must stay at parity
    assert by["2024/opal-festival-2024"]["n_resolved"] >= 13
    # no recovered gig should be empty
    for slug, g in out["gigs"].items():
        assert g["n_tracks"] >= 1, slug
