signal-bot/tests/test_stats_commands.py
Krzysztof kuhy Rudnicki 35102be863 release: 0.2.0 «Urbanus I» — a private photo album, and a tagger for it
Per-person photo commands, where the mechanism is public and the album is
not. Nothing identifying is in this repository: who the people are, what
fires their photos and which file shows whom all live in one directory
outside it, and the tests, the docs and the offline e2e run use three
invented people instead.

Several names in one message intersect -- "!alfa beta" is a photo with both
-- and where an entry offers a choice the bot picks among the readings that
actually have a photo, so a question the album could answer is not refused.
A combination nobody photographed gets one line rather than silence, which
from the group's side is indistinguishable from a broken bot.

Scoping is enforced in the dispatcher, not in the handler: a command the
group is not allowed resolves to nothing, so it is not answered, not counted
as a command, and never named in a log line outside its own groups. A
handler that quietly returned would leak all three. Commands are named by
pseudonym (pack:p1), so the journal and command_uses say somebody asked for
a photo and nothing about whose.

Video works: the attachment goes out as data:video/mp4;filename=...;base64,
which is the container's own spelling, read off its /swagger/doc.json.
Told it is a photo, Signal renders a clip as an unplayable file.

tool/tagger.py serves one page on loopback to fill the album in -- names are
typed into the page, never as arguments, and it prints how many photos each
command would have when it stops, because a command with zero is dead on
arrival.

Also renames the real people who were still sitting in the stats test
fixtures. They remain in the history; the point is that nothing new is
added to it.

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

