signal-bot/signal_bot/transport_fake.py
Krzysztof kuhy Rudnicki a202317072 feat: answer the message that asked, not just the room
A "!cat" in a busy group produced a cat picture floating loose in the chat with
nothing tying it to whoever asked. Replies now quote the triggering message.

The container advertises this and always could: /v1/about reports
capabilities {"v2/send": ["quotes", "mentions"]}, and /v2/send takes
quote_timestamp, quote_author and quote_message. What was missing was on this
side -- parse_envelope discarded the envelope's timestamp, which is the id
Signal quotes a message by, so nothing downstream could have built a quote even
in principle. IncomingMessage now carries it, preferring the payload's own
value over the envelope's for the case where they ever disagree.

Quote is a value object rather than three more parameters threaded through
every send: the Transport protocol has five methods and two of them send.
CommandContext grows reply_image() so quoting lives in one place instead of
each handler reaching for ctx.transport itself, which is what animals.py did.

quote_of() returns None when the timestamp or author is missing. Answering the
room is a worse reply than a quoted one but a much better outcome than a
payload the container rejects.

The e2e gate learned to tell the two apart: Expectation.quotes demands a send
either does or does not point at a message, defaulting to None so the ten
existing scenarios stay indifferent. Without it the fake would have accepted an
unquoted reply as a pass.

Verified in the testbot group on a real Signal client before this bump, per
kuhy's rule that a bump is a publish rather than a checkpoint: he sent !cat and
confirmed the picture renders attached to his message as a reply.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-29 23:01:01 +02:00

70 lines
2.3 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""In-process transport double used by the unit tests.
This is the fast path: no sockets, no server, no event loop scheduling games.
The protocol-faithful HTTP/WebSocket fake lives in the ``fake_signal`` package
and is what the end-to-end script drives.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Iterable
from signal_bot.quoting import Quote
@dataclass(frozen=True, slots=True)
class SentMessage:
"""One outbound call captured by the fake."""
recipient: str
text: str | None = None
image_base64: str | None = None
quote: Quote | None = None
@dataclass
class FakeTransport:
"""Replays canned frames and records everything the bot sends."""
frames: list[str] = field(default_factory=list)
sent: list[SentMessage] = field(default_factory=list)
attachments: list[str] = field(default_factory=list)
deleted: list[str] = field(default_factory=list)
def queue(self, frames: Iterable[str]) -> None:
"""Add frames to be yielded by the next receive() iteration."""
self.frames.extend(frames)
async def receive(self) -> AsyncIterator[str]:
"""Yield every queued frame once, then stop."""
for frame in list(self.frames):
yield frame
async def send_text(self, text: str, recipient: str, quote: Quote | None = None) -> None:
"""Record a text send."""
self.sent.append(SentMessage(recipient=recipient, text=text, quote=quote))
async def send_image(
self, image_base64: str, recipient: str, quote: Quote | None = None
) -> None:
"""Record an image send."""
self.sent.append(SentMessage(recipient=recipient, image_base64=image_base64, quote=quote))
async def list_attachments(self) -> list[str]:
"""Return the configured attachment ids."""
return list(self.attachments)
async def delete_attachment(self, attachment_id: str) -> None:
"""Record an attachment deletion."""
self.deleted.append(attachment_id)
@property
def texts(self) -> list[str]:
"""Every text body sent so far, in order."""
return [m.text for m in self.sent if m.text is not None]