signal-bot/tests/test_scenario.py
Krzysztof kuhy Rudnicki aefffb2fd9
Some checks are pending
CI / check (push) Waiting to run
feat: add a photo to the album from the chat, tagged, stripped and verified
Send a photo captioned "!add_photo alfa wakacje" in one of the album's own
groups and it lands in the pack tagged with both, retrievable through !alfa
and the new !losowe immediately -- no restart, and no tagger session on the PC.
A label matching nobody becomes a custom tag, and !losowe is the only door to
one, which is what keeps a live swap a *data* swap: it can never register a
trigger or widen a command's groups.

No typed label travels. Labels become pseudonym keys in a new registry
arg_resolver, at the dispatcher, before the line that logs the command name --
so no handler, journal entry, command_uses row or reply body ever holds a word
somebody typed. The reply is counts and pseudonyms: "dodano · p1: 4 · t1: 1".

Stripping is exiftool with a *positive* check on the output, never an exit
code. ffmpeg was tried first and fails silently on photos: given a JPEG with a
real APP1 Exif segment it returned a byte-identical file, GPS intact, exit 0.
The surviving-tag allowlist is per tag *name* rather than per group, because
measurement showed a group-level "must be empty" rule rejects every format
except JPEG -- a valid PNG always reports its IHDR, an MP4 some fifty QuickTime
tags that are the container itself. Codec configuration (CompressorName,
VendorID, the HEVC parameter set) is on the list by decision: CompressorName
lives in the avc1 sample description and exiftool cannot write it at all, so
the alternative was refusing every video and every iPhone HEIC. Location and
identity tags are still refused, and the e2e run re-reads the stored files to
prove a QuickTime geotag did not survive.

.webm and .svg stay on the format allowlist and are stored *unstripped*:
exiftool 13.55 can write neither, and its SVG reader does not even surface
<metadata>. That is a deliberate, documented hole, not an oversight.

Two things this had to defuse along the way:
- personal_data_sources now reads custom.json, so "!add_photo kot <photo>"
  would have outlawed the word "kot" and refused every future commit, fixable
  only by hand-editing an album the committer cannot see. Minting now refuses
  a word that is already a command, and the gate subtracts the repo's own
  alias vocabulary from custom labels only -- curated people.json names are
  never excused.
- signal_bot/external.py is now the single subprocess site in the repository,
  so ruff's S603 needs exactly one scoped per-file-ignore instead of one per
  module that shells out. A test asserts nothing else imports subprocess.

A failed add changes nothing: media and the three manifests are written, the
album is reloaded, and only a load that succeeds is published; a load that
fails rolls all four back and keeps serving the pack that was already working,
without raising -- Restart=always would turn a ConfigError into a loop.

977 tests, 100% branch coverage, no omits and no suppressions. scripts/e2e.sh
gains 8 scenarios driven against the real exiftool on media generated at run
time, and asserts the invariants a reply cannot: that a refused upload is not
stored, that a message with no attachment leaves custom.json byte-identical,
and that signal-cli's own copy of every ingested attachment is gone.

Closes TODO-add-photo.md.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JqqE22jp94B1ypwxppdr1o
2026-08-31 11:20:00 +02:00

