File size: 3,279 Bytes
535dd76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# tests/test_citation_cartel.py
#
# Test suite for Citation Cartel Detector.
# 8 tests covering cartel detection, diversity,
# self-citation, and risk levels.

import pytest
from src.scipeerai.modules.citation_cartel import CitationCartelDetector

engine = CitationCartelDetector()


def test_cartel_detected():
    """Smith dominates citations — critical risk."""
    r = engine.analyze(
        "Based on Smith (2021), Smith (2020), Smith (2019), "
        "Smith (2018), Smith (2017), Jones (2020), Jones (2019), "
        "Smith et al. (2022) we conclude. Smith (2016) also showed."
    )
    assert r.risk_level in ("high", "critical")
    assert r.cartel_score > 0.3


def test_diverse_citations_pass():
    """Many different authors — low risk."""
    r = engine.analyze(
        "Smith (2021) showed X. Jones (2020) found Y. "
        "Williams (2019) reported Z. Brown (2018) confirmed. "
        "Davis (2022) extended. Wilson (2021) replicated. "
        "Taylor (2020) validated. Anderson (2019) supported."
    )
    assert r.risk_level in ("low", "medium")


def test_insufficient_citations():
    """Less than 5 citations — low risk flag."""
    r = engine.analyze(
        "This study examines cognitive behavioral therapy "
        "effects on adult anxiety disorders over twelve weeks "
        "using validated measurement instruments throughout."
    )
    flag_types = [f.flag_type for f in r.flags]
    assert "insufficient_citations" in flag_types


def test_cartel_score_range():
    """Score must be between 0 and 1."""
    r = engine.analyze(
        "Smith (2021), Smith (2020), Smith (2019), "
        "Smith (2018), Jones (2020), Jones (2019), "
        "Smith et al. (2022), Smith (2016), Jones (2021)."
    )
    assert 0.0 <= r.cartel_score <= 1.0


def test_flag_structure():
    """Flags have correct required fields."""
    r = engine.analyze(
        "Smith (2021), Smith (2020), Smith (2019), "
        "Smith (2018), Smith (2017), Jones (2020), "
        "Jones (2019), Smith et al. (2022) findings."
    )
    if r.flags_count > 0:
        flag = r.flags[0]
        assert hasattr(flag, 'flag_type')
        assert hasattr(flag, 'severity')
        assert hasattr(flag, 'description')
        assert hasattr(flag, 'evidence')
        assert hasattr(flag, 'suggestion')


def test_authors_extracted():
    """Authors correctly extracted from citations."""
    r = engine.analyze(
        "Smith (2021), Jones (2020), Williams (2019), "
        "Brown (2018), Davis (2022), Wilson (2021), "
        "Taylor (2020), Anderson (2019) all support this."
    )
    assert len(r.authors_found) >= 1


def test_summary_contains_key_info():
    """Summary mentions citations and risk level."""
    r = engine.analyze(
        "Smith (2021), Smith (2020), Smith (2019), "
        "Smith (2018), Jones (2020), Jones (2019), "
        "Smith et al. (2022), Smith (2016) findings."
    )
    assert "Citation" in r.summary


def test_network_diversity_range():
    """Diversity score must be between 0 and 1."""
    r = engine.analyze(
        "Smith (2021), Smith (2020), Jones (2020), "
        "Jones (2019), Williams (2019), Brown (2018), "
        "Davis (2022), Wilson (2021) all confirmed."
    )
    assert 0.0 <= r.network_diversity <= 1.0