Spaces:
Sleeping

File size: 2,153 Bytes
6d50eaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Analyzer-accuracy gates.

Measures the AST analyzer's precision/recall against the annotated
``sample_buggy_code.py`` ground truth and the ``sample_clean_code.py``
false-positive control.  Pure Python — no models, no network.
"""

from __future__ import annotations

from pathlib import Path

import pytest

from intellicode.analysis import CodeAnalyzer
from intellicode.evaluation import (
    ANALYZER_TARGET_CLASSES,
    evaluate_analyzer,
    parse_expect_markers,
)

pytestmark = pytest.mark.eval

_FIXTURES = Path(__file__).resolve().parents[1] / "fixtures"
BUGGY_FIXTURE = _FIXTURES / "sample_buggy_code.py"
CLEAN_FIXTURE = _FIXTURES / "sample_clean_code.py"


def test_analyzer_f1_meets_baseline(baselines):
    """Analyzer F1 over the ground-truth fixture must meet the pinned floor."""
    metrics = evaluate_analyzer(CodeAnalyzer(), BUGGY_FIXTURE, CLEAN_FIXTURE)
    floor = baselines["analyzer"]

    assert metrics.f1 >= floor["f1"], f"F1 {metrics.f1:.3f} < baseline {floor['f1']}"
    assert metrics.false_positives_on_clean <= floor["max_false_positives_on_clean"], (
        f"{metrics.false_positives_on_clean} false positive(s) on clean code"
    )


def test_no_false_positives_on_clean_code():
    """Well-written code should produce zero target-class findings."""
    detections = CodeAnalyzer().analyze_file(str(CLEAN_FIXTURE)).issues
    target_hits = [i for i in detections if i.type in ANALYZER_TARGET_CLASSES]
    assert target_hits == [], f"Unexpected findings on clean code: {target_hits}"


def test_every_marked_pattern_is_detected():
    """Recall must be perfect on the annotated fixture (no missed patterns)."""
    metrics = evaluate_analyzer(CodeAnalyzer(), BUGGY_FIXTURE, CLEAN_FIXTURE)
    assert metrics.false_negatives == 0, f"Missed {metrics.false_negatives} pattern(s)"


def test_ground_truth_covers_target_classes():
    """The fixture should exercise a broad set of target anti-patterns."""
    marked_types = {t for _, t in parse_expect_markers(BUGGY_FIXTURE)}
    covered = marked_types & ANALYZER_TARGET_CLASSES
    assert len(covered) >= 8, f"Only {len(covered)} target classes exercised: {covered}"