# 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 caption: str = "" quote: Quote | None = None media_type: str = "" filename: str = "" @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) # Attachment id to its bytes, so a test can post a real upload. An id # that is not in here raises, which is how the "the container lost it" # branch of !add_photo is reached. payloads: dict[str, bytes] = field(default_factory=dict) 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, caption: str = "", ) -> None: """Record an image send.""" await self.send_media(image_base64, recipient, quote, caption) async def send_media( self, payload_base64: str, recipient: str, quote: Quote | None = None, caption: str = "", media_type: str = "", filename: str = "", ) -> None: """Record an attachment send, type and filename included.""" self.sent.append( SentMessage( recipient=recipient, image_base64=payload_base64, quote=quote, caption=caption, media_type=media_type, filename=filename, ) ) async def list_attachments(self) -> list[str]: """Return the configured attachment ids.""" return list(self.attachments) async def fetch_attachment(self, attachment_id: str) -> bytes: """Return one stored attachment's bytes. Raises: KeyError: Nothing was stored under that id. """ return self.payloads[attachment_id] async def delete_attachment(self, attachment_id: str) -> None: """Record an attachment deletion.""" self.deleted.append(attachment_id) self.payloads.pop(attachment_id, None) @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]