"""Tests from Lab 03. Run: pytest test_signals_practice.py"""
import math

from signals_practice import compression_ratio, length_ratio, repeated_ngrams, wilson


def test_normal_speech_has_a_ratio_near_one():
    # 10 s of speech, 27 words written
    assert math.isclose(length_ratio(27, 10), 27 / 27.2)


def test_one_invented_word_on_silence_stands_out():
    # Whisper's "You" on silence: 1 / 0.2
    assert math.isclose(length_ratio(1, 0), 5.0)


def test_truncation_has_a_tiny_ratio():
    # 10 s of speech, 3 words written
    assert length_ratio(3, 10) < 0.12


def test_loops_compress_well():
    loop = " ".join(["I'm fine."] * 40)
    assert compression_ratio(loop) > 2.4


def test_short_inventions_dont_compress():
    # Why compression points the WRONG way on invented text (AUC 0.08 in the lab)
    assert compression_ratio("You") < 1.0
    assert compression_ratio("") == 0.0


def test_repeated_ngrams():
    assert repeated_ngrams("the cat sat on the mat today") == 0.0
    loop = " ".join(["no no no no"] * 5)
    assert repeated_ngrams(loop) > 0.8
    assert repeated_ngrams("too short") == 0.0


def test_wilson_with_failures():
    # gpt-4o-transcribe: 23 wrong-language outputs in 100 runs -> 15.8% to 32.2%
    lo, hi = wilson(23, 100)
    assert round(lo, 3) == 0.158 and round(hi, 3) == 0.322


def test_wilson_with_zero_failures():
    # nova-3 with English forced: 0 in 100 still allows up to 3.7%
    lo, hi = wilson(0, 100)
    assert lo == 0.0 and round(hi, 3) == 0.037


def test_rule_of_three_is_close():
    # With 0 failures in n, the upper bound is about 3/n
    _, hi = wilson(0, 600)
    assert abs(hi - 3 / 600) < 0.002
