# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """Catching up on reports the bot was offline for.""" from __future__ import annotations from typing import TYPE_CHECKING import pytest from signal_bot.stats.nightly import ( CATCHUP_LIMIT, LAST_REPORTED_KEY, closing_periods, days_to_report, mark_reported, ) if TYPE_CHECKING: from signal_bot.store import Store GROUP = "group-a==" def test_a_group_with_no_marker_gets_only_the_latest_day(store: Store) -> None: """A new group, and the first run after this release, must not backlog.""" assert days_to_report(store, GROUP, "2026-09-01") == ["2026-09-01"] def test_an_up_to_date_group_owes_nothing(store: Store) -> None: mark_reported(store, GROUP, "2026-09-01") assert days_to_report(store, GROUP, "2026-09-01") == [] def test_the_day_after_the_marker_is_owed(store: Store) -> None: mark_reported(store, GROUP, "2026-08-31") assert days_to_report(store, GROUP, "2026-09-01") == ["2026-09-01"] def test_an_outage_is_caught_up_oldest_first(store: Store) -> None: """Four days offline: all four get posted, in the order they happened.""" mark_reported(store, GROUP, "2026-08-28") assert days_to_report(store, GROUP, "2026-09-01") == [ "2026-08-29", "2026-08-30", "2026-08-31", "2026-09-01", ] def test_catch_up_is_bounded(store: Store) -> None: """A month offline must not open with a month of back-posts.""" mark_reported(store, GROUP, "2026-07-01") days = days_to_report(store, GROUP, "2026-09-01") assert len(days) == CATCHUP_LIMIT # The most recent ones, not the oldest. assert days[-1] == "2026-09-01" assert days[0] == "2026-08-26" def test_a_marker_ahead_of_the_latest_day_owes_nothing(store: Store) -> None: """Defends the clock going backwards, e.g. an NTP correction.""" mark_reported(store, GROUP, "2026-09-05") assert days_to_report(store, GROUP, "2026-09-01") == [] def test_the_marker_is_per_group(store: Store) -> None: mark_reported(store, GROUP, "2026-09-01") assert store.meta(GROUP, LAST_REPORTED_KEY) == "2026-09-01" assert store.meta("group-b==", LAST_REPORTED_KEY) is None def test_a_month_boundary_is_crossed_by_the_date(store: Store) -> None: mark_reported(store, GROUP, "2026-08-30") assert days_to_report(store, GROUP, "2026-09-02") == [ "2026-08-31", "2026-09-01", "2026-09-02", ] @pytest.mark.parametrize( ("day", "expected"), [ # A Sunday closes a week. ("2026-09-06", ["tydzień 36"]), # The last day of a month closes it. ("2026-09-30", ["wrzesień 2026"]), # 31 December closes both the month and the year. ("2026-12-31", ["grudzień 2026", "2026"]), # A Sunday mid-month closes the week only. ("2026-08-30", ["tydzień 35"]), # An ordinary Tuesday closes nothing. ("2026-09-15", []), # 28 February in a leap year is not a month end. ("2028-02-28", []), ("2028-02-29", ["luty 2028"]), ], ) def test_closing_periods(day: str, expected: list[str]) -> None: """Keyed off the day being reported, so a catch-up still gets it right.""" assert [period.label for period in closing_periods(day)] == expected