signal-bot/tests/test_stats_nightly.py
Krzysztof kuhy Rudnicki 8df2646377 feat: weekly, monthly and year-in-review posts, keyed off the day reported
A Sunday closes a week, a month's last day closes the month, and
31 December closes the year. Keyed off the day being *reported* rather
than off today, which is the only reading that stays right when the bot
has been offline: catching up on a week-old Sunday now also posts that
week's summary, and there is a test asserting the five messages a
three-day outage produces.

One scheduler, a date predicate. Three background tasks firing at the
same wall-clock time into the same group is three independent ways for
an evening to go wrong, for no gain over an if-statement. A failed
period summary is deliberately best-effort and runs after the day has
been marked, so it cannot make the bot re-post the daily one tomorrow.

Also: the quote frame arrived while this was being written, and it
carries a trap. `quote` has its own nested `attachments` array holding
the *quoted* message's media -- so a text reply to somebody's photo has
attachments in the payload that belong to somebody else. Reading them
would credit the replier with a photo they did not post, in a group
where people reply to photos constantly, inflating one number without
ever looking wrong.

The parser already read the top level only and is correct. It is now
pinned by a test built from the exact captured frame, and written up in
DOCS-signal-envelopes.md, because "it happens to be right" and "it
cannot silently stop being right" are different things. This is the same
wrong-nesting-level mistake that once made the bot ignore its own
stickers.

test_bot.py went past the 250-line cap on the way, so the report and
catch-up tests move into files named for what they cover.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-30 19:56:25 +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_yesterday(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_yesterday_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