"""Tests on Lab 07's recorded runs (31 October 2026, Apple M4 Max, Docker Desktop with 16 vCPU,
HAProxy 3.2): 03_health_checks.py (s2 frozen for 60 s behind four HAProxy listeners, each sent
100 req/s; then three health checks at 700 req/s of uneven work) and 04_deploys.py (s1-s4
restarted one at a time at 300 req/s, check every 1 s, fall 2).
Run: pytest test_health_practice.py"""
from health_practice import (away_times, capacity, check_can_notice, detection_range, expected_failures,
                             mean_work_ms, passive_detection, rise_range, utilization)

MIX = [(70, 5), (20, 30), (10, 100)]  # 70% of requests take 5 ms, 20% 30 ms, 10% 100 ms


def close(a, b, tol=1e-6):
    return abs(a - b) <= tol


def test_detection_range_matches_the_lab():
    # fall 3, 1 s check timeout: the lab's predicted ranges, and the measured DOWN times inside them
    assert detection_range(1, 1, 3) == (4, 6)  # measured 5.98 s
    assert detection_range(5, 1, 3) == (12, 18)  # measured 13.39 s
    lo, hi = detection_range(15, 1, 3)
    assert (lo, hi) == (32, 48)
    assert lo <= 45.2 <= hi  # measured 45.2 s


def test_the_first_formula_was_too_low():
    # The lab first predicted fall × inter + timeout: 4 s for the 1 s check. HAProxy took 5.98 s.
    lo, hi = detection_range(1, 1, 3)
    assert hi > 3 * 1 + 1
    assert lo <= 5.98 <= hi


def test_rise_range():
    assert rise_range(1, 2) == (1, 2)  # UP again 2.05 s after the thaw
    assert rise_range(5, 2) == (5, 10)  # 5.54 s
    assert rise_range(15, 2) == (15, 30)  # 15.41 s; the passive listener 19.93 s


def test_expected_failures_is_share_times_time():
    est = expected_failures(100, 4, 45.2)
    assert close(est, 1130)
    assert abs(est - 1116) / 1116 < 0.02  # measured 1,116 with the 15 s check
    assert abs(expected_failures(100, 4, 13.39) - 347) / 347 < 0.05  # 5 s check: 347
    assert close(expected_failures(100, 4, 2.3), 57.5)  # passive: 53 measured


def test_passive_detection():
    # 2 s timeout server, 5 errors in a row, s2's share 25 req/s: about 2.2 s; HAProxy said 2.3 s
    t = passive_detection(2, 5, 25)
    assert close(t, 2.2)
    assert abs(t - 2.3) < 0.15


def test_capacity_and_utilization():
    assert close(mean_work_ms(MIX), 19.5)
    four = capacity(4, 4, MIX)
    three = capacity(3, 4, MIX)
    assert round(four) == 821 and round(three) == 615
    assert round(utilization(700, 4, 4, MIX), 2) == 0.85  # the lab's "85% load"
    assert utilization(700, 3, 4, MIX) > 1  # one table closed: more arrives than three can serve


def test_away_times_from_the_deploy_log():
    # kill, round 1, from 04_deploys.json (seconds from the start of the deploy)
    steps = [
        {"server": "s1", "stop": 0.0, "started": 0.755},
        {"server": "s2", "stop": 1.762, "started": 2.389},
        {"server": "s3", "stop": 3.391, "started": 4.039},
        {"server": "s4", "stop": 5.045, "started": 5.680},
    ]
    away = away_times(steps)
    assert [round(a, 3) for a in away] == [0.755, 0.627, 0.648, 0.635]


def test_a_restart_is_too_short_for_the_check():
    # Every server in every deploy was away 0.54-0.93 s. Check every 1 s, fall 2: never noticed.
    for away in (0.545, 0.755, 0.925):
        assert not check_can_notice(away, 1, 2)
    assert check_can_notice(1.5, 1, 2)
    # A longer restart, or fall 1, would have been noticed
    assert check_can_notice(0.6, 1, 1)
