signal-bot/tests/pack_fixture.py
Krzysztof kuhy Rudnicki 35102be863 release: 0.2.0 «Urbanus I» — a private photo album, and a tagger for it
Per-person photo commands, where the mechanism is public and the album is
not. Nothing identifying is in this repository: who the people are, what
fires their photos and which file shows whom all live in one directory
outside it, and the tests, the docs and the offline e2e run use three
invented people instead.

Several names in one message intersect -- "!alfa beta" is a photo with both
-- and where an entry offers a choice the bot picks among the readings that
actually have a photo, so a question the album could answer is not refused.
A combination nobody photographed gets one line rather than silence, which
from the group's side is indistinguishable from a broken bot.

Scoping is enforced in the dispatcher, not in the handler: a command the
group is not allowed resolves to nothing, so it is not answered, not counted
as a command, and never named in a log line outside its own groups. A
handler that quietly returned would leak all three. Commands are named by
pseudonym (pack:p1), so the journal and command_uses say somebody asked for
a photo and nothing about whose.

Video works: the attachment goes out as data:video/mp4;filename=...;base64,
which is the container's own spelling, read off its /swagger/doc.json.
Told it is a photo, Signal renders a clip as an unplayable file.

tool/tagger.py serves one page on loopback to fill the album in -- names are
typed into the page, never as arguments, and it prints how many photos each
command would have when it stops, because a command with zero is dead on
arrival.

Also renames the real people who were still sitting in the stats test
fixtures. They remain in the history; the point is that nothing new is
added to it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017w1HZ3cZ7neL3rFdQ6VMBA
2026-08-30 22:35:41 +02:00

79 lines
2.8 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""A synthetic photo pack, built in a temp directory.
Deliberately not a fixture: several test modules want packs of different
shapes, and a builder says which shape a test needs in the test itself. The
people are alfa, beta and gamma -- the real album's names are private and
must never appear in this repository, which is the whole reason the pack
lives outside it.
"""
from __future__ import annotations
import base64
import json
from typing import TYPE_CHECKING, Any
from tests.conftest import WATCHED_GROUP
if TYPE_CHECKING:
from pathlib import Path
# A 1x1 PNG, inline so no binary file enters the repo -- the same trick
# fake_signal.state uses for the fixture image it serves.
PIXEL = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
)
# Not a playable file, and it does not need to be: nothing in the bot decodes
# a video, it only has to be named and typed correctly on the way out.
CLIP = b"\x00\x00\x00\x18ftypmp42fake"
PEOPLE: list[dict[str, Any]] = [
{"key": "p1", "triggers": ["!alfa", "!alfka"]},
{"key": "p2", "triggers": ["!beta"]},
{"key": "p3", "triggers": ["!gamma", "!gammą"]},
]
COMBOS: list[dict[str, Any]] = [
{"key": "c1", "triggers": ["!oboje"], "all_of": ["p1", "p2"]},
{"key": "c2", "triggers": ["!ktokolwiek"], "any_of": ["p2", "p3"]},
]
TAGS: dict[str, list[str]] = {
"alfa.png": ["p1"],
"both.png": ["p1", "p2"],
"gamma.png": ["p3"],
"clip.mp4": ["p1"],
}
def build(
root: Path,
*,
people: list[dict[str, Any]] | None = None,
combos: list[dict[str, Any]] | None = None,
tags: dict[str, list[str]] | None = None,
groups: list[str] | None = None,
media: dict[str, bytes] | None = None,
write_tags: bool = True,
) -> Path:
"""Write a pack into ``root`` and return the directory."""
manifest: dict[str, Any] = {
"groups": [WATCHED_GROUP] if groups is None else groups,
"people": PEOPLE if people is None else people,
"combos": COMBOS if combos is None else combos,
}
tagged = TAGS if tags is None else tags
root.mkdir(parents=True, exist_ok=True)
(root / "people.json").write_text(json.dumps(manifest), encoding="utf-8")
if write_tags:
(root / "tags.json").write_text(json.dumps(tagged), encoding="utf-8")
album = root / "media"
album.mkdir(exist_ok=True)
# `is None`, not `or`: an empty mapping is a pack whose files are gone,
# which is a case the loader has to refuse and a test has to be able to ask for.
default = {name: CLIP if name.endswith(".mp4") else PIXEL for name in tagged}
files = default if media is None else media
for name, payload in files.items():
(album / name).write_bytes(payload)
return root