signal-bot/tests/test_bot_catchup.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

163 lines
6.2 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Coming back from an outage: the days and the periods the bot still owes."""
from __future__ import annotations
import datetime as dt
from typing import TYPE_CHECKING
import httpx
from signal_bot.bot import Bot, build_bot
from signal_bot.liveness import Liveness
from signal_bot.stats.nightly import LAST_REPORTED_KEY, mark_reported
from signal_bot.stats.periods import WARSAW
from signal_bot.transport_fake import FakeTransport
from tests.conftest import WATCHED_GROUP, mock_client
from tests.test_bot_report import TODAY, YESTERDAY, RefusingTransport, _bot, wrote
if TYPE_CHECKING:
from signal_bot.config import Config
from signal_bot.discovery import GroupDirectory
from signal_bot.store import Store
def _at(
config: Config,
store: Store,
transport: FakeTransport,
directory: GroupDirectory,
day: str,
) -> Bot:
"""A bot whose clock reads 21:37 on ``day``, which is the day it reports."""
at = dt.datetime.fromisoformat(day).replace(hour=21, minute=37, tzinfo=WARSAW)
client = mock_client(lambda r: httpx.Response(200))
return build_bot(
config, client, store, transport, directory, Liveness(config, client), clock=lambda: at
)
async def test_a_missed_day_is_posted_when_the_bot_returns(
config: Config, store: Store, transport: FakeTransport, directory: GroupDirectory
) -> None:
"""An outage over 21:37 used to lose that day's report entirely."""
wrote(store, WATCHED_GROUP, "u1", "Alice", 2, day="2026-08-30")
wrote(store, WATCHED_GROUP, "u1", "Alice", 3, day="2026-08-31")
wrote(store, WATCHED_GROUP, "u1", "Alice", 4, day=YESTERDAY)
mark_reported(store, WATCHED_GROUP, "2026-08-29")
await _bot(config, store, transport, directory).publish_report()
# Five, not three: catching up on the days also catches up the periods
# that closed inside them. The 30th was a Sunday and the 31st ended
# August, so each is followed by its own summary.
assert len(transport.texts) == 5
assert "30 sierpnia" in transport.texts[0]
assert "tydzień 35" in transport.texts[1]
assert "31 sierpnia" in transport.texts[2]
assert "sierpień 2026" in transport.texts[3]
assert "1 września" in transport.texts[4]
assert store.meta(WATCHED_GROUP, LAST_REPORTED_KEY) == TODAY
async def test_a_day_nobody_spoke_is_settled_not_retried_forever(
config: Config, store: Store, transport: FakeTransport, directory: GroupDirectory
) -> None:
wrote(store, WATCHED_GROUP, "u1", "Alice", 1, day=YESTERDAY)
mark_reported(store, WATCHED_GROUP, "2026-08-30")
await _bot(config, store, transport, directory).publish_report()
assert len(transport.texts) == 1
assert store.meta(WATCHED_GROUP, LAST_REPORTED_KEY) == TODAY
async def test_a_failed_send_leaves_its_day_unmarked(
config: Config, store: Store, directory: GroupDirectory
) -> None:
"""So the next run retries it instead of losing the day silently."""
wrote(store, WATCHED_GROUP, "u1", "Alice", 1, day=YESTERDAY)
mark_reported(store, WATCHED_GROUP, "2026-08-31")
transport = RefusingTransport(refuses=WATCHED_GROUP)
await _bot(config, store, transport, directory).publish_report()
assert store.meta(WATCHED_GROUP, LAST_REPORTED_KEY) == "2026-08-31"
async def test_publishing_twice_posts_once(
config: Config, store: Store, transport: FakeTransport, directory: GroupDirectory
) -> None:
"""The marker is what stops a restart re-posting the same day."""
wrote(store, WATCHED_GROUP, "u1", "Alice", 3)
bot = _bot(config, store, transport, directory)
await bot.publish_report()
await bot.publish_report()
assert len(transport.texts) == 1
async def test_a_sunday_also_posts_the_week(
config: Config, store: Store, transport: FakeTransport, directory: GroupDirectory
) -> None:
"""One scheduler, a date predicate -- not three tasks racing at 21:37.
The week now closes on the Sunday evening it ends, off a day that is
itself still running. That is the point of the predicate taking a day
instead of reading a clock: partial or not, Sunday is the last day of
the week and the summary belongs to it.
"""
sunday = "2026-08-30"
wrote(store, WATCHED_GROUP, "u1", "Alice", 4, day="2026-08-28")
wrote(store, WATCHED_GROUP, "u1", "Alice", 6, day=sunday)
mark_reported(store, WATCHED_GROUP, "2026-08-29")
await _at(config, store, transport, directory, sunday).publish_report()
assert len(transport.texts) == 2
assert "Niedziela, 30 sierpnia" in transport.texts[0]
assert "tydzień 35" in transport.texts[1]
async def test_a_saturday_closes_nothing(
config: Config, store: Store, transport: FakeTransport, directory: GroupDirectory
) -> None:
"""The day before the week ends is still just a day."""
saturday = "2026-08-29"
wrote(store, WATCHED_GROUP, "u1", "Alice", 4, day=saturday)
mark_reported(store, WATCHED_GROUP, "2026-08-28")
await _at(config, store, transport, directory, saturday).publish_report()
assert len(transport.texts) == 1
assert "Sobota, 29 sierpnia" in transport.texts[0]
async def test_an_ordinary_day_posts_only_the_day(
config: Config, store: Store, transport: FakeTransport, directory: GroupDirectory
) -> None:
wrote(store, WATCHED_GROUP, "u1", "Alice", 4)
await _bot(config, store, transport, directory).publish_report()
assert len(transport.texts) == 1
async def test_a_failing_weekly_summary_does_not_unmark_the_day(
config: Config, store: Store, directory: GroupDirectory
) -> None:
"""The day was posted; retrying it tomorrow would repeat it."""
sunday = "2026-08-30"
wrote(store, WATCHED_GROUP, "u1", "Alice", 6, day=sunday)
mark_reported(store, WATCHED_GROUP, "2026-08-29")
class RefuseSecond(FakeTransport):
"""Lets the day through and rejects everything after it."""
async def send_text(self, text: str, recipient: str) -> None:
if self.sent:
msg = "no"
raise RuntimeError(msg)
await super().send_text(text, recipient)
transport = RefuseSecond()
await _at(config, store, transport, directory, sunday).publish_report()
assert store.meta(WATCHED_GROUP, LAST_REPORTED_KEY) == sunday