import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from app.notifiers import telegram_lotto as tl


def test_format_sunday_review_text():
    payload = {
        "draw_no": 1170,
        "winner_analysis": {"score_total": 0.41, "percentile": 0.33,
            "score_frequency": 0.4, "score_fingerprint": 0.5, "score_gap": 0.3,
            "score_cooccur": 0.45, "score_diversity": 0.6},
        "forward": [
            {"strategy": "engine_w", "label": "w1", "prizes": {"1st":0,"2nd":0,"3rd":0,"4th":1,"5th":12}, "best_match": 4, "avg_meta_score": 0.55},
            {"strategy": "random_null", "label": "-", "prizes": {"1st":0,"2nd":0,"3rd":0,"4th":0,"5th":10}, "best_match": 3, "avg_meta_score": 0.33},
        ],
        "track_record": {},
        "calibration_trend": [{"draw_no":1170,"score_total":0.41,"percentile":0.33}],
    }
    txt = tl.format_sunday_review(payload)
    assert "1170" in txt
    assert "%" in txt        # percentile 표기
    assert "engine" in txt.lower() or "엔진" in txt


def test_format_sunday_review_no_calibration():
    payload = {"draw_no": 1171, "winner_analysis": None, "forward": []}
    txt = tl.format_sunday_review(payload)
    assert "1171" in txt
    assert "%" not in txt  # no percentile section when calibration absent
    assert "데이터 없음" in txt


def test_format_sunday_review_missing_prizes_no_crash():
    payload = {"draw_no": 1171, "winner_analysis": None,
               "forward": [{"strategy": "engine_w", "label": "w1", "best_match": 3}]}  # no 'prizes'
    txt = tl.format_sunday_review(payload)   # must NOT raise
    assert "1171" in txt
