import datetime as dt
from app.screener.telegram import build_telegram_payload


def test_build_payload_includes_top10_and_link():
    rows = [
        {
            "rank": i, "ticker": f"00{i:04}", "name": f"종목{i}",
            "total_score": 90 - i,
            "scores": {"foreign_buy": 80 + i, "volume_surge": 60, "momentum": 70,
                       "high52w": 75, "rs_rating": 85, "ma_alignment": 80, "vcp_lite": 30},
            "close": 50000, "entry_price": 50250, "stop_price": 48500,
            "target_price": 53750, "r_pct": 3.5,
        }
        for i in range(1, 21)
    ]
    p = build_telegram_payload(
        asof=dt.date(2026, 5, 12),
        mode="auto",
        survivors_count=612,
        top_n=20,
        rows=rows,
        run_id=42,
    )
    assert p["parse_mode"] == "MarkdownV2"
    text = p["text"]
    assert "2026" in text and "05" in text and "12" in text
    assert "종목1" in text
    assert "종목10" in text
    assert "종목11" not in text           # 본문 1-10만
    assert "42" in text                   # run_id 링크


def test_score_threshold_filters_node_labels():
    rows = [{
        "rank": 1, "ticker": "A", "name": "A주",
        "total_score": 80,
        "scores": {"foreign_buy": 90, "volume_surge": 50, "momentum": 70,
                   "high52w": 30, "rs_rating": 80, "ma_alignment": 80, "vcp_lite": 60},
        "close": 50000, "entry_price": 50250, "stop_price": 48500,
        "target_price": 53750, "r_pct": 3.5,
    }]
    p = build_telegram_payload(dt.date(2026, 5, 12), "auto", 100, 1, rows, run_id=1)
    text = p["text"]
    # ≥70 노드만 풀 라벨로 표시 (foreign_buy=90, momentum=70, rs_rating=80, ma_alignment=80)
    assert "외국인 90" in text
    assert "20일모멘텀 70" in text
    assert "RS레이팅 80" in text
    assert "이평선정배열 80" in text
    # <70 노드는 숨김 (volume_surge=50, high52w=30, vcp_lite=60)
    assert "거래량급증" not in text
    assert "52주신고가" not in text
    assert "VCP수축" not in text


def test_prices_have_won_suffix():
    rows = [{
        "rank": 1, "ticker": "A", "name": "A주",
        "total_score": 80,
        "scores": {"foreign_buy": 80},
        "close": 50000, "entry_price": 50250, "stop_price": 48500,
        "target_price": 53750, "r_pct": 3.5,
    }]
    p = build_telegram_payload(dt.date(2026, 5, 12), "auto", 100, 1, rows, run_id=1)
    text = p["text"]
    assert "50,250원" in text
    assert "48,500원" in text
    assert "53,750원" in text
