mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 21:27:16 +01:00
Some checks are pending
CI / check (push) Waiting to run
You asked for !add_photo in testbot without the real album being reachable there. One album cannot do that: its people.json "groups" list *is* the privacy guarantee, so making the commands exist in a second group necessarily makes the real photos postable there. So the bot now loads several albums. PHOTO_PACK_DIR takes a comma-separated list, the spelling GROUP_IDS and TRUSTED_INVITERS already use. Each album carries its own group scope, its own people, its own tags and its own custom labels; they share nothing but the process they run in. That makes trigger collisions between albums legal for the first time, and the registry had to learn it. Two albums are two different sets of people and both may reasonably call somebody "!alfa" -- no message can ever mean both, because a command a group is not scoped to is not a command there at all. So a trigger may be claimed more than once, and DuplicateTriggerError now fires only where the group sets *overlap*. Overlapping albums are refused at startup rather than resolved by registration order: a coin toss about which private album receives a photo is the outcome this whole design exists to prevent. Two consequences worth naming: - registry.resolve() takes an optional group. Without one it still answers "does anything claim this word", which is what pack_add's mint check wants; with one it answers "what runs here", which is what the dispatcher wants. resolve_with_args no longer resolves-then-checks, because with two claimants the first one found may be the wrong group's. - CommandContext moved to signal_bot/context.py to keep registry.py under the 250-line cap. A filing change: that file is "which word runs what", this one is "what the thing that runs may do". DOCS-add-photo.md is likewise a split, not new prose -- DOCS-photo-pack.md had grown past the cap and the ingest path is its own subject now. Verified live: the unit serves both albums, the real group resolves its 13 album commands and testbot its 7, and !losowe / !alfa / !dodaj all fire in testbot through the live dispatcher. 997 tests, 100% branch coverage, 28 e2e scenarios. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JqqE22jp94B1ypwxppdr1o
198 lines
6.6 KiB
Python
198 lines
6.6 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Answering one photo request: what gets sent, and what gets refused."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
from signal_bot.context import CommandContext
|
|
from signal_bot.models import parse_envelope
|
|
from signal_bot.pack_command import (
|
|
EITHER,
|
|
EVERYBODY,
|
|
NO_PHOTO,
|
|
ONE_PERSON,
|
|
description_for,
|
|
register_pack_commands,
|
|
)
|
|
from signal_bot.pack_holder import PackHolder
|
|
from signal_bot.pack_load import load
|
|
from signal_bot.registry import CommandRegistry
|
|
from tests import pack_fixture
|
|
from tests.conftest import WATCHED_GROUP, envelope
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
from signal_bot.config import Config
|
|
from signal_bot.pack import Pack
|
|
from signal_bot.registry import Handler
|
|
from signal_bot.store import Store
|
|
from signal_bot.transport_fake import FakeTransport
|
|
|
|
|
|
@pytest.fixture
|
|
def pack(tmp_path: Path) -> Pack:
|
|
return load(pack_fixture.build(tmp_path / "pack"))
|
|
|
|
|
|
def _registry(pack: Pack) -> CommandRegistry:
|
|
registry = CommandRegistry()
|
|
register_pack_commands(registry, PackHolder(pack))
|
|
return registry
|
|
|
|
|
|
def _ctx(
|
|
transport: FakeTransport,
|
|
store: Store,
|
|
config: Config,
|
|
text: str,
|
|
args: tuple[str, ...] = (),
|
|
) -> CommandContext:
|
|
message = parse_envelope(envelope(text=text))
|
|
assert message is not None
|
|
return CommandContext(
|
|
message=message,
|
|
group_id=WATCHED_GROUP,
|
|
transport=transport,
|
|
store=store,
|
|
config=config,
|
|
command_name="pack:p1",
|
|
now=0.0,
|
|
day="2026-08-30",
|
|
args=args,
|
|
)
|
|
|
|
|
|
async def _run(
|
|
pack: Pack, transport: FakeTransport, store: Store, config: Config, text: str
|
|
) -> None:
|
|
"""Type one line at the pack, the way the dispatcher would."""
|
|
registry = _registry(pack)
|
|
resolved = registry.resolve_with_args(text, WATCHED_GROUP)
|
|
assert resolved is not None
|
|
await resolved.command.handler(_ctx(transport, store, config, text, resolved.args))
|
|
|
|
|
|
def test_each_entry_shape_explains_itself(pack: Pack) -> None:
|
|
by_key = {entry.key: entry for entry in pack.entries}
|
|
assert description_for(by_key["p1"]) == ONE_PERSON
|
|
assert description_for(by_key["c1"]) == EVERYBODY
|
|
assert description_for(by_key["c2"]) == EITHER
|
|
|
|
|
|
def test_a_description_never_names_anybody(pack: Pack) -> None:
|
|
"""!pomoc prints it; the trigger is the only place a name may appear."""
|
|
for entry in pack.entries:
|
|
described = description_for(entry)
|
|
assert described in {ONE_PERSON, EVERYBODY, EITHER}
|
|
assert not any(trigger.strip("!") in described for trigger in entry.triggers)
|
|
|
|
|
|
async def test_a_photo_is_sent_with_its_own_type_and_name(
|
|
pack: Pack, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
await _run(pack, transport, store, config, "!gamma")
|
|
sent = transport.sent[0]
|
|
assert sent.filename == "gamma.png"
|
|
assert sent.media_type == "image/png"
|
|
assert sent.image_base64 is not None
|
|
assert base64.b64decode(sent.image_base64) == pack_fixture.PIXEL
|
|
assert sent.quote is not None
|
|
|
|
|
|
async def test_two_names_want_a_photo_with_both(
|
|
pack: Pack, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
"""both.png is the only file tagged with p1 and p2 at once."""
|
|
await _run(pack, transport, store, config, "!alfa beta")
|
|
assert transport.sent[0].filename == "both.png"
|
|
|
|
|
|
async def test_the_second_name_may_carry_its_own_bang(
|
|
pack: Pack, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
await _run(pack, transport, store, config, "!alfa !beta")
|
|
assert transport.sent[0].filename == "both.png"
|
|
|
|
|
|
async def test_a_combination_nobody_photographed_is_answered_in_one_line(
|
|
pack: Pack, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
await _run(pack, transport, store, config, "!beta gamma")
|
|
assert transport.texts == [NO_PHOTO]
|
|
assert transport.sent[0].image_base64 is None
|
|
|
|
|
|
async def test_a_name_nobody_has_gets_the_same_answer(
|
|
pack: Pack, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
"""From the group's side it is the same question: is there such a photo?"""
|
|
await _run(pack, transport, store, config, "!alfa nikt")
|
|
assert transport.texts == [NO_PHOTO]
|
|
|
|
|
|
async def test_a_choice_lands_on_the_reading_that_has_a_photo(
|
|
tmp_path: Path, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
"""p2 has no photo at all, so "either of them" must land on p3 every time.
|
|
|
|
Deterministic on purpose. Asserting that a random choice eventually
|
|
produces both readings is a test that fails one run in sixteen, and this
|
|
repo has already shipped one bug because a random API was believed after
|
|
a single call.
|
|
"""
|
|
pack = load(pack_fixture.build(tmp_path / "pack", tags={"gamma.png": ["p3"]}))
|
|
for _ in range(5):
|
|
await _run(pack, transport, store, config, "!ktokolwiek")
|
|
assert [message.filename for message in transport.sent] == ["gamma.png"] * 5
|
|
|
|
|
|
async def test_a_choice_with_nothing_behind_either_reading_is_answered(
|
|
tmp_path: Path, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
pack = load(pack_fixture.build(tmp_path / "pack", tags={"alfa.png": ["p1"]}))
|
|
await _run(pack, transport, store, config, "!ktokolwiek")
|
|
assert transport.texts == [NO_PHOTO]
|
|
|
|
|
|
async def test_a_video_keeps_being_a_video(
|
|
tmp_path: Path, transport: FakeTransport, store: Store, config: Config
|
|
) -> None:
|
|
"""Told it is a jpeg, Signal renders an unplayable file."""
|
|
pack = load(pack_fixture.build(tmp_path / "pack", tags={"clip.mp4": ["p1"]}))
|
|
await _run(pack, transport, store, config, "!alfa")
|
|
assert transport.sent[0].media_type == "video/mp4"
|
|
assert transport.sent[0].filename == "clip.mp4"
|
|
|
|
|
|
def test_every_command_is_named_by_key_and_scoped_to_the_pack(pack: Pack) -> None:
|
|
"""A log line or a command_uses row must not say who was asked for."""
|
|
registry = _registry(pack)
|
|
assert [command.name for command in registry.commands] == [
|
|
"pack:p1",
|
|
"pack:p2",
|
|
"pack:p3",
|
|
"pack:c1",
|
|
"pack:c2",
|
|
]
|
|
for command in registry.commands:
|
|
assert command.groups == pack.groups
|
|
assert command.takes_args
|
|
|
|
|
|
def test_the_gate_is_applied_to_every_entry(pack: Pack) -> None:
|
|
registry = CommandRegistry()
|
|
marked: list[str] = []
|
|
|
|
def decorate(handler: Handler) -> Handler:
|
|
"""Count the wrap and change nothing else."""
|
|
marked.append("wrapped")
|
|
return handler
|
|
|
|
register_pack_commands(registry, PackHolder(pack), decorate=decorate)
|
|
assert len(marked) == len(pack.entries)
|