signal-bot/tests/test_stats_nightly.py
Krzysztof kuhy Rudnicki 50bd390b00 release: 0.1.6 «Zephyrinus» — 21:37 reports today, not yesterday
The scheduler fired exactly on time tonight and posted nothing, because
publish_report covered the previous full day and the bot's first recorded
day was today: "no messages in group.WTI5dVptbG... on 2026-08-29". Correct by
code, a day late to everybody reading the group.

So the report covers the day it runs on. The count closes at the report
time and render_day says so, because a total that quietly omits three hours
is worse than one that admits to it. Everything that made a late day
recoverable stays: the per-group marker still prevents a double post, and
days_to_report still yields every day an outage swallowed.

closing_periods keeps taking a day rather than reading a clock, so a week
now closes on the Sunday evening it ends -- off a day that is itself still
running -- and a catch-up on a week-old Sunday still gets it right.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017w1HZ3cZ7neL3rFdQ6VMBA
2026-08-30 22:09:13 +02:00

102 lines
3.2 KiB
Python

# 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