"""Tests on Lab 02's recorded numbers (eval-ai-labs, 02_how_many_evals): the simulated 90% system of
01_shrinking_interval.py, the edge cases and coverage of 02_when_wald_breaks.py, the made-up changelog in
data/changelog.jsonl (05_your_results.py Exercise 3), and the video's simulated 80% system on 30 cases.
Run: pytest test_howmany_practice.py"""
from howmany_practice import (cases_for_margin, exact_coverage, margin_for_n, newcombe_diff_ci,
                              p_retest_beats, verdict, wald_ci, wilson_ci)


def r(x, d=3):
    return round(x, d)


def test_margin_and_the_square_root_law():
    # the lab's table at p = 90%: 20 cases ±13.1%, 100 cases ±5.9%, 1,000 cases ±1.9%
    assert r(margin_for_n(0.9, 20)) == 0.131
    assert r(margin_for_n(0.9, 100)) == 0.059
    assert r(margin_for_n(0.9, 1000), 4) == 0.0186
    # four times the cases, half the margin: 30 cases ±14.3% at 80%, 120 cases ±7.2%
    assert r(margin_for_n(0.8, 30)) == 0.143
    assert abs(margin_for_n(0.8, 120) / margin_for_n(0.8, 30) - 0.5) < 1e-12


def test_wald_breaks_at_the_edges():
    assert wald_ci(20, 20) == (1.0, 1.0)  # "100% ± 0"
    assert wald_ci(0, 20) == (0.0, 0.0)  # "0 harmful outputs, ± 0"
    assert r(wald_ci(19, 20)[1]) == 1.046  # runs past 100%
    assert r(wald_ci(1, 20)[0]) == -0.046  # starts below 0%
    assert tuple(r(x) for x in wald_ci(82, 100)) == (0.745, 0.895)


def test_wilson():
    assert tuple(r(x) for x in wilson_ci(82, 100)) == (0.733, 0.883)  # the unlucky draw: misses 90%
    assert tuple(r(x) for x in wilson_ci(20, 20)) == (0.839, 1.0)  # a perfect score isn't proof
    assert tuple(r(x) for x in wilson_ci(0, 20)) == (0.0, 0.161)  # 0 harmful of 20: up to 16.1%
    assert tuple(r(x, 2) for x in wilson_ci(22, 30)) == (0.56, 0.86)  # the first re-test
    assert tuple(r(x, 2) for x in wilson_ci(24, 30)) == (0.63, 0.90)  # document cutting, before
    assert tuple(r(x, 2) for x in wilson_ci(27, 30)) == (0.74, 0.97)  # and after
    assert wilson_ci(0, 0) == (0.0, 1.0)


def test_the_changelog():
    # (before, after, n) from data/changelog.jsonl; ranges in points as 05_your_results.py prints them
    log = [
        (41, 44, 50, (-8.4, 20.3), "could be noise"),     # "think step by step"
        (158, 171, 200, (-1.0, 14.0), "could be noise"),  # newer small model
        (24, 27, 30, (-8.8, 28.5), "could be noise"),     # new way to cut documents
        (420, 447, 500, (1.2, 9.6), "bigger than noise"),  # rewrote the instructions
        (81, 88, 100, (-3.2, 17.1), "could be noise"),    # 3 worked examples
        (83, 85, 100, (-8.3, 12.3), "could be noise"),    # less random answers
        (300, 336, 400, (3.4, 14.5), "bigger than noise"),  # re-sorting step
    ]
    for a, b, n, want, v in log:
        lo, hi = newcombe_diff_ci(a, n, b, n)
        assert (round(100 * lo, 1), round(100 * hi, 1)) == want, (a, b, n)
        assert verdict(lo, hi) == v
    assert verdict(-0.2, -0.01) == "worse"


def test_cases_for_a_margin():
    # ±8% / ±4% / ±2% / ±1% at 90%: about ×4 each time
    assert [cases_for_margin(m, 0.9) for m in (0.08, 0.04, 0.02, 0.01)] == [55, 217, 865, 3458]


def test_coverage():
    # how often "95%" really contains the truth, at 20 cases
    assert r(exact_coverage(wald_ci, 20, 0.99), 2) == 0.18
    assert r(exact_coverage(wilson_ci, 20, 0.99), 2) == 0.98
    assert r(exact_coverage(wald_ci, 20, 0.95), 2) == 0.64
    # the video's 80% system on 30 cases: Wilson catches the truth 96% of the time
    assert r(exact_coverage(wilson_ci, 30, 0.8), 2) == 0.96


def test_noise_alone():
    # an unchanged 80% system, re-tested on 30 fresh cases, beats its first score by 3+ cases (10+ points)
    assert r(p_retest_beats(30, 0.8, 3)) == 0.209
    # beating it by 1 or more is about as likely as not doing so (ties take the rest)
    assert 0.4 < p_retest_beats(30, 0.8, 1) < 0.5
