signal-bot/tests/test_registry.py
Krzysztof kuhy Rudnicki ca38147576 release: 0.1.4 «Eleuteriusz» — the list of commands lives in the chat now
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
2026-08-30 21:08:02 +02:00

92 lines
3.0 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Trigger resolution."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from signal_bot.models import parse_envelope
from signal_bot.registry import CommandContext, CommandRegistry, DuplicateTriggerError
from signal_bot.transport_fake import FakeTransport
from tests.conftest import WATCHED_GROUP, envelope
if TYPE_CHECKING:
from signal_bot.config import Config
from signal_bot.store import Store
async def _noop(ctx: CommandContext) -> None:
return None
def test_resolves_a_registered_trigger() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat", "!kot"], _noop, "cat description")
assert registry.resolve("!kot") is not None
assert registry.resolve("!cat").name == "cat"
def test_resolution_is_case_insensitive_and_trims() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat"], _noop, "cat description")
assert registry.resolve(" !CAT ") is not None
def test_unknown_and_prefixless_text_resolves_to_nothing() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat"], _noop, "cat description")
assert registry.resolve("!nope") is None
assert registry.resolve("cat") is None
@pytest.mark.parametrize("text", [None, ""])
def test_empty_text_resolves_to_nothing(text: str | None) -> None:
assert CommandRegistry().resolve(text) is None
def test_duplicate_trigger_is_rejected() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat"], _noop, "cat description")
with pytest.raises(DuplicateTriggerError, match="!cat"):
registry.register("feline", ["!cat"], _noop, "feline description")
def test_commands_lists_in_registration_order() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat"], _noop, "cat description")
registry.register("dog", ["!dog"], _noop, "dog description")
assert [c.name for c in registry.commands] == ["cat", "dog"]
def _ctx(transport: FakeTransport, store: Store, config: Config) -> CommandContext:
message = parse_envelope(envelope(text="!papiez"))
assert message is not None
return CommandContext(
message=message,
group_id=WATCHED_GROUP,
transport=transport,
store=store,
config=config,
command_name="pope",
now=100.0,
day="2026-08-30",
)
async def test_reply_image_defaults_to_no_caption(store: Store, config: Config) -> None:
transport = FakeTransport()
await _ctx(transport, store, config).reply_image("Zm9v")
assert transport.sent[0].caption == ""
async def test_reply_image_passes_the_caption_through(store: Store, config: Config) -> None:
"""The caption rides in the same payload, so Signal shows one message."""
transport = FakeTransport()
await _ctx(transport, store, config).reply_image("Zm9v", "8. Telesfor")
sent = transport.sent[0]
assert sent.caption == "8. Telesfor"
assert sent.image_base64 == "Zm9v"
assert sent.quote is not None