from plexreaper.config import ProtectConfig, Weights from plexreaper.evidence import build_evidence from plexreaper.models import Evidence from plexreaper.scoring import compile_protection, protection, score, watched_collections from tests.conftest import NOW, make_session, make_title W = Weights() def codes(b): return {s.code: s.points for s in b.signals} def test_never_watched_old_large_low_rated_scores_high(): t = make_title(days_ago=1500, size_gb=60, audience_rating=3.1) b = score(t, Evidence(), W, now=NOW) c = codes(b) assert c["NEVER"] == 30 and c["AGE"] == 15 and c["LOWRATE"] == 5 assert 13 <= c["SIZE"] <= 15 assert "BLOAT" in c and "downgrade" in b.flags assert b.score >= 65 def test_watched_recently_scores_low(): t = make_title(days_ago=1500, size_gb=60) ev = build_evidence([make_session(percent=100, days_ago=10, user_id=1), make_session(percent=100, user_id=2)], {}, __import__("plexreaper.config").config.EvidenceConfig()) b = score(t, ev["plex://movie/aaa"], W, now=NOW) c = codes(b) assert c["WATCHED"] == -50 and c["RECENT"] == -20 assert "NEVER" not in c assert b.score == 0 def test_ghost_and_abandon_add_up_with_cap(): ev = Evidence(touch_sessions=2, abandoned_sessions=5, abandoned_users=4) c = codes(score(make_title(days_ago=100), ev, W, now=NOW)) assert c["GHOST"] == 5 and c["ABANDON"] == 24 def test_prior_without_sessions_keeps(): t = make_title(plex_view_count=2, days_ago=100, size_gb=0.5) c = codes(score(t, Evidence(), W, now=NOW)) assert c["PRIOR"] == -25 and "NEVER" not in c def test_sibling_collection(): watched = make_title("plex://movie/w", rating_key=1, collections=["Saga"], plex_view_count=1) unwatched = make_title("plex://movie/u", rating_key=2, collections=["Saga"]) wc = watched_collections([watched, unwatched], {}) assert wc == {"Saga"} assert codes(score(unwatched, Evidence(), W, now=NOW, watched_collections=wc))["SIBLING"] == -15 def test_stalled_show(): show = make_title("plex://show/s", kind="show", season_count=5) ev = Evidence(full_sessions=1, full_users=1, furthest_season=1, furthest_episode=3) b = score(show, ev, W, now=NOW) assert codes(b)["STALLED"] == 10 and "trim" in b.flags def test_protection_rules(): rules = compile_protection(ProtectConfig(genres=["Sport"], title_patterns=[r"(?i)\bimax\b", "("], libraries=[9])) assert protection(make_title(genres=["sport"]), rules) == "genre 'sport'" assert protection(make_title(title="Dunkirk IMAX"), rules).startswith("title matches") assert protection(make_title(library_id=9), rules).startswith("library") assert protection(make_title(), rules) is None def test_score_clamped(): ev = Evidence(full_sessions=9, full_users=9, abandoned_sessions=0) assert score(make_title(), ev, W, now=NOW).score == 0