""" Tests for web/routes/context_overflow_api.py Tests cover: - get_context_overflow_metrics endpoint - get_research_context_overflow endpoint - Various time period filters - Error handling """ import pytest from unittest.mock import Mock from datetime import datetime, timezone class TestGetContextOverflowMetricsUnit: """Unit tests for get_context_overflow_metrics logic.""" def test_calculate_truncation_rate(self): """Test truncation rate calculation.""" # Test the calculation logic # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). requests_with_context = 100 truncated_requests = 15 truncation_rate = 0 if requests_with_context > 0: truncation_rate = (truncated_requests / requests_with_context) * 100 assert truncation_rate == 15.0 def test_calculate_truncation_rate_zero_division(self): """Test truncation rate with zero requests.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). requests_with_context = 0 truncated_requests = 0 truncation_rate = 0 if requests_with_context > 0: truncation_rate = (truncated_requests / requests_with_context) * 100 assert truncation_rate == 0 class TestTimePeriodCalculation: """Tests for time period calculation logic.""" def test_7d_period(self): """Test 7 day period calculation.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). from datetime import timedelta now = datetime.now(timezone.utc) period = "7d" start_date = None if period != "all": if period == "7d": start_date = now - timedelta(days=7) assert start_date is not None assert (now - start_date).days == 7 def test_30d_period(self): """Test 30 day period calculation.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). from datetime import timedelta now = datetime.now(timezone.utc) period = "30d" start_date = None if period != "all": if period == "30d": start_date = now - timedelta(days=30) assert start_date is not None assert (now - start_date).days == 30 def test_3m_period(self): """Test 3 month period calculation.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). from datetime import timedelta now = datetime.now(timezone.utc) period = "3m" start_date = None if period != "all": if period == "3m": start_date = now - timedelta(days=90) assert start_date is not None assert (now - start_date).days == 90 def test_1y_period(self): """Test 1 year period calculation.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). from datetime import timedelta now = datetime.now(timezone.utc) period = "1y" start_date = None if period != "all": if period == "1y": start_date = now - timedelta(days=365) assert start_date is not None assert (now - start_date).days == 365 def test_all_period(self): """Test all time period has no date filter.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). period = "all" start_date = None if period != "all": start_date = datetime.now(timezone.utc) assert start_date is None def test_invalid_period_defaults_to_30d(self): """Test that an invalid period value is normalized to 30d.""" # audit: PUNCHLIST reviewed 2026-05 — issue resolved by prior PR (recommendation: DELETE). VALID_PERIODS = {"7d", "30d", "3m", "1y", "all"} for invalid in ["999d", "abc", "", "10x", "