mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-11 22:03:50 +01:00
!staty, !wczoraj, !tydzień, !miesiąc, !rok, !rekordy, !korona, !dni and !ja, under 29 triggers with both Polish spellings of each. None takes an argument. registry.resolve matches the whole folded body against a trigger, so "!ja ala" resolves to nothing; teaching it to split on whitespace would silently make "!kot pls" fire where today it does nothing. Each question gets its own trigger instead. !wczoraj renders through the same code path as the nightly post, which is not a convenience: the bot cannot trigger its own 21:37 report, so without byte-identical output there is no way to check the headline feature of this release short of waiting for the evening. Two guards worth naming: - test_every_trigger_is_unique builds the whole registry. DuplicateTriggerError subclasses ValueError, and ValueError is in startup_retry.RETRYABLE -- so a colliding alias would not crash the bot, it would retry forever logging "signal-cli not reachable yet". Checked at test time instead of never. - No trigger may contain a digit: fold() maps 0134578 onto letters, so "!7dni" would resolve as "!tdni". Also asserted. Every command answers on an empty history rather than crashing, which is the state the group will actually be in for the next few days: records, the crown and the weekday table all say "za mało danych" because their only day so far is the backfilled one, and they exclude it. Verified against a copy of the live database -- all nine render correctly on the real group's data. One test bug caught while writing it: it seeded "today" and excluded 2026-08-30, which on the day it was written were the same date, so it passed for the wrong reason. Fixed dates now. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
178 lines
6.1 KiB
Python
178 lines
6.1 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
|
|
from signal_bot.stats.commands import HANDLERS, register_stats_commands
|
|
from tests.stats_seed import GROUP, IGA, KUHY, MITHRA, Shape, wrote
|
|
|
|
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)
|
|
|
|
|
|
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))
|
|
await HANDLERS[name](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, MITHRA, 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, MITHRA, 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, MITHRA, 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, MITHRA, 8)
|
|
wrote(store, TODAY, KUHY, 5)
|
|
wrote(store, TODAY, IGA, 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, MITHRA, 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, MITHRA, 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 o 23:00" in answer
|
|
|
|
|
|
async def test_me_is_honest_with_somebody_who_never_wrote(store: Store) -> None:
|
|
wrote(store, TODAY, MITHRA, 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), MITHRA, 9)
|
|
wrote(store, periods.shift(TODAY, -1), MITHRA, 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", IGA, 40)
|
|
wrote(store, "2026-08-15", MITHRA, 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_weekdays_marks_today(store: Store) -> None:
|
|
wrote(store, TODAY, MITHRA, 5)
|
|
assert "← dziś" in await run(store, "stats_weekdays")
|