from __future__ import annotations from app.processing.utils.statistics import detect_anomalies, moving_average, percentile_stats def test_moving_average() -> None: values = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0] result = moving_average(values, window=3) assert len(result) == 5 assert abs(result[0] - 2.0) < 0.001 def test_moving_average_short_input() -> None: values = [1.0, 2.0] result = moving_average(values, window=5) assert result == values def test_detect_anomalies() -> None: values = [10.0, 10.1, 9.9, 10.0, 50.0, 10.0, 9.8] anomalies = detect_anomalies(values, threshold=2.0) assert len(anomalies) >= 1 assert any(a["value"] == 50.0 for a in anomalies) def test_percentile_stats() -> None: values = list(range(1, 101)) stats = percentile_stats([float(v) for v in values]) assert abs(stats["p50"] - 50.5) < 1.0 assert stats["p99"] > stats["p95"]