signal-bot/tests/test_registry.py
Krzysztof kuhy Rudnicki 558dd297a4 release: 0.0.7 «Telesfor» — !papież, and the bot can test itself
Adds !papież / !papiez / !pope: a random pope as a photo, captioned with his
Latin name, reign and pl.wikipedia link. popes.json already carried a Commons
portrait filename for all 267 and nothing had ever consumed it.

The Latin name comes from the Latin column of Poczet papieży, not from the
la.wikipedia sitelink. The sitelink resolved for all 267 and was still wrong:
la.wikipedia numbers several popes differently, so #154 came out as
"Stefan IX — Stephanus X" in a single caption, which reads as an off-by-one.
The table's own column says "Stephanus Nonus" and cannot disagree with the
name beside it. LATIN_OVERRIDES ships empty; it is a tripwire, not a backlog.

Portraits are cached on disk under CacheDirectory=signal-bot, named from the
ordinal so no upstream string ever reaches a path, and bounded at 267 entries
so nothing needs evicting. Verified that CacheDirectory wins against
ProtectHome=read-only before relying on it.

Also adds POST /_selftest and scripts/selftest.sh. Signal never delivers an
account its own sends, so no command has ever been exercised end-to-end
without asking a human to type it. This injects a real frame into the live
dispatcher; it is loopback-only and refuses any group not in SELFTEST_GROUPS,
which is empty by default.

Verified against the real endpoints, not just the fake: 267/267 portraits
resolve on Commons, regeneration is byte-identical on every previously
shipped field, and !papież was driven 7 times through the live dispatcher in
testbot, returning distinct popes.

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

92 lines
2.9 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)
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)
assert registry.resolve(" !CAT ") is not None
def test_unknown_and_prefixless_text_resolves_to_nothing() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat"], _noop)
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)
with pytest.raises(DuplicateTriggerError, match="!cat"):
registry.register("feline", ["!cat"], _noop)
def test_commands_lists_in_registration_order() -> None:
registry = CommandRegistry()
registry.register("cat", ["!cat"], _noop)
registry.register("dog", ["!dog"], _noop)
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