"""Tests on the capstone's recorded run (October 2026): results/summary.json for the 250 test PRs and
heldout/results/pipeline/summary.json for the 99 held-out PRs, as in report/TABLES.md (T1, T2, T7, T8,
T11, T13). Every "person" is an AI stand-in; no model was called. Run: pytest test_pipeline_practice.py"""
from pipeline_practice import caught, choose, per_100, per_quarter, person_minutes

# T1, the 250 PRs: person minutes a PR, bugs through per 100 PRs (1 PR in 10 buggy), serious bugs of 38.
MAIN = {
    "humans": {"minutes": 18.25, "per_100": 1.0405, "serious": 2.0},
    "humans_checks": {"minutes": 18.53, "per_100": 0.9249, "serious": 2.0},
    "ai_only": {"minutes": 1.26, "per_100": 1.7919, "serious": 6.0},
    "pipeline_no_checks": {"minutes": 14.70, "per_100": 0.9827, "serious": 3.0},
    "pipeline": {"minutes": 14.42, "per_100": 1.2277, "serious": 3.72},
    "pipeline_split": {"minutes": 14.83, "per_100": 1.1699, "serious": 3.72},
    "ai_plus_one": {"minutes": 13.60, "per_100": 0.7514, "serious": 2.0},
}

# T13, the 99 held-out PRs: serious bugs of 15.
HELD = {
    "humans": {"minutes": 23.60, "per_100": 1.1594, "serious": 1.0},
    "humans_checks": {"minutes": 24.13, "per_100": 1.1594, "serious": 1.0},
    "ai_only": {"minutes": 1.72, "per_100": 1.5942, "serious": 3.0},
    "pipeline_no_checks": {"minutes": 22.51, "per_100": 0.8696, "serious": 1.0},
    "pipeline": {"minutes": 22.17, "per_100": 1.0319, "serious": 1.24},
    "pipeline_split": {"minutes": 22.17, "per_100": 1.0319, "serious": 1.24},
    "ai_plus_one": {"minutes": 15.69, "per_100": 0.8696, "serious": 1.0},
}


def close(a, b, tol=0.005):
    return abs(a - b) <= tol


def test_caught_by_a_check_or_a_person():
    assert caught(True, False, 0) == 1.0
    assert caught(False, True, 0) == 1.0
    assert caught(False, False, 0) == 0.0


def test_caught_through_the_finding_check():
    # "Proven" keeps 69 of 75 true findings (92%). One finding: 0.92. Two: almost always one survives.
    assert close(caught(False, False, 1, keep=69 / 75), 0.92)
    assert close(caught(False, False, 2, keep=69 / 75), 0.9936, 1e-4)
    assert caught(False, False, 1) == 1.0  # no check: the finding reaches whoever reads


def test_caught_when_only_the_author_reads():
    # AI only: nobody reads; authors who act on half of the correct findings.
    assert close(caught(False, False, 1, act=0.5), 0.5)
    assert close(caught(False, False, 1, keep=0.92, act=0.5), 0.46)


def test_person_minutes():
    assert person_minutes(100, "auto") == 0.0
    assert person_minutes(100, "quick") == 3.0
    assert close(person_minutes(100, "full"), 12.0)        # 100 lines at 500 an hour
    assert close(person_minutes(20, "full"), 5.0)          # at least 5 minutes
    assert close(person_minutes(100, "two"), 24.0)         # two people, twice a full read
    assert close(person_minutes(100, "full", wrong_comments=1.5), 15.0)


def test_per_100_matches_t1():
    # People read everything let 18 of 173 bugs through, the pick 13, AI only 31.
    assert close(per_100(18), 1.04)
    assert close(per_100(13), 0.75)
    assert close(per_100(31), 1.79)
    # Serious bugs on the same scale: 2 of the 173 buggy PRs -> 0.12 per 100.
    assert close(per_100(2), 0.12)
    # At 1 PR in 5 buggy (T8): 1.50 for the pick.
    assert close(per_100(13, prevalence=0.20), 1.50)


def test_the_pick_on_the_250():
    assert choose(MAIN) == "ai_plus_one"


def test_without_the_serious_rule_nobody_reads():
    # T8: escapes alone, and AI only wins at 1.3 minutes a PR.
    assert choose(MAIN, serious_ref=None) == "ai_only"


def test_reading_the_ai_comments_first_flips_the_pick():
    # T11: the careful stand-in reading the AI's comments first (Chapter 10). Only the setups where a person
    # sees AI comments change; AI only and people + checks don't, and the pipeline only gets worse.
    lean = dict(MAIN)
    lean["ai_plus_one"] = {"minutes": 13.60, "per_100": 1.0405, "serious": 5.0}
    lean["pipeline"] = {"minutes": 14.42, "per_100": 1.3480, "serious": 4.72}
    del lean["pipeline_no_checks"], lean["pipeline_split"]
    assert choose(lean) == "humans"


def test_the_pick_holds_on_held_out_prs():
    assert choose(HELD) == "ai_plus_one"
    assert choose(HELD, serious_ref=None) == "ai_only"


def test_nothing_passes_an_impossible_budget():
    assert choose(MAIN, max_per_100=0.5) is None


def test_per_quarter_matches_t7():
    # 60 PRs a week, 13 weeks: 780 PRs.
    assert close(per_quarter(MAIN["humans"]["per_100"]), 8.1, 0.05)
    assert close(per_quarter(MAIN["ai_plus_one"]["per_100"]), 5.9, 0.05)
