mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 20:37:03 +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
132 lines
4.9 KiB
Python
132 lines
4.9 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The shape of a pack: where it lives, and what a typed name can mean."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
from signal_bot.pack import (
|
|
MAX_NAMES,
|
|
candidate_keysets,
|
|
default_dirs,
|
|
media_type,
|
|
options_for,
|
|
)
|
|
from signal_bot.pack_load import load
|
|
from tests import pack_fixture
|
|
|
|
if TYPE_CHECKING:
|
|
from signal_bot.pack import Entry, Pack
|
|
|
|
|
|
def _entry(pack: Pack, key: str) -> Entry:
|
|
return next(entry for entry in pack.entries if entry.key == key)
|
|
|
|
|
|
@pytest.fixture
|
|
def pack(tmp_path: Path) -> Pack:
|
|
return load(pack_fixture.build(tmp_path / "pack"))
|
|
|
|
|
|
def test_a_media_type_comes_from_the_suffix_case_insensitively() -> None:
|
|
assert media_type("a.JPG") == "image/jpeg"
|
|
assert media_type("clip.mp4") == "video/mp4"
|
|
|
|
|
|
def test_matching_wants_every_key_not_any_of_them(pack: Pack) -> None:
|
|
assert pack.matching(frozenset({"p1"})) == ("alfa.png", "both.png", "clip.mp4")
|
|
assert pack.matching(frozenset({"p1", "p2"})) == ("both.png",)
|
|
assert pack.matching(frozenset({"p2", "p3"})) == ()
|
|
|
|
|
|
def test_media_path_is_inside_the_album(pack: Pack) -> None:
|
|
assert pack.media_path("alfa.png") == pack.directory / "media" / "alfa.png"
|
|
|
|
|
|
def test_an_explicit_directory_is_taken_at_its_word(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""And is reported as explicit, which is what makes its absence an error."""
|
|
monkeypatch.setenv("PHOTO_PACK_DIR", "/srv/album")
|
|
assert default_dirs() == ((Path("/srv/album"), True),)
|
|
|
|
|
|
def test_several_albums_can_be_named_at_once(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""One bot, several albums, each carrying its own group scope.
|
|
|
|
Comma-separated, the same spelling GROUP_IDS and TRUSTED_INVITERS use.
|
|
"""
|
|
monkeypatch.setenv("PHOTO_PACK_DIR", "/srv/album, /srv/testalbum ")
|
|
assert default_dirs() == ((Path("/srv/album"), True), (Path("/srv/testalbum"), True))
|
|
|
|
|
|
def test_blank_entries_in_the_list_are_dropped(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A trailing comma is a typo, not a request to load the current directory."""
|
|
monkeypatch.setenv("PHOTO_PACK_DIR", "/srv/album,,")
|
|
assert default_dirs() == ((Path("/srv/album"), True),)
|
|
|
|
|
|
def test_systemd_beats_anything_under_home(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""ProtectHome=read-only is why: StateDirectory is exposed regardless."""
|
|
monkeypatch.setenv("STATE_DIRECTORY", "/var/lib/signal-bot")
|
|
assert default_dirs() == ((Path("/var/lib/signal-bot/pack"), False),)
|
|
|
|
|
|
def test_the_xdg_fallback_needs_no_environment(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.delenv("XDG_DATA_HOME", raising=False)
|
|
monkeypatch.setattr(Path, "home", lambda: Path("/home/somebody"))
|
|
assert default_dirs() == ((Path("/home/somebody/.local/share/signal-bot/pack"), False),)
|
|
|
|
|
|
def test_a_name_resolves_with_or_without_the_bang(pack: Pack) -> None:
|
|
""" "!alfa beta" is how people write it; the second half has no "!"."""
|
|
assert options_for(pack, "beta") == (frozenset({"p2"}),)
|
|
assert options_for(pack, "!beta") == (frozenset({"p2"}),)
|
|
|
|
|
|
def test_a_name_resolves_through_the_same_folding_as_a_command(pack: Pack) -> None:
|
|
"""The diacritic spelling is a separate alias and folds onto the same key."""
|
|
assert options_for(pack, "gammą") == (frozenset({"p3"}),)
|
|
|
|
|
|
def test_a_word_that_is_not_a_name_resolves_to_nothing(pack: Pack) -> None:
|
|
assert options_for(pack, "kto") is None
|
|
|
|
|
|
def test_one_trigger_alone_asks_for_that_person(pack: Pack) -> None:
|
|
assert candidate_keysets(pack, _entry(pack, "p1"), ()) == (frozenset({"p1"}),)
|
|
|
|
|
|
def test_a_tail_of_names_is_intersected(pack: Pack) -> None:
|
|
"""Which is the whole feature: "!alfa beta" means a photo with both."""
|
|
assert candidate_keysets(pack, _entry(pack, "p1"), ["beta"]) == (frozenset({"p1", "p2"}),)
|
|
|
|
|
|
def test_an_all_of_combo_is_one_set_of_several_people(pack: Pack) -> None:
|
|
assert candidate_keysets(pack, _entry(pack, "c1"), ()) == (frozenset({"p1", "p2"}),)
|
|
|
|
|
|
def test_an_any_of_combo_offers_each_reading_separately(pack: Pack) -> None:
|
|
"""So the handler can prefer one that actually has a photo."""
|
|
assert candidate_keysets(pack, _entry(pack, "c2"), ()) == (
|
|
frozenset({"p2"}),
|
|
frozenset({"p3"}),
|
|
)
|
|
|
|
|
|
def test_a_choice_multiplies_out_with_the_rest_of_the_line(pack: Pack) -> None:
|
|
assert candidate_keysets(pack, _entry(pack, "c2"), ["alfa"]) == (
|
|
frozenset({"p2", "p1"}),
|
|
frozenset({"p3", "p1"}),
|
|
)
|
|
|
|
|
|
def test_an_unknown_word_in_the_tail_is_not_silently_dropped(pack: Pack) -> None:
|
|
"""Dropping it would answer a different question and look like bad tags."""
|
|
assert candidate_keysets(pack, _entry(pack, "p1"), ["beta", "nikt"]) is None
|
|
|
|
|
|
def test_too_many_names_is_refused_rather_than_multiplied_out(pack: Pack) -> None:
|
|
assert candidate_keysets(pack, _entry(pack, "p1"), ["beta"] * MAX_NAMES) is None
|