from datetime import date import pytest from app.sources import github as github_source def test_iter_year_ranges_splits_cross_year_window() -> None: ranges = github_source._iter_year_ranges(date(2025, 6, 1), date(2026, 6, 1)) assert ranges == [ (date(2025, 6, 1), date(2025, 12, 31)), (date(2026, 1, 1), date(2026, 6, 1)), ] @pytest.mark.asyncio async def test_fetch_github_activity_without_token_merges_year_slices(monkeypatch) -> None: requested_ranges: list[tuple[date, date]] = [] async def fake_fetch_public_range(username, from_date, to_date, timeout_seconds): requested_ranges.append((from_date, to_date)) return { from_date.isoformat(): 1, to_date.isoformat(): 2, } monkeypatch.setattr( github_source, "_fetch_github_activity_public_range", fake_fetch_public_range, ) activity = await github_source.fetch_github_activity( username="octocat", token=None, from_date=date(2025, 6, 1), to_date=date(2026, 6, 1), timeout_seconds=1.0, ) assert requested_ranges == [ (date(2025, 6, 1), date(2025, 12, 31)), (date(2026, 1, 1), date(2026, 6, 1)), ] assert activity == { "2025-06-01": 1, "2025-12-31": 2, "2026-01-01": 1, "2026-06-01": 2, } @pytest.mark.asyncio async def test_fetch_github_activity_with_token_merges_year_slices(monkeypatch) -> None: requested_ranges: list[tuple[date, date, str]] = [] async def fake_fetch_graphql_range(username, token, from_date, to_date, timeout_seconds): requested_ranges.append((from_date, to_date, token)) return { from_date.isoformat(): 3, to_date.isoformat(): 4, } monkeypatch.setattr( github_source, "_fetch_github_activity_graphql_range", fake_fetch_graphql_range, ) activity = await github_source.fetch_github_activity( username="octocat", token="secret", from_date=date(2025, 6, 1), to_date=date(2026, 6, 1), timeout_seconds=1.0, ) assert requested_ranges == [ (date(2025, 6, 1), date(2025, 12, 31), "secret"), (date(2026, 1, 1), date(2026, 6, 1), "secret"), ] assert activity == { "2025-06-01": 3, "2025-12-31": 4, "2026-01-01": 3, "2026-06-01": 4, }