# 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 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 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 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) == YESTERDAY 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) == YESTERDAY 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.""" 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") at_monday = dt.datetime(2026, 8, 31, 21, 37, tzinfo=WARSAW) client = mock_client(lambda r: httpx.Response(200)) bot = build_bot( config, client, store, transport, directory, Liveness(config, client), clock=lambda: at_monday, ) await bot.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_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() client = mock_client(lambda r: httpx.Response(200)) bot = build_bot( config, client, store, transport, directory, Liveness(config, client), clock=lambda: dt.datetime(2026, 8, 31, 21, 37, tzinfo=WARSAW), ) await bot.publish_report() assert store.meta(WATCHED_GROUP, LAST_REPORTED_KEY) == sunday