"""Tests on Lab 02's recorded runs of the stand-in reviewer (an AI model at three budgets, October 2026):
bug c20-d2 and two real comments on it, the scorecard on all 250 PRs, intervals on the 40-PR sample, and
whether each run wrote any bug comment on each sample PR. Run: pytest test_scoring_practice.py"""
from scoring_practice import flips, points_at, scorecard, wilson

# c20-d2: the per-sync batch size is never loaded, and its test was loosened to hide it.
C20D2 = {"worker/harbor_worker/models.py": [32], "worker/tests/test_batch_size.py": [16, 19, 20]}


def test_points_at_the_bug_lines():
    # careful, run 1: on the bug's line (and the judge said it names the real cause)
    assert points_at("worker/harbor_worker/models.py", 32, C20D2)
    # quick, run 1: on one of the test's lines too, so it's a candidate (the judge then said no)
    assert points_at("worker/tests/test_batch_size.py", 19, C20D2)
    # within 3 lines counts; 4 away doesn't
    assert points_at("worker/harbor_worker/models.py", 35, C20D2)
    assert not points_at("worker/harbor_worker/models.py", 36, C20D2)
    assert points_at("worker/tests/test_batch_size.py", 23, C20D2)
    assert not points_at("worker/tests/test_batch_size.py", 24, C20D2)


def test_points_at_other_files_and_missing_lines():
    # c20-d1's bug is in destinations/__init__.py:42, a file c20-d2's bug doesn't touch
    assert not points_at("worker/harbor_worker/destinations/__init__.py", 42, C20D2)
    assert points_at("worker/harbor_worker/models.py", None, C20D2)  # a whole-file comment
    assert points_at(None, None, C20D2)  # a whole-PR comment


def _prs(found, buggy, flagged, clean):
    return (
        [{"clean": False, "found": i < found, "unmatched_bugs": 0} for i in range(buggy)]
        + [{"clean": True, "found": False, "unmatched_bugs": 1 if i < flagged else 0} for i in range(clean)]
    )


def test_scorecard_on_all_250_prs():
    # run 1 on 173 planted bugs and 77 clean PRs
    for found, flagged, recall, fa in [(114, 30, 66, 39), (138, 37, 80, 48), (152, 45, 88, 58)]:
        s = scorecard(_prs(found, 173, flagged, 77))
        assert round(s["recall"] * 100) == recall
        assert round(s["false_alarms"] * 100) == fa


def test_unmatched_comments_on_buggy_prs_are_not_false_alarms():
    prs = [{"clean": False, "found": True, "unmatched_bugs": 3}, {"clean": True, "found": False, "unmatched_bugs": 0}]
    assert scorecard(prs) == {"recall": 1.0, "false_alarms": 0.0}


def test_wilson_on_the_sample():
    pct = lambda lo_hi: tuple(round(x * 100) for x in lo_hi)
    assert pct(wilson(26, 30)) == (70, 95)  # careful, run 1, on the sample's 30 bugs
    assert pct(wilson(18, 30)) == (42, 75)  # quick, run 1: about ±17 points
    assert pct(wilson(4, 7)) == (25, 84)    # 4 of 7 on L PRs says almost nothing
    lo, hi = wilson(0, 10)
    assert lo == 0.0 and 0 < hi < 1


# The 40 sample PRs, in sitting order. Each string: did runs 1, 2, 3 write at least one bug comment?
QUICK = ["111", "101", "111", "011", "110", "111", "111", "010", "111", "111", "011", "000", "000", "111", "111",
         "111", "110", "111", "101", "010", "111", "000", "111", "000", "110", "010", "111", "111", "101", "010",
         "111", "111", "111", "101", "010", "111", "000", "111", "010", "100"]
STANDARD = ["111", "111", "111", "110", "111", "111", "111", "111", "010", "111", "111", "000", "001", "111", "111",
            "101", "111", "100", "111", "111", "111", "000", "111", "110", "111", "110", "111", "001", "001", "001",
            "111", "111", "111", "111", "100", "111", "000", "111", "111", "000"]
CAREFUL = ["111", "011", "111", "111", "111", "111", "111", "111", "000", "111", "111", "000", "000", "111", "111",
           "101", "111", "101", "111", "111", "111", "000", "111", "111", "111", "111", "111", "111", "111", "111",
           "111", "111", "111", "111", "111", "111", "111", "111", "111", "100"]


def test_run_to_run_flips():
    assert flips(QUICK) == 16
    assert flips(STANDARD) == 11
    assert flips(CAREFUL) == 4
    assert flips(["000", "111"]) == 0