207 lines
7.3 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Scenario loading, rendering and adjudication."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from fake_signal.scenario import (
Expectation,
GroupArrival,
Injection,
Scenario,
ScenarioError,
check,
describe,
load_scenario,
quotes,
)
from signal_bot.models import parse_envelope
GROUPS = {"watched": "W==", "reply": "R==", "other": "O=="}
def _write(tmp_path: Path, document: str) -> Path:
path = tmp_path / "s.yaml"
path.write_text(document, encoding="utf-8")
return path
def test_ships_scenarios_load_and_are_valid() -> None:
paths = sorted(Path("fake_signal/scenarios").glob("*.yaml"))
assert paths
for path in paths:
assert load_scenario(path).name
def test_injection_renders_a_parseable_group_envelope() -> None:
frame = Injection(text="!cat").to_frame(GROUPS)
message = parse_envelope(frame)
assert message is not None
assert message.text == "!cat"
assert message.group_id == "W=="
def test_injection_renders_a_direct_message() -> None:
message = parse_envelope(Injection(text="hi", group=None).to_frame(GROUPS))
assert message is not None
assert message.is_direct_message
def test_injection_renders_sync_sticker_and_reaction() -> None:
sticker = parse_envelope(Injection(sticker=True, sync=True).to_frame(GROUPS))
reaction = parse_envelope(Injection(reaction=True).to_frame(GROUPS))
assert sticker is not None
assert sticker.has_sticker
assert reaction is not None
assert reaction.is_reaction
def test_unknown_group_alias_is_rejected() -> None:
with pytest.raises(ScenarioError, match="unknown group alias"):
Injection(group="nope").to_frame(GROUPS)
def test_loads_a_full_scenario(tmp_path: Path) -> None:
path = _write(
tmp_path,
"name: demo\ninject:\n - {text: '!cat'}\nexpect:\n - {kind: image}\n",
)
scenario = load_scenario(path)
assert scenario.name == "demo"
assert scenario.inject[0].text == "!cat"
assert scenario.expect[0].kind == "image"
@pytest.mark.parametrize(
("document", "match"),
[
("- a list\n", "must contain a mapping"),
("inject: []\n", "missing a name"),
("name: x\ninject:\n - {bogus: 1}\n", "unknown inject keys"),
("name: x\nexpect:\n - {bogus: 1}\n", "unknown expect keys"),
("name: x\ninject:\n - not-a-mapping\n", "must be a mapping"),
],
)
def test_malformed_scenarios_are_rejected(tmp_path: Path, document: str, match: str) -> None:
with pytest.raises(ScenarioError, match=match):
load_scenario(_write(tmp_path, document))
def test_describe_classifies_sends() -> None:
assert describe({"message": "hi", "recipients": ["R=="]}) == ("text", "R==", "hi")
assert describe({"base64_attachments": ["A"], "recipients": ["R=="]})[0] == "image"
assert describe({})[1] == ""
def test_quotes_detects_a_pointed_reply() -> None:
assert quotes({"quote_timestamp": 1719177917717}) is True
assert quotes({"message": "hi"}) is False
def test_check_enforces_a_quoted_reply_in_both_directions() -> None:
"""Without this the e2e gate could not tell a quoted reply from a plain one."""
quoted = [{"base64_attachments": ["A"], "recipients": ["R=="], "quote_timestamp": 17}]
plain = [{"base64_attachments": ["A"], "recipients": ["R=="]}]
wants_quote = Scenario("s", expect=[Expectation(kind="image", to="reply", quotes=True)])
wants_none = Scenario("s", expect=[Expectation(kind="image", to="reply", quotes=False)])
assert check(wants_quote, quoted, GROUPS) == []
assert check(wants_none, plain, GROUPS) == []
assert "quotes: expected True" in check(wants_quote, plain, GROUPS)[0]
assert "quotes: expected False" in check(wants_none, quoted, GROUPS)[0]
def test_check_passes_on_a_match() -> None:
scenario = Scenario("s", expect=[Expectation(kind="image", to="reply")])
sent = [{"base64_attachments": ["A"], "recipients": ["R=="]}]
assert check(scenario, sent, GROUPS) == []
def test_check_reports_a_count_mismatch() -> None:
scenario = Scenario("s", expect=[Expectation()])
failures = check(scenario, [], GROUPS)
assert "expected 1 send(s), captured 0" in failures[0]
assert any("missing" in line for line in failures)
def test_check_reports_kind_recipient_and_body() -> None:
scenario = Scenario("s", expect=[Expectation(kind="image", to="reply", contains="nope")])
sent = [{"message": "hello", "recipients": ["W=="]}]
failures = check(scenario, sent, GROUPS)
assert any("kind:" in f for f in failures)
assert any("recipient:" in f for f in failures)
assert any("body:" in f for f in failures)
def test_check_accepts_a_literal_recipient() -> None:
scenario = Scenario("s", expect=[Expectation(to="literal==")])
assert check(scenario, [{"message": "", "recipients": ["literal=="]}], GROUPS) == []
def test_unexpected_sends_are_a_failure() -> None:
"""A scoping regression shows up here: expect [] and the bot replied."""
failures = check(Scenario("s"), [{"message": "leak", "recipients": ["R=="]}], GROUPS)
assert "expected 0 send(s), captured 1" in failures[0]
def test_frame_is_json() -> None:
assert json.loads(Injection().to_frame(GROUPS))["account"] == "+15550000000"
def test_group_arrival_renders_a_listing_row() -> None:
"""A scenario names aliases; the fake needs both real spellings."""
entry = GroupArrival(id="reply", internal_id="watched", name="added").to_entry(GROUPS)
assert entry == {
"id": GROUPS["reply"],
"internal_id": GROUPS["watched"],
"name": "added",
"member": True,
"joinable": True,
}
@pytest.mark.parametrize("field", ["id", "internal_id"])
def test_group_arrival_rejects_an_unknown_alias(field: str) -> None:
arrival = GroupArrival(**{"id": "reply", "internal_id": "watched", field: "nope"})
with pytest.raises(ScenarioError, match="unknown group alias"):
arrival.to_entry(GROUPS)
def test_arrivals_are_parsed_from_a_file(tmp_path: Path) -> None:
path = tmp_path / "s.yaml"
path.write_text(
"name: demo\narrive:\n - {id: reply, internal_id: watched}\n", encoding="utf-8"
)
assert load_scenario(path).arrive[0].internal_id == "watched"
def test_an_injection_can_carry_an_inbound_attachment() -> None:
"""What makes !add_photo expressible offline at all."""
frame = Injection(text="!dodaj alfa", attachment="att-1").to_frame(GROUPS)
message = parse_envelope(frame)
assert message is not None
assert message.attachment_ids == ("att-1",)
assert message.attachment_types == ("image/jpeg",)
def test_an_attachment_type_can_be_stated() -> None:
frame = Injection(text="!dodaj alfa", attachment="att-2", attachment_type="video/mp4").to_frame(
GROUPS
)
message = parse_envelope(frame)
assert message is not None
assert message.attachment_types == ("video/mp4",)
def test_the_attachment_lands_at_the_top_level_not_inside_a_quote() -> None:
"""A reply's quote carries the *quoted* message's media; taking it would
let !add_photo re-file a photo somebody else posted."""
frame = json.loads(Injection(text="!dodaj alfa", attachment="att-1").to_frame(GROUPS))
payload = frame["envelope"]["dataMessage"]
assert "attachments" in payload
assert "quote" not in payload