from src.utils import humanize


def test_enabled_by_default(monkeypatch):
    monkeypatch.delenv(humanize.HUMANIZE_ENV, raising=False)
    assert humanize.enabled() is True


def test_kill_switch_disables(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '0')
    assert humanize.enabled() is False


def test_pause_disabled_sleeps_exactly(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '0')
    calls = []
    monkeypatch.setattr(humanize, 'sleep', calls.append)
    humanize.pause(0.2)
    assert calls == [0.2]


def test_pause_stays_within_human_bounds(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '1')
    calls = []
    monkeypatch.setattr(humanize, 'sleep', calls.append)
    for _ in range(200):
        humanize.pause(0.2)
    assert len(calls) == 200
    assert all(0.1 <= value <= 0.4 for value in calls)
    # Kein Metronom: Werte variieren.
    assert len(set(calls)) > 50


def test_pause_ignores_non_positive_values(monkeypatch):
    calls = []
    monkeypatch.setattr(humanize, 'sleep', calls.append)
    humanize.pause(0)
    humanize.pause(-1)
    assert calls == []


def test_reaction_bounds(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '1')
    calls = []
    monkeypatch.setattr(humanize, 'sleep', calls.append)
    for _ in range(100):
        humanize.reaction()
    assert all(0.03 <= value <= 0.2 for value in calls)


def test_reaction_disabled_is_noop(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '0')
    calls = []
    monkeypatch.setattr(humanize, 'sleep', calls.append)
    humanize.reaction()
    assert calls == []


def test_jitter_coordinate_disabled_is_identity(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '0')
    assert humanize.jitterCoordinate(150) == 150


def test_jitter_coordinate_stays_within_radius(monkeypatch):
    monkeypatch.setenv(humanize.HUMANIZE_ENV, '1')
    values = {humanize.jitterCoordinate(150) for _ in range(200)}
    assert values <= set(range(148, 153))
    assert 150 in values
