"""Tests on Lab 05's recorded simulation (eval-ai-labs, 05_saturation): one random review of the
simulated support assistant (rng 0, the review Scripts 1 and 2 read), up to case 2,223, where the
"20 failures with nothing new" rule fired. 28 failure types exist; the review shows 24 of them by then.
Run: pytest test_saturation_practice.py"""
import math

from saturation_practice import (chao1, chao1_interval, discovery_curve, frequencies, good_turing_coverage,
                                 labels_from_failures, saturation_point, unseen_shares)

# Every failure in the first 2,223 cases, as "position:type". All other cases passed.
REVIEW = """
6:N03 25:N04 28:C01 46:N03 85:N12 94:N02 102:N01 104:C02 119:N02 174:D01 178:N04 186:N01 203:N01
221:N01 252:N06 262:N03 267:A03 268:N02 278:N04 401:D04 408:N06 494:N01 551:N01 557:A01 620:N02
630:N01 643:N07 663:N01 672:N02 673:D02 682:N10 690:N06 703:D01 727:N02 784:A04 801:N07 822:D04
838:A01 891:A01 894:N06 907:N02 934:D04 951:N05 969:A01 972:N02 979:N02 985:N01 989:D05 1000:A01
1013:A01 1032:N01 1042:N01 1069:D02 1089:D01 1099:N02 1114:N01 1115:N10 1143:N05 1171:N02 1189:C05
1237:A01 1240:A03 1241:N02 1246:N07 1265:N06 1285:A01 1290:D05 1303:N01 1328:N04 1362:D03 1375:N03
1380:A01 1424:N01 1426:N01 1456:D04 1462:N04 1472:N02 1498:N01 1517:N08 1523:D01 1548:N02 1550:D03
1552:N07 1556:A02 1567:D01 1589:N01 1601:N01 1627:C01 1636:N03 1638:D03 1659:N04 1673:D05 1720:N04
1730:N09 1735:D02 1738:N01 1766:A03 1793:D01 1807:A03 1808:N04 1816:C05 1818:N02 1855:D02 1864:N12
1879:N02 1907:C04 1908:A01 1950:N06 1993:N03 2004:N07 2026:N01 2030:D01 2031:N01 2037:N01 2052:N01
2074:N07 2110:N01 2124:D04 2129:N04 2137:N01 2184:N07 2189:D02 2193:D02 2213:N04 2219:N04 2223:N02
"""

# Each type's design share of all requests (stratum share x stratum failure rate x the type's weight),
# and Lab 04's cost per failure: normal 1, difficult and ambiguous 2, critical 50.
SHARE = {"N01": 0.009, "N02": 0.00648, "N03": 0.00504, "N04": 0.00432, "N05": 0.00288, "N06": 0.00252,
         "N07": 0.0018, "N08": 0.00144, "N09": 0.00108, "N10": 0.00072, "N11": 0.00054, "N12": 0.00036,
         "N13": 0.00018, "D01": 0.00225, "D02": 0.001875, "D03": 0.0015, "D04": 0.001125, "D05": 0.00075,
         "A01": 0.0036, "A02": 0.002, "A03": 0.0016, "A04": 0.0008, "C01": 0.00105, "C02": 0.00075,
         "C03": 0.0006, "C04": 0.0003, "C05": 0.00021, "C06": 0.00009}
COST = {m: {"N": 1, "D": 2, "A": 2, "C": 50}[m[0]] for m in SHARE}


def review(n):
    return labels_from_failures(REVIEW, n)


def test_discovery_curve():
    curve = discovery_curve(review(2223))
    assert len(curve) == 2223
    assert curve[99] == 5  # the first 100 cases: 6 failures of 5 types
    assert curve[499] == 11 and curve[999] == 18 and curve[1999] == 24  # climbs fast, then crawls
    assert curve[5] == 1 and curve[4] == 0  # the first failure is case 6
    assert all(b - a in (0, 1) for a, b in zip(curve, curve[1:]))


def test_frequencies():
    assert frequencies(review(1000)) == {9: 2, 5: 1, 4: 1, 3: 3, 2: 2, 1: 9}  # 18 types, 9 of them seen once
    assert frequencies([None, None]) == {}
    assert frequencies(["a", None, "b", "a"]) == {2: 1, 1: 1}


def test_chao1_at_1000():
    c = chao1(review(1000))
    assert (c["observed"], c["f1"], c["f2"]) == (18, 9, 2)
    assert c["estimate"] == 30  # 18 + 9 x 8 / (2 x 3): the truth is 28
    lo, hi = chao1_interval(c)
    assert (round(lo), round(hi)) == (21, 72)


def test_chao1_later():
    c = chao1(review(500))
    assert (c["observed"], c["f1"], c["f2"], c["estimate"]) == (11, 6, 1, 18.5)  # runs low here
    c = chao1(review(2000))
    assert (c["observed"], c["f1"], c["f2"], c["estimate"]) == (24, 6, 5, 26.5)
    assert chao1(["a", "a", "b", "b"])["estimate"] == 2  # no singletons: nothing hints at more


def test_good_turing():
    assert round(good_turing_coverage(review(1000)), 3) == 0.816  # 1 - 9/49; the truth was 86%
    assert round(good_turing_coverage(review(500)), 3) == 0.727  # 1 - 6/22
    assert math.isnan(good_turing_coverage([None, None]))


def test_stopping_rule():
    labels = review(2223)
    assert saturation_point(labels, 20) == 2223  # fired on case 2,223, with 24 of 28 types found
    assert saturation_point(labels, 50) is None  # not within these cases
    assert saturation_point(["a", None, "a", "b", "b", None, "b"], 2) == 7
    assert saturation_point(["a", "a", "b", "a"], 2) is None  # "b" was new: the streak restarted


def test_rare_is_not_harmless():
    seen = {x for x in review(1000) if x}
    fail, cost = unseen_shares(seen, SHARE, COST)
    assert round(fail, 3) == 0.145 and round(cost, 3) == 0.323  # this review at 1,000 cases
    seen = {x for x in review(2000) if x}
    fail, cost = unseen_shares(seen, SHARE, COST)
    assert round(fail, 3) == 0.026 and round(cost, 3) == 0.162  # cost share over 6x the failure share
    assert unseen_shares(set(SHARE), SHARE, COST) == (0.0, 0.0)
