# 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"