"""Tests on Lab 02's recorded runs (October 2026, Apple M4 Max, one uvicorn worker, Postgres 18):
the per-second p99s of the open-loop test on the freezing endpoint, the fan-out run and the
A/A test. Run: pytest test_tail_practice.py"""
from tail_practice import (beats_noise, mean_of_percentiles, merge, p_any_slow, percentile,
                           percentile_from_tally, share_with_slow, single_percentile_for_slowest,
                           spread, tally)

QUIET, FROZEN = 1.0, 1000.0  # ms, the made-up example
EDGES = [1, 2, 5, 10, 100, 1000, 2000]  # ms, the ranges' upper bounds

# The real test: each one-second window's own p99, ms (02_hdr_histogram.py, Exercise 2)
PER_SECOND_P99 = [
    7.699, 3.845, 1031.167, 513.791, 2.763, 3.159, 1.99, 2.026, 1.929, 2.111, 3.187, 2.225,
    1001.471, 480.767, 5.739, 2.153, 5.491, 7.071, 3.559, 2.205, 2.935, 2.541, 1007.615, 487.423,
    85.311, 2.141, 2.915, 5.715, 1.913, 1.709, 8.007, 2.031, 995.839, 484.607, 2.065, 2.069,
    1.837, 1.995, 1.941, 2.026, 3.597, 2.867, 1008.127, 483.071, 3.263, 2.537, 4.383, 3.991,
    1.737, 2.335, 3.075, 2.787, 1011.199, 493.055, 30.639, 1.63, 1.785, 5.167,
]

# The A/A test: p99 of 8 identical runs, ms (06_aa_noise.py)
AA_P99 = [1.7718, 2.3872, 1.5758, 1.661, 1.7896, 2.2363, 1.7707, 3.8124]
AA_P50 = [0.3966, 0.4032, 0.39, 0.402, 0.4044, 0.4048, 0.397, 0.3958]


def test_percentile_nearest_rank():
    x = [5, 1, 4, 2, 3]
    assert percentile(x, 50) == 3
    assert percentile(x, 99) == 5 and percentile(x, 0) == 1
    second = [QUIET] * 99 + [FROZEN]
    assert percentile(second, 99) == QUIET  # one frozen request in 100 doesn't reach the p99
    assert percentile([QUIET] * 98 + [FROZEN] * 2, 99) == FROZEN


def test_made_up_example():
    quiet, frozen = [QUIET] * 100, [FROZEN] * 100
    windows = [frozen] + [quiet] * 9
    assert round(mean_of_percentiles(windows, 99), 1) == 100.9  # "about a hundred"
    everything = [x for w in windows for x in w]
    assert percentile(everything, 99) == FROZEN  # 100 of 1,000 froze: 10 in 100
    # the average can be too high, too: 5 frozen requests in one second
    windows = [[QUIET] * 95 + [FROZEN] * 5] + [quiet] * 9
    assert round(mean_of_percentiles(windows, 99), 1) == 100.9
    assert percentile([x for w in windows for x in w], 99) == QUIET  # 5 of 1,000 is under 1%


def test_real_test_average():
    assert len(PER_SECOND_P99) == 58
    assert round(sum(PER_SECOND_P99) / 58, 1) == 159.5  # the "dashboard" number; true p99 925.5
    assert round(mean_of_percentiles([[v] for v in PER_SECOND_P99], 99), 1) == 159.5


def test_tally_and_merge():
    quiet = [0.8] * 60 + [1.5] * 39 + [7.0]
    frozen = [0.9] * 50 + [600.0] * 50
    a, b = tally(quiet, EDGES), tally(frozen, EDGES)
    assert a == [60, 39, 0, 1, 0, 0, 0] and sum(b) == 100
    assert merge(a, b) == tally(quiet + frozen, EDGES)  # adding tallies = tallying everything
    assert percentile_from_tally(a, EDGES, 99) == 2  # the quiet second's p99 range
    assert percentile_from_tally(merge(a, b), EDGES, 99) == 1000  # read once from the total


def test_fanout_formula():
    assert round(p_any_slow(1), 3) == 0.01
    assert round(p_any_slow(5), 3) == 0.049
    assert round(p_any_slow(20), 3) == 0.182  # 82 pages in 100 all fast
    assert 0.628 > 3 * p_any_slow(20)  # measured: 62.8% of 8,532 pages
    assert round(single_percentile_for_slowest(20), 2) == 99.95
    assert single_percentile_for_slowest(1) == 99


def test_share_with_slow():
    slow_line = 0.93  # ms, a lone lookup's p99
    pages = [[0.4, 0.5], [0.4, 1.2], [1.0, 1.1], [0.3, 0.93]]
    assert share_with_slow(pages, slow_line) == 0.5  # 0.93 itself is not over the line
    # a typical lookup inside a 20-call page took 1.14 ms: past the line on its own
    assert share_with_slow([[1.14] * 20], slow_line) == 1.0


def test_aa_spread():
    assert round(spread(AA_P99), 2) == 1.26  # 126%: 1.58 to 3.81 ms with nothing changed
    assert round(spread(AA_P50), 2) == 0.04  # the median barely moves
    assert round((max(AA_P99) - min(AA_P99)) / 0.1413, 1) == 15.8  # vs. one run's 0.14 ms interval


def test_beats_noise():
    assert not beats_noise(1.6, AA_P99)  # "1.8 to 1.6 ms" is inside the A/A range
    assert beats_noise(1.2, AA_P99)
    assert not beats_noise(1.8, AA_P99)