216 lines
7.6 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""The `!` commands, end to end through a real store."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
import pytest
from signal_bot.animals import load_aliases
from signal_bot.folding import fold
from signal_bot.registry import CommandRegistry
from signal_bot.stats import periods
from signal_bot.stats.backfill import BACKFILL_KEY, seed_from_counter
from signal_bot.stats.commands import HANDLERS, register_stats_commands
from tests.stats_seed import ALA, BASIA, GROUP, KUHY, Shape, wrote
from tests.test_stats_backfill import LEGACY as LEGACY_COUNTER
if TYPE_CHECKING:
from signal_bot.store import Store
TODAY = periods.today()
YESTERDAY = periods.previous_day()
@dataclass
class Sender:
"""The bit of IncomingMessage the handlers actually read."""
source_uuid: str = KUHY[0]
@dataclass
class FakeContext:
"""A CommandContext stand-in that records what was said back."""
store: Store
group_id: str = GROUP
message: Sender = field(default_factory=Sender)
replies: list[str] = field(default_factory=list)
async def reply(self, text: str) -> None:
"""Record the answer instead of sending it."""
self.replies.append(text)
def seed_from_counter_rows(store: Store, uuid: str, name: str, count: int) -> None:
"""Seed a pre-0.1.0 counter and carry it over, as the migration did."""
store.events.connection.executescript(LEGACY_COUNTER)
store.events.connection.execute(
"INSERT INTO message_counts (group_id, uuid, display_name, count) VALUES (?, ?, ?, ?)",
(GROUP, uuid, name, count),
)
store.events.connection.commit()
seed_from_counter(store, "2026-08-01")
async def run(store: Store, name: str, uuid: str = KUHY[0]) -> str:
"""Run one command and return what it said."""
ctx = FakeContext(store=store, message=Sender(source_uuid=uuid))
handler, _ = HANDLERS[name]
await handler(ctx)
return ctx.replies[0]
def test_every_trigger_is_unique_across_the_whole_registry() -> None:
"""A collision raises DuplicateTriggerError, which is a ValueError.
That matters more than it looks: build_bot runs inside create_runtime, and
ValueError is in startup_retry.RETRYABLE. A colliding alias would not
crash the bot -- it would retry forever, logging "signal-cli not reachable
yet", which is the wrong diagnosis entirely. This test is the thing that
catches it instead.
"""
registry = CommandRegistry()
register_stats_commands(registry)
names = {command.name for command in registry.commands}
assert names == set(HANDLERS)
def test_no_trigger_contains_a_digit() -> None:
"""fold() maps digits onto letters, so "!7dni" resolves as "!tdni"."""
table = load_aliases()
for name in HANDLERS:
for trigger in table[name]:
assert not any(character.isdigit() for character in trigger), trigger
def test_polish_spellings_fold_together() -> None:
"""Somebody typing !tydzien and somebody typing !tydzień want the same thing."""
assert fold("!tydzień") == fold("!tydzien")
assert fold("!miesiąc") == fold("!miesiac")
@pytest.mark.parametrize("name", list(HANDLERS))
async def test_every_command_answers_on_an_empty_history(store: Store, name: str) -> None:
"""Day one of this release: every command says something, none crashes."""
assert await run(store, name)
async def test_today_and_yesterday_are_different_days(store: Store) -> None:
wrote(store, TODAY, ALA, 4)
wrote(store, YESTERDAY, KUHY, 7)
assert "Dziś, na razie:" in await run(store, "stats_today")
assert "Ala" in await run(store, "stats_today")
assert "kuhy" in await run(store, "stats_yesterday")
async def test_yesterday_renders_the_nightly_block_verbatim(store: Store) -> None:
"""The only way to check the headline output without waiting for 21:37."""
wrote(store, YESTERDAY, ALA, 5)
answer = await run(store, "stats_yesterday")
assert answer.startswith("📊 ")
assert "👑 Ala — 5" in answer
async def test_a_day_nobody_wrote_on_says_so(store: Store) -> None:
assert "Nikt jeszcze nic nie napisał" in await run(store, "stats_yesterday")
async def test_the_week_shows_a_share_bar(store: Store) -> None:
wrote(store, TODAY, ALA, 8)
wrote(store, TODAY, KUHY, 2)
answer = await run(store, "stats_week")
assert "Ala — 8" in answer
assert "80%" in answer
assert "█" in answer
async def test_the_month_puts_medals_on_the_podium(store: Store) -> None:
wrote(store, TODAY, ALA, 8)
wrote(store, TODAY, KUHY, 5)
wrote(store, TODAY, BASIA, 2)
answer = await run(store, "stats_month")
assert "🥇 Ala" in answer
assert "🥈 kuhy" in answer
assert "🥉 Basia" in answer
async def test_a_first_period_says_so_rather_than_claiming_no_change(store: Store) -> None:
wrote(store, TODAY, ALA, 8)
assert "pierwszy taki okres w historii" in await run(store, "stats_year")
async def test_me_reports_the_asker(store: Store) -> None:
wrote(store, TODAY, KUHY, 6, Shape(hour=23))
wrote(store, TODAY, ALA, 2)
answer = await run(store, "stats_me")
assert "kuhy — 6 wiadomości w historii" in answer
assert "Udział w rozmowie: 75%" in answer
assert "Najczęściej piszesz między 23:00 a 00:00" in answer
async def test_me_is_honest_with_somebody_who_never_wrote(store: Store) -> None:
wrote(store, TODAY, ALA, 4)
assert "ani jednej wiadomości" in await run(store, "stats_me", uuid="u-nobody")
async def test_records_and_crown_fill_in_once_there_is_history(store: Store) -> None:
wrote(store, periods.shift(TODAY, -2), ALA, 9)
wrote(store, periods.shift(TODAY, -1), ALA, 4)
wrote(store, TODAY, KUHY, 3)
records = await run(store, "stats_records")
assert "Najwięcej w jeden dzień: 9" in records
assert "Rekordy życiowe:" in records
crown = await run(store, "stats_crown")
assert "Najdłuższa seria: Ala" in crown
assert "Dni z koroną łącznie:" in crown
async def test_records_skips_somebody_with_no_days_left_after_exclusions(
store: Store,
) -> None:
"""Somebody whose only day was the backfilled one has no record to show.
Fixed dates, not periods.today(): on the day this was written those were
the same date, and the test passed for the wrong reason.
"""
wrote(store, "2026-08-01", BASIA, 40)
wrote(store, "2026-08-15", ALA, 5)
store.set_meta(GROUP, BACKFILL_KEY, "2026-08-01")
rendered = await run(store, "stats_records")
assert "Ala" in rendered
assert "Basia" not in rendered
async def test_the_backfilled_day_cannot_invent_a_favourite_hour(
store: Store,
) -> None:
"""The 0.1.1 bug: everybody's favourite hour came out as 12:00.
The backfilled counter had no hours, and the placeholder was noon -- so
for every person the 319 seeded rows outvoted every real evening, and
!ja told each of them the same thing.
"""
seed_from_counter_rows(store, KUHY[0], KUHY[1], 300)
wrote(store, "2026-08-15", KUHY, 3, Shape(hour=21))
answer = await run(store, "stats_me")
assert "między 21:00 a 22:00" in answer
assert "12:00" not in answer
async def test_somebody_with_only_backfilled_messages_has_no_hour(
store: Store,
) -> None:
"""Nothing is known about when they wrote, so the line is omitted."""
seed_from_counter_rows(store, KUHY[0], KUHY[1], 5)
answer = await run(store, "stats_me")
assert "Najczęściej piszesz" not in answer
async def test_weekdays_marks_today(store: Store) -> None:
wrote(store, TODAY, ALA, 5)
assert "← dziś" in await run(store, "stats_weekdays")