File size: 5,829 Bytes
5971541
 
 
bb6c547
5971541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc862d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5971541
 
 
 
 
 
 
 
bb6c547
 
 
 
 
 
 
 
 
 
f97653f
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import importlib.util
from pathlib import Path

from jawbreaker.analyzers import load_json_prediction, validate_prediction


def load_run_eval_module():
    spec = importlib.util.spec_from_file_location("run_eval", Path("eval/run_eval.py"))
    assert spec is not None
    assert spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def test_validate_prediction_accepts_complete_prediction() -> None:
    prediction = {
        "risk_level": "dangerous",
        "scam_type": "credential_theft",
        "summary": "This is pretending to be a bank.",
        "tactics": ["fake authority", "credential request"],
        "safest_action": "Do not click links. Open the official app directly.",
        "trusted_person_message": "Can you check this for me?",
        "scam_dna": {
            "impersonates": "bank",
            "pressure": "account locked",
            "ask": "login",
            "risk": "credential theft",
        },
    }

    assert validate_prediction(prediction) == []


def test_score_rows_tracks_dangerous_as_safe() -> None:
    run_eval = load_run_eval_module()
    rows = [
        {
            "id": "case_1",
            "category": "bank_phishing",
            "input": "Bank login now",
            "expected_risk_level": "dangerous",
            "expected_scam_type": "credential_theft",
            "expected_tactics": ["credential request"],
        }
    ]
    predictions = {
        "case_1": {
            "risk_level": "safe",
            "scam_type": "none",
            "summary": "Looks fine.",
            "tactics": [],
            "safest_action": "No action needed.",
            "trusted_person_message": "Can you check this?",
            "scam_dna": {"impersonates": "", "pressure": "", "ask": "", "risk": ""},
        }
    }

    metrics = run_eval.score_rows(rows, predictions, elapsed=0.01)

    assert metrics["risk_level_accuracy"] == 0
    assert metrics["dangerous_as_safe"] == ["case_1"]
    assert metrics["dangerous_as_needs_check"] == []
    assert metrics["suspicious_as_safe"] == []


def test_score_rows_tracks_dangerous_undercalls_and_suspicious_as_safe() -> None:
    run_eval = load_run_eval_module()
    rows = [
        {
            "id": "danger_case",
            "category": "family_impersonation",
            "input": "Grandpa, I need money before midnight.",
            "expected_risk_level": "dangerous",
            "expected_scam_type": "family_impersonation",
            "expected_tactics": ["payment pressure"],
        },
        {
            "id": "suspicious_case",
            "category": "suspicious",
            "input": "Open this marketplace escrow link.",
            "expected_risk_level": "suspicious",
            "expected_scam_type": "fake_escrow",
            "expected_tactics": ["suspicious link"],
        },
    ]
    base_prediction = {
        "scam_type": "unknown",
        "summary": "Check this.",
        "tactics": [],
        "safest_action": "Verify through a trusted route.",
        "trusted_person_message": "Can you check this?",
        "scam_dna": {"impersonates": "", "pressure": "", "ask": "", "risk": ""},
    }
    predictions = {
        "danger_case": {**base_prediction, "risk_level": "needs_check"},
        "suspicious_case": {**base_prediction, "risk_level": "safe"},
    }

    metrics = run_eval.score_rows(rows, predictions, elapsed=0.01)

    assert metrics["dangerous_as_needs_check"] == ["danger_case"]
    assert metrics["suspicious_as_safe"] == ["suspicious_case"]


def test_score_rows_tracks_model_errors() -> None:
    run_eval = load_run_eval_module()
    rows = [
        {
            "id": "case_1",
            "category": "safe_benign",
            "input": "Dentist appointment Tuesday.",
            "expected_risk_level": "safe",
            "expected_scam_type": "none",
            "expected_tactics": [],
        }
    ]
    predictions = {
        "case_1": {
            "risk_level": "safe",
            "scam_type": "none",
            "summary": "Looks fine.",
            "tactics": [],
            "safest_action": "No action needed.",
            "trusted_person_message": "Can you check this?",
            "scam_dna": {"impersonates": "", "pressure": "", "ask": "", "risk": ""},
            "_jawbreaker_model_error": "JSONDecodeError('empty')",
        }
    }

    metrics = run_eval.score_rows(rows, predictions, elapsed=0.01)

    assert metrics["model_errors"] == [{"id": "case_1", "error": "JSONDecodeError('empty')"}]


def test_has_unsafe_action_allows_do_not_send_money() -> None:
    run_eval = load_run_eval_module()

    assert not run_eval.has_unsafe_action("Do not send money. Call a known number.")
    assert run_eval.has_unsafe_action("Send money to verify the account.")


def test_load_json_prediction_extracts_embedded_object() -> None:
    prediction = load_json_prediction(
        'Here is the result: {"risk_level": "safe", "scam_type": "none", '
        '"summary": "ok", "tactics": [], "safest_action": "No action.", '
        '"trusted_person_message": "Please check.", '
        '"scam_dna": {"impersonates": "", "pressure": "", "ask": "", "risk": ""}}'
    )

    assert prediction["risk_level"] == "safe"


def test_load_json_prediction_ignores_qwen_thinking_tokens() -> None:
    prediction = load_json_prediction(
        '<think>I should reason internally.</think>{"risk_level": "dangerous", '
        '"scam_type": "family_impersonation", "summary": "scam", '
        '"tactics": ["secrecy"], "safest_action": "Do not reply.", '
        '"trusted_person_message": "Can you check this?", '
        '"scam_dna": {"impersonates": "family", "pressure": "secret", "ask": "money", "risk": "payment"}}'
    )

    assert prediction["risk_level"] == "dangerous"