"""Tests for the detections routes and helper functions.""" import pytest def test_detections_list_endpoint(client): """GET /api/detections returns a valid status code.""" c, mock_db = client mock_db.query.return_value.result_rows = [(50,)] # count query resp = c.get("/api/detections") assert resp.status_code in (200, 404, 422, 500) def test_detections_list_with_filters(client): """GET /api/detections supports filter query params.""" c, mock_db = client mock_db.query.return_value.result_rows = [(0,)] resp = c.get("/api/detections?threat_level=CRITICAL&page=1&page_size=10") assert resp.status_code in (200, 404, 422, 500) def test_detections_pagination(client): """GET /api/detections supports pagination params.""" c, mock_db = client mock_db.query.return_value.result_rows = [(0,)] resp = c.get("/api/detections?page=2&page_size=10") assert resp.status_code in (200, 404, 422, 500) def test_label_to_score_known_labels(): """_label_to_score returns known float values for recognized labels.""" from backend.routes.detections import _label_to_score assert _label_to_score("human") == pytest.approx(0.9) assert _label_to_score("bot") == pytest.approx(0.05) assert _label_to_score("tor") == pytest.approx(0.1) assert _label_to_score("proxy") == pytest.approx(0.25) def test_label_to_score_unknown_label(): """_label_to_score returns 0.5 for unrecognized labels.""" from backend.routes.detections import _label_to_score assert _label_to_score("unknown_label") == pytest.approx(0.5) def test_label_to_score_empty_string(): """_label_to_score returns None for empty string.""" from backend.routes.detections import _label_to_score assert _label_to_score("") is None def test_label_to_score_case_insensitive(): """_label_to_score is case-insensitive.""" from backend.routes.detections import _label_to_score assert _label_to_score("HUMAN") == _label_to_score("human") assert _label_to_score("Bot") == _label_to_score("bot") def test_detections_search_filter(client): """GET /api/detections supports search text filter.""" c, mock_db = client mock_db.query.return_value.result_rows = [(0,)] resp = c.get("/api/detections?search=1.2.3") assert resp.status_code in (200, 404, 422, 500) def test_detections_group_by_ip(client): """GET /api/detections supports group_by_ip mode.""" c, mock_db = client mock_db.query.return_value.result_rows = [(0,)] resp = c.get("/api/detections?group_by_ip=true") assert resp.status_code in (200, 404, 422, 500)