mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 20:37:03 +01:00
Thirteen commands, and the only list of them was a README table nobody in a Signal group can read — which had itself gone stale two releases earlier, still advertising four commands after nine more had shipped. 0.1.1 gave each command an output that says what the command is; that only helps once you already know to type it. !pomoc (also !pomocy, !help, !komendy, !commands) prints all fourteen, grouped, one Polish line each. The interesting part is that it cannot go stale. CommandRegistry.register now takes the description as a REQUIRED argument, so a command that does not explain itself does not compile; and render_help prints any command that help_command.GROUPS forgot to file, under no heading, rather than dropping it. Two independent ways to fail closed, because the README proved that a hand-maintained second list is a list that drifts. Command also gained `primary`, the first raw alias. The trigger set could not supply it: those are folded skeletons in an unordered frozenset, so all that survives of "!papież" there is "!papiez", and the styled Unicode aliases come out as zalgo. aliases.json already lists the canonical Polish spelling first. The full trigger table moves to DOCS-commands.md. It did not fit in README — that file was already sitting exactly on the shared 250-line cap, and the gate caught the overflow on the first commit attempt. Exercised in testbot at the unbumped version before this bump: the live dispatcher ran it and posted the list, journal clean. pytest 708 green at 100% branch coverage, e2e green including a new scenario that asserts !pomoc mentions a command registered by a different module. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017w1HZ3cZ7neL3rFdQ6VMBA
145 lines
5.0 KiB
Python
145 lines
5.0 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""!pomoc, and the two gates that keep it from going stale."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from signal_bot.animals import register_animal_commands
|
|
from signal_bot.config import DEFAULT_COMMONS_API_URL
|
|
from signal_bot.help_command import (
|
|
COMMAND_NAME,
|
|
GROUPS,
|
|
HEADING,
|
|
SPELLINGS,
|
|
register_help_command,
|
|
render_help,
|
|
)
|
|
from signal_bot.http_client import ImageFetcher
|
|
from signal_bot.models import parse_envelope
|
|
from signal_bot.popes_command import register_pope_command
|
|
from signal_bot.portraits import PortraitCache
|
|
from signal_bot.registry import CommandContext, CommandRegistry
|
|
from signal_bot.stats.commands import register_stats_commands
|
|
from tests.conftest import WATCHED_GROUP, envelope
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.config import Config
|
|
from signal_bot.store import Store
|
|
from signal_bot.transport_fake import FakeTransport
|
|
|
|
|
|
def _offline_fetcher() -> ImageFetcher:
|
|
"""A fetcher wired to a transport that refuses to be called.
|
|
|
|
!pomoc never fetches anything, and the other commands are registered here
|
|
only so that the help text has something to list.
|
|
"""
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
raise AssertionError(request.url)
|
|
|
|
return ImageFetcher(httpx.AsyncClient(transport=httpx.MockTransport(handler)))
|
|
|
|
|
|
def full_registry() -> CommandRegistry:
|
|
"""Every command the live bot registers, wired the way build_bot wires it."""
|
|
registry = CommandRegistry()
|
|
fetcher = _offline_fetcher()
|
|
register_animal_commands(registry, fetcher)
|
|
register_pope_command(registry, PortraitCache(fetcher, DEFAULT_COMMONS_API_URL))
|
|
register_stats_commands(registry)
|
|
register_help_command(registry)
|
|
return registry
|
|
|
|
|
|
async def _noop(ctx: CommandContext) -> None:
|
|
"""A handler that does nothing."""
|
|
|
|
|
|
def test_every_registered_command_carries_a_description() -> None:
|
|
"""The whole point of making description a required argument."""
|
|
for command in full_registry().commands:
|
|
assert command.description.strip(), command.name
|
|
|
|
|
|
def test_help_lists_every_command_exactly_once() -> None:
|
|
"""A command missing from !pomoc, or listed twice, fails here.
|
|
|
|
Asserted on the whole line rather than the bare trigger, so prose that
|
|
happens to mention "!stats" can neither satisfy nor break it.
|
|
"""
|
|
registry = full_registry()
|
|
text = render_help(registry.commands)
|
|
for command in registry.commands:
|
|
assert text.count(f"\n{command.primary} — {command.description}") == 1, command.name
|
|
|
|
|
|
def test_help_prints_the_polish_spelling_not_the_folded_one() -> None:
|
|
"""`triggers` holds skeletons; only `primary` still carries the diacritics."""
|
|
text = render_help(full_registry().commands)
|
|
assert "!papież" in text
|
|
assert "!papiez" not in text
|
|
assert "!tydzień" in text
|
|
|
|
|
|
def test_headings_appear_in_order() -> None:
|
|
"""Obrazki, Papież, Statystyki, then the ungrouped block, then the note."""
|
|
text = render_help(full_registry().commands)
|
|
positions = [text.index(f"\n{title}\n") for title, _ in GROUPS]
|
|
assert positions == sorted(positions)
|
|
assert text.startswith(HEADING)
|
|
assert text.endswith(SPELLINGS)
|
|
|
|
|
|
def test_a_command_outside_the_groups_is_still_listed() -> None:
|
|
"""The safety net: forgetting to add a command to GROUPS cannot hide it."""
|
|
registry = full_registry()
|
|
registry.register("ping", ["!ping"], _noop, "odpowiada pong")
|
|
assert "\n!ping — odpowiada pong" in render_help(registry.commands)
|
|
|
|
|
|
def test_a_group_naming_an_unknown_command_fails_loudly() -> None:
|
|
"""A typo in GROUPS raises rather than silently dropping a line."""
|
|
registry = CommandRegistry()
|
|
register_help_command(registry)
|
|
with pytest.raises(KeyError):
|
|
render_help(registry.commands)
|
|
|
|
|
|
async def test_the_handler_replies_with_the_rendered_list(
|
|
transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
"""!pomoc answers in the group that asked, quoting the asker."""
|
|
registry = full_registry()
|
|
command = registry.resolve("!pomoc")
|
|
assert command is not None
|
|
assert command.name == COMMAND_NAME
|
|
message = parse_envelope(envelope(text="!pomoc"))
|
|
assert message is not None
|
|
await command.handler(
|
|
CommandContext(
|
|
message=message,
|
|
group_id=WATCHED_GROUP,
|
|
transport=transport,
|
|
store=store,
|
|
config=config,
|
|
command_name=COMMAND_NAME,
|
|
now=100.0,
|
|
day="2026-08-30",
|
|
)
|
|
)
|
|
assert transport.sent[0].recipient == WATCHED_GROUP
|
|
assert transport.sent[0].text == render_help(registry.commands)
|
|
|
|
|
|
@pytest.mark.parametrize("trigger", ["!pomoc", "!pomocy", "!help", "!komendy", "!commands"])
|
|
def test_every_alias_resolves(trigger: str) -> None:
|
|
"""All five spellings reach the same command."""
|
|
command = full_registry().resolve(trigger)
|
|
assert command is not None
|
|
assert command.name == COMMAND_NAME
|