"""测试：方法学小结生成"""

from app.services.methods_summary import generate_methods_summary


def _flow(ident=100, screened=85, ex_screen=65, full_text=20, ex_elig=15, included=5,
          reasons=None):
    return {
        "identification_total": ident,
        "screened": screened,
        "excluded_screening": ex_screen,
        "full_text_assessed": full_text,
        "excluded_eligibility": ex_elig,
        "included_total": included,
        "excluded_reasons": reasons or {},
    }


def test_summary_zh_basic():
    review = {"databases_searched": ["PubMed", "Embase"], "created_at": "2026-06-01"}
    flow = _flow()
    s = generate_methods_summary(review, flow, "zh")
    assert "PubMed" in s
    assert "Embase" in s
    assert "85" in s
    assert "5" in s
    assert "2026-06-01" in s


def test_summary_en():
    review = {"databases_searched": ["PubMed"], "created_at": "2026-06-01"}
    flow = _flow()
    s = generate_methods_summary(review, flow, "en")
    assert "searched" in s
    assert "PubMed" in s
    assert "were screened" in s
    assert "were included" in s


def test_summary_no_databases():
    review = {"created_at": "2026-06-01"}
    flow = _flow()
    s = generate_methods_summary(review, flow, "zh")
    assert "PubMed" in s  # 回退默认值


def test_summary_with_exclusion_reasons():
    review = {"databases_searched": ["PubMed"], "created_at": "2026-06-01"}
    flow = _flow(reasons={"No full text": 10, "Wrong design": 5})
    s = generate_methods_summary(review, flow, "zh")
    assert "No full text(n=10)" in s or "No full text" in s
    assert "Wrong design" in s
    assert "15" in s or "15篇" in s  # excluded_eligibility count


def test_summary_search_strategy_date():
    """search_strategy 中的日期优先于 created_at"""
    review = {
        "databases_searched": ["PubMed"],
        "search_strategy": {"search_date": "2026-05-15"},
        "created_at": "2026-06-01",
    }
    flow = _flow()
    s = generate_methods_summary(review, flow, "zh")
    assert "2026-05-15" in s
    assert "2026-06-01" not in s
