signal-bot/tests/test_stats_nightly.py
Krzysztof kuhy Rudnicki 16e066f4ba feat: survive an outage, and keep the counts that predate the event log
Two gaps kuhy pointed at, both about not losing messages the bot was not
awake for.

Missed *counts* already worked and now have a test saying so: an event
is keyed by Signal's own send timestamp, signal-cli keeps receiving in
json-rpc mode while the bot is down, and the replay on reconnect is
absorbed by the primary key. Messages sent during an outage come back
filed under the day they were actually sent.

Missed *reports* did not. A bot that was down at 21:37 never posted that
day and never went back for it. The last day reported is now written
down per group, so returning from an outage posts every day it owes,
oldest first, bounded at a week. A day nobody spoke on is marked settled
rather than reconsidered forever; a day whose send *failed* is
deliberately left unmarked so the next run retries it.

The pre-0.1.0 counter is carried over rather than discarded: 342
messages across four groups, 319 of them in the real group. What the
counter cannot say is when, so before assuming, four places were checked
for a real inbound timestamp -- journald (commands only), signal-cli's
account.db (19 tables, none inbound; message_send_log is the bot's own
outgoing resend buffer), Signal Desktop (unlinked, empty, and it does
not backfill on link), and signal-api/attachments (the one dated
artifact). The attachment mtimes put 12 files on 29 Aug against 79 on
30 Aug inside the counter's window, which is why the whole counter is
filed under 30 Aug rather than split.

That day is marked in group_meta, and a marked day claims no average
message length -- those messages have a count but no lengths, so
"średnio 0 znaków" would be a measurement nobody took. The mark is also
what will keep the day out of personal records: a merged 22-hour day
would otherwise hand somebody a record they never set.

render_day's options move into a DayView value object; the keyword list
had reached eight and this renderer is about to be shared by several
commands that each want a different subset.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-30 19:23:53 +02:00

76 lines
2.4 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Catching up on reports the bot was offline for."""
from __future__ import annotations
from typing import TYPE_CHECKING
from signal_bot.stats.nightly import (
CATCHUP_LIMIT,
LAST_REPORTED_KEY,
days_to_report,
mark_reported,
)
if TYPE_CHECKING:
from signal_bot.store import Store
GROUP = "group-a=="
def test_a_group_with_no_marker_gets_only_yesterday(store: Store) -> None:
"""A new group, and the first run after this release, must not backlog."""
assert days_to_report(store, GROUP, "2026-09-01") == ["2026-09-01"]
def test_an_up_to_date_group_owes_nothing(store: Store) -> None:
mark_reported(store, GROUP, "2026-09-01")
assert days_to_report(store, GROUP, "2026-09-01") == []
def test_the_day_after_the_marker_is_owed(store: Store) -> None:
mark_reported(store, GROUP, "2026-08-31")
assert days_to_report(store, GROUP, "2026-09-01") == ["2026-09-01"]
def test_an_outage_is_caught_up_oldest_first(store: Store) -> None:
"""Four days offline: all four get posted, in the order they happened."""
mark_reported(store, GROUP, "2026-08-28")
assert days_to_report(store, GROUP, "2026-09-01") == [
"2026-08-29",
"2026-08-30",
"2026-08-31",
"2026-09-01",
]
def test_catch_up_is_bounded(store: Store) -> None:
"""A month offline must not open with a month of back-posts."""
mark_reported(store, GROUP, "2026-07-01")
days = days_to_report(store, GROUP, "2026-09-01")
assert len(days) == CATCHUP_LIMIT
# The most recent ones, not the oldest.
assert days[-1] == "2026-09-01"
assert days[0] == "2026-08-26"
def test_a_marker_ahead_of_yesterday_owes_nothing(store: Store) -> None:
"""Defends the clock going backwards, e.g. an NTP correction."""
mark_reported(store, GROUP, "2026-09-05")
assert days_to_report(store, GROUP, "2026-09-01") == []
def test_the_marker_is_per_group(store: Store) -> None:
mark_reported(store, GROUP, "2026-09-01")
assert store.meta(GROUP, LAST_REPORTED_KEY) == "2026-09-01"
assert store.meta("group-b==", LAST_REPORTED_KEY) is None
def test_a_month_boundary_is_crossed_by_the_date(store: Store) -> None:
mark_reported(store, GROUP, "2026-08-30")
assert days_to_report(store, GROUP, "2026-09-02") == [
"2026-08-31",
"2026-09-01",
"2026-09-02",
]