"""Tests on Lab 03's recorded runs (October 2026, Apple M4 Max, Docker Desktop): the knee table
from 01_queue_sim.py, the one-worker run at 86 req/s from 02_real_vs_model.py, and the capacity
plan from 05_capacity_plan.py.
Run: pytest test_queue_practice.py"""
import math

from queue_practice import (erlang_c, max_rate_for_p99, mm1_mean, mmc_mean, mmc_p99, utilization,
                            workers_needed)


def test_utilization():
    assert round(utilization(50, 0.010), 3) == 0.5  # 50 req/s x 10 ms: half busy
    assert round(utilization(85.78, 0.010), 2) == 0.86  # what the asked 10 ms said
    assert 0.95 < utilization(85.78, 0.011148) < 0.96  # the measured 11.1 ms: about 95% busy
    assert round(utilization(600, 0.01042, 7), 2) == 0.89  # the plan's minimum


def test_the_knee():
    # time in the system, in service times, at 50/80/90/95/99% busy: 2, 5, 10, 20, 100
    for rho, want in [(0.5, 2), (0.8, 5), (0.9, 10), (0.95, 20), (0.99, 100)]:
        assert round(mm1_mean(rho, 1.0), 6) == want
    try:
        mm1_mean(1.0, 1.0)
    except ValueError:
        pass
    else:
        raise AssertionError("100% busy has no steady state: raise ValueError")


def test_the_formula_that_missed():
    # one worker at 85.78 req/s: the asked 10 ms predicts 70.3 ms; the real mean was 203.5 ms
    assert round(1000 * mm1_mean(85.78, 100), 1) == 70.3
    # fed the run's own measured work (11.148 ms) it predicts 255 ms
    assert round(1000 * mm1_mean(85.78, 1 / 0.011148)) == 255


def test_erlang_c_and_four_workers():
    assert round(erlang_c(0.5, 1, 1), 6) == 0.5  # one worker: P(wait) = utilization
    assert round(erlang_c(3.2, 1, 4), 2) == 0.60  # four workers at 80%: 60% of arrivals wait
    assert round(mmc_mean(3.2, 1, 4), 4) == 1.7455  # four workers sharing one line at 80%
    assert round(mmc_mean(3.8, 1, 4), 2) == 5.46  # ... and at 95%
    assert round(mmc_mean(0.8, 1, 1), 6) == 5.0  # c = 1 is the one-worker queue


def test_p99():
    assert round(mmc_p99(0.8, 1, 1), 2) == 23.03  # one worker at 80%: ln(100) x 5
    assert round(mmc_p99(0.8, 1, 1) / mm1_mean(0.8, 1), 3) == round(math.log(100), 3)
    assert round(mmc_p99(3.2, 1, 4), 2) == 6.89  # four workers at 80%


MU = 1 / 0.01041775  # the work per request measured at today's peak: 10.42 ms, 96 a second


def test_the_plan():
    assert round(600 / MU, 2) == 6.25  # workers' worth of work at 2x peak
    assert round(1000 * mmc_p99(600, MU, 7)) == 77
    assert round(1000 * mmc_p99(600, MU, 9)) == 49
    assert workers_needed(600, MU, 0.100) == 7  # the fewest that meet p99 <= 100 ms
    assert workers_needed(600, MU, 0.100, max_utilization=0.7) == 9  # with 30% spare
    assert workers_needed(300, MU, 0.100) == 4  # today's peak, for comparison


def test_where_the_plan_breaks():
    edge = max_rate_for_p99(MU, 9, 0.100)
    assert round(edge) == 812
    assert round(edge / 600 - 1, 2) == 0.35  # 35% more traffic than planned
    assert round(1000 * mmc_p99(edge, MU, 9)) == 100
