signal-bot/tests/test_bot_catchup.py
Krzysztof kuhy Rudnicki 6c6d6d92de
Some checks are pending
CI / check (push) Waiting to run
fix: the evening report is one message, not one per thing it has to say
Tomorrow's run would have posted four times into the same group: the day
the bot owed from the catch-up, the week that closed inside it, today, and
August's summary. Four notifications in one evening is how a daily tally
becomes something people mute.

They are blocks in a single message now, separated by a rule because Signal
renders no markdown. That also makes the send atomic: the group gets all of
it or none of it, and nothing is marked either way, where before a day was
marked before its own weekly summary had even been attempted -- so a summary
that failed was simply lost.

The composition moved to reporting.py, both because bot.py hit the 250-line
cap and because it is worth having pure: "what would the group be told?" is
now answerable with no transport and no send.

The closing-time caveat reads the clock instead of REPORT_HOUR. The
scheduled run fires at the configured minute and still says exactly that; a
run started by hand at some other hour now tells the truth rather than
repeating a time nobody waited for.

The 0.1.6 entry gains a bullet rather than taking a new version: no group has
been told about 0.1.6 yet -- every one of them is still recorded at 0.1.5 --
so the release it describes has not happened from their side.

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

166 lines
6.5 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 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
from signal_bot.transport_fake import FakeTransport
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()
# One message, five blocks. Catching up on the days also catches up the
# periods that closed inside them -- the 30th was a Sunday and the 31st
# ended August -- and posting each separately would turn one evening
# into five notifications.
assert len(transport.texts) == 1
report = transport.texts[0]
# Whole headers, not bare dates: a curiosity line inside one day's block
# can name another day ("rekord życiowy (poprzedni 1, 31 sierpnia)"), so
# a substring search finds the wrong occurrence and the order looks wrong.
sunday = report.index("Niedziela, 30 sierpnia")
week = report.index("tydzień 35")
monday = report.index("Poniedziałek, 31 sierpnia")
assert sunday < week < monday < report.index("sierpień 2026")
assert "Wtorek, 1 września" in report
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) == 1
assert "Niedziela, 30 sierpnia" in transport.texts[0]
assert "tydzień 35" in transport.texts[0]
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_refused_report_marks_nothing_at_all(
config: Config, store: Store, directory: GroupDirectory
) -> None:
"""One message means one outcome: the group got all of it, or none.
The day used to be marked before its weekly summary was even attempted,
so a summary that failed was simply lost. With both inside one send there
is no half-state to reason about -- the next run says the whole thing.
"""
sunday = "2026-08-30"
wrote(store, WATCHED_GROUP, "u1", "Alice", 6, day=sunday)
mark_reported(store, WATCHED_GROUP, "2026-08-29")
transport = RefusingTransport(refuses=WATCHED_GROUP)
await _at(config, store, transport, directory, sunday).publish_report()
assert transport.texts == []
assert store.meta(WATCHED_GROUP, LAST_REPORTED_KEY) == "2026-08-29"