"""Tests on Lab 07's recorded numbers (eval-ai-labs, labs/07_decision_engine): the example goal from
01_design.py (about 90% expected, ±2 points, 100,000 requests, groups 90/5/4/1 with a critical 1%,
5 runs a case, catch a 2-point drop), and 02_audit.py's audit of Lab 01's simulated store set.
Run: pytest test_design_practice.py"""
from design_practice import (binding_constraint, design_cost, disagreement, n_for_margin, n_for_zero_failures,
                             n_paired, total_variation, verdict, wilson_margin, word_overlap)


def test_margin_alone():
    assert n_for_margin(0.90, 0.02) == 865  # before the finite-population correction
    assert n_for_margin(0.90, 0.02, population=100_000) == 857
    assert n_for_margin(0.90, 0.03, population=100_000) == 383  # loosened to ±3
    assert n_for_margin(0.90, 0.01, population=100_000) == 3342  # tightened to ±1
    assert n_for_margin(0.87, 0.03) == 483  # the store's next design


def test_critical_claim():
    assert n_for_zero_failures(0.05) == 59  # "fails < 5%"
    assert n_for_zero_failures(0.01) == 299  # "fails < 1%"
    assert round(0.01 * 857) == 9  # the critical group's share of traffic would give only 9


def test_regression_checks():
    a_only, b_only = disagreement(0.90, 0.88, 0.6)
    assert round(a_only + b_only, 3) == 0.079  # versions disagree on about 8% of cases
    assert n_paired(a_only, b_only) == 1549


def test_binding_constraint():
    assert binding_constraint({"margin": 857, "critical claim": 59, "regression checks": 1549}) == ("regression checks", 1549)
    # loosening the margin doesn't change which one binds
    assert binding_constraint({"margin": 383, "critical claim": 59, "regression checks": 1549})[0] == "regression checks"
    # tightening it does
    assert binding_constraint({"margin": 3342, "critical claim": 59, "regression checks": 1549})[0] == "margin"


def test_cost():
    dollars, hours = design_cost(1551, 5)  # the design: 1,551 cases, 7,755 runs
    assert round(dollars) == 1598 and round(hours) == 39
    assert round(design_cost(1556, 5)[0]) == 1603  # ±3: five dollars MORE, from rounding
    assert round(design_cost(907, 5)[0]) == 934  # no regression checks
    assert round(1 - design_cost(907, 5)[0] / dollars, 3) == 0.415  # about 42% cheaper
    assert round(design_cost(3368, 5)[0]) == 3469 and round(design_cost(3368, 5)[1]) == 84  # ±1
    assert round(design_cost(1551, 1)[0]) == 1560  # 1 run instead of 5: labels are the cost


def test_store_sample_size():
    assert round(wilson_margin(83, 100), 3) == 0.073  # 83 of 100 passed: ±7.3 points
    assert round(wilson_margin(41, 59), 2) == 0.11  # the critical group's 59 cases at 70%: about ±11


def test_store_mix():
    eval_set = {"easy": 0.40, "medium": 0.40, "hard": 0.20}
    production = {"easy": 0.645, "medium": 0.2125, "hard": 0.1425}
    assert round(total_variation(eval_set, production), 3) == 0.245  # printed as TVD 0.25
    topics_set = {"account": 0.18, "billing": 0.24, "product": 0.18, "returns": 0.18, "shipping": 0.22}
    topics_prod = {"account": 0.1525, "billing": 0.195, "product": 0.14, "returns": 0.1325, "shipping": 0.25,
                   "subscription": 0.13}
    assert round(total_variation(topics_set, topics_prod), 2) == 0.16


def test_duplicates():
    # Lab 01's pair-03: the same question, reworded. No words in common.
    assert word_overlap("What is your return window?", "How many days do I have to send something back?") == 0.0
    # a copy with a different order number: digits are masked, so it's the same words
    assert word_overlap("Where is order #55120?", "Where is order #77311?") == 1.0
    assert word_overlap("", "") == 1.0


def test_verdict():
    store = ["⚠", "⚠", "✓", "✓", "✗", "?", "?", "⚠", "⚠"]  # the store's nine lines, in order
    assert verdict(store) == "blocked"
    assert verdict(["⚠", "?", "⚠", "✓"]) == "caveats"
    assert verdict(["✓", "?", "✓"]) == "unchecked"  # "?" is not the same as fine
    assert verdict(["✓"] * 9) == "clean"  # the clean synthetic set
