"""测试：阴性结果检测器"""

from app.services.negative_detector import detect_negative_result


def test_no_abstract():
    r = detect_negative_result(None)
    assert r == {"is_negative": False, "confidence": "low", "matched_patterns": []}


def test_empty_text():
    r = detect_negative_result("")
    assert r == {"is_negative": False, "confidence": "low", "matched_patterns": []}


def test_no_significant_difference():
    r = detect_negative_result("The study found no significant difference between groups.")
    assert r["is_negative"] is True
    assert r["confidence"] == "medium"


def test_failed_to_show():
    r = detect_negative_result("The treatment failed to show improvement.")
    assert r["is_negative"] is True


def test_not_statistically_significant():
    r = detect_negative_result("The primary endpoint was not statistically significant.")
    assert r["is_negative"] is True


def test_non_significant():
    r = detect_negative_result("Results were non-significant.")
    assert r["is_negative"] is True


def test_no_benefit():
    r = detect_negative_result("The drug showed no benefit over placebo.")
    assert r["is_negative"] is True


def test_p_greater_than_005():
    r = detect_negative_result("The p value was p>0.05 for the primary endpoint.")
    assert r["is_negative"] is True


def test_null_result():
    r = detect_negative_result("This null result suggests the drug is ineffective.")
    assert r["is_negative"] is True


def test_positive_counterpattern_wins():
    """阳性表述压倒阴性，不判定为阴性结果"""
    r = detect_negative_result(
        "The treatment demonstrated superiority over placebo. "
        "Although some secondary endpoints were not statistically significant, "
        "the primary endpoint showed significant improvement."
    )
    assert r["is_negative"] is False


def test_high_confidence_two_matches():
    """两个及以上阴性模式匹配 → high confidence"""
    r = detect_negative_result(
        "The trial failed to show benefit. The primary endpoint was not "
        "statistically significant. No significant difference was observed."
    )
    assert r["is_negative"] is True
    assert r["confidence"] == "high"


def test_title_abstract_combined():
    """标题 + 摘要联合检测"""
    r = detect_negative_result("failed to show efficacy.", "A negative trial of drug X")
    assert r["is_negative"] is True
    assert len(r["matched_patterns"]) >= 2


def test_positive_trial_not_marked_negative():
    """明确阳性的研究不误标"""
    r = detect_negative_result(
        "The primary endpoint was met with significant improvement in overall "
        "survival (HR 0.65, p=0.003)."
    )
    assert r["is_negative"] is False


def test_lack_of_efficacy():
    r = detect_negative_result("There was a lack of efficacy in the treatment arm.")
    assert r["is_negative"] is True
