"""Tests on Lab 04's recorded results (eval-ai-labs, 04_stratified_sampling; a simulated support
assistant: 100,000 requests, normal / difficult / ambiguous / critical = 90 / 5 / 4 / 1% of traffic,
true pass rates 96 / 85 / 80 / 70%, failure costs x1 / x2 / x2 / x50).
Run: pytest test_strata_practice.py"""
from strata_practice import (chance_at_least, chance_of_none, clean_cases_needed, harm_per_1000,
                             random_cases_needed, raw_score, verdict, weighted_score, wilson_ci,
                             zero_failure_upper)

SHARES = {"normal": 0.90, "difficult": 0.05, "ambiguous": 0.04, "critical": 0.01}
TRUE_PASS = {"normal": 0.96, "difficult": 0.85, "ambiguous": 0.80, "critical": 0.70}
COST = {"normal": 1, "difficult": 2, "ambiguous": 2, "critical": 50}
# Script 3, Exercise 1: one evaluation with 90 / 20 / 20 / 20 cases
ONE = {"normal": (87, 90), "difficult": (17, 20), "ambiguous": (17, 20), "critical": (15, 20)}


def test_random_sets_miss_the_critical_group():
    # 100 random cases: no critical case 36.6% of the time (the lab simulated 345 of 1,000)
    assert round(chance_of_none(100, 0.01), 3) == 0.366
    # 150 random cases: none about 1 time in 5
    assert round(chance_of_none(150, 0.01), 3) == 0.221
    # 2,000 random cases: at least 20 critical only about half the time (median 20)
    assert 0.5 < chance_at_least(2000, 0.01, 20) < 0.6


def test_random_cases_needed():
    # Script 1, Exercise 3: >= 20 critical cases, 95% of the time: 2,784 random cases
    n = random_cases_needed(0.01, 20)
    assert n == 2784
    assert n - round(n * 0.01) == 2756  # not critical
    assert chance_at_least(n - 1, 0.01, 20) < 0.95 <= chance_at_least(n, 0.01, 20)


def test_wilson_ranges_for_the_critical_group():
    lo, hi = wilson_ci(1, 1)  # the typical random 150: one critical case
    assert (round(lo, 2), hi) == (0.21, 1.0) and round((hi - lo) * 100) == 79
    lo, hi = wilson_ci(14, 20)  # a floor of 20, at the true 70%
    assert (round(lo, 2), round(hi, 2)) == (0.48, 0.85) and round((hi - lo) * 100) == 37
    lo, hi = wilson_ci(63, 90)  # risk-based: 90 critical cases
    assert (round(lo, 2), round(hi, 2)) == (0.60, 0.78) and round((hi - lo) * 100) == 19
    assert wilson_ci(0, 0) == (0.0, 1.0)


def test_raw_vs_weighted():
    assert round(raw_score(ONE), 3) == 0.907  # 136 of 150
    assert round(weighted_score(ONE, SHARES), 3) == 0.954
    truth = weighted_score({g: (round(TRUE_PASS[g] * 1000), 1000) for g in SHARES}, SHARES)
    assert round(truth, 4) == 0.9455
    assert abs(weighted_score(ONE, SHARES) - truth) < 0.01  # off by less than a point
    assert round((truth - raw_score(ONE)) * 100) == 4  # raw: off by about 4


def test_zero_failures():
    assert round(zero_failure_upper(20), 3) == 0.139  # 20 clean: still up to 13.9%
    assert round(zero_failure_upper(100), 3) == 0.030
    assert clean_cases_needed(0.01) == 299  # "fails under 1%"
    assert clean_cases_needed(0.001) == 2995
    assert clean_cases_needed(0.10) == 29


def test_release_verdicts():
    targets = {"normal": 0.95, "difficult": 0.80, "ambiguous": 0.75, "critical": 0.90}
    got = {g: verdict(*wilson_ci(k, n), targets[g]) for g, (k, n) in ONE.items()}
    assert got == {"normal": "can't tell", "difficult": "can't tell", "ambiguous": "can't tell",
                   "critical": "fails"}  # critical 53-89%: all below 90%
    assert verdict(0.91, 0.99, 0.90) == "meets"


def test_where_the_harm_is():
    no_cost = harm_per_1000(SHARES, TRUE_PASS, {g: 1 for g in SHARES})  # plain failures
    assert [round(no_cost[g], 1) for g in SHARES] == [36.0, 7.5, 8.0, 3.0]
    assert round(sum(no_cost.values()), 1) == 54.5
    harm = harm_per_1000(SHARES, TRUE_PASS, COST)
    assert [round(harm[g]) for g in SHARES] == [36, 15, 16, 150]
    assert round(sum(harm.values())) == 217
    assert round(harm["critical"] / sum(harm.values()), 2) == 0.69  # 1% of traffic, 69% of harm
