mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 20:47:17 +01:00
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
149 lines
5.4 KiB
Python
149 lines
5.4 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 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
|