"""测试：RCT 规则检测器"""
from app.services.rct_detector import detect_rct


def test_confirmed_rct_by_pub_type():
    """pub_type 明确标注 Randomized Controlled Trial → confirmed"""
    result = detect_rct(
        pub_types=["Randomized Controlled Trial"],
        title="A trial of drug X for lung cancer",
        abstract="Patients were randomized to receive...",
        pub_year=2020,
    )
    assert result is not None
    assert result["is_rct"] is True
    assert result["confidence"] == "confirmed"
    assert result["source"] == "pub_type"


def test_confirmed_controlled_clinical_trial():
    """Controlled Clinical Trial 也属于 confirmed"""
    result = detect_rct(
        pub_types=["Controlled Clinical Trial"],
        title=None,
        abstract=None,
        pub_year=2021,
    )
    assert result is not None
    assert result["confidence"] == "confirmed"


def test_suspected_rct_by_keyword_abstract():
    """Clinical Trial + 摘要含 randomized → suspected"""
    result = detect_rct(
        pub_types=["Clinical Trial, Phase II"],
        title="Study of drug Y",
        abstract="This randomized double-blind trial evaluated...",
        pub_year=2022,
    )
    assert result is not None
    assert result["is_rct"] is True
    assert result["confidence"] == "suspected"
    assert result["source"] == "keyword"


def test_suspected_rct_title_keyword_only():
    """无 Clinical Trial pub_type，但标题含随机关键词 → suspected"""
    result = detect_rct(
        pub_types=["Journal Article"],
        title="A randomized controlled trial of drug Z",
        abstract="We evaluated the efficacy...",
        pub_year=2023,
    )
    assert result is not None
    assert result["confidence"] == "suspected"


def test_not_rct_no_keywords():
    """无关键词、无相关 pub_type → None"""
    result = detect_rct(
        pub_types=["Review"],
        title="A review of lung cancer treatments",
        abstract="This review summarizes recent advances...",
        pub_year=2022,
    )
    assert result is None


def test_not_rct_old_literature():
    """2000 年前的文献不做规则推断"""
    result = detect_rct(
        pub_types=["Clinical Trial"],
        title="A study",
        abstract="randomized trial of...",
        pub_year=1995,
    )
    assert result is None


def test_false_positive_suppression():
    """提到 'previous RCT' 的假阳性应被抑制"""
    result = detect_rct(
        pub_types=["Review"],
        title="A systematic review",
        abstract="Previous RCTs have shown that drug X is effective...",
        pub_year=2022,
    )
    assert result is None


def test_allocated_keyword():
    """allocated to receive 应触发 suspected"""
    result = detect_rct(
        pub_types=["Clinical Trial"],
        title="Study of drug A vs B",
        abstract="Patients were allocated to receive either drug A or placebo...",
        pub_year=2021,
    )
    assert result is not None
    assert result["confidence"] == "suspected"


def test_empty_pub_types_and_abstract():
    """空 pub_types 和空摘要 → None"""
    result = detect_rct(
        pub_types=[],
        title="",
        abstract="",
        pub_year=2020,
    )
    assert result is None


def test_none_fields():
    """None 字段不报错"""
    result = detect_rct(
        pub_types=None,
        title=None,
        abstract=None,
        pub_year=2020,
    )
    assert result is None
