Coverage for src/robot_dispatch/messaging/publisher.py: 94%
17 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 17:18 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 17:18 +0000
1from __future__ import annotations
3from typing import Any, Protocol, runtime_checkable
6@runtime_checkable
7class Publisher(Protocol):
8 """Minimal publishing contract, implemented by the real broker and by test doubles."""
10 async def publish(
11 self,
12 *,
13 exchange: str,
14 routing_key: str,
15 payload: dict[str, Any],
16 message_id: str | None = None,
17 headers: dict[str, Any] | None = None,
18 expiration_ms: int | None = None,
19 ) -> None: ...
22class RecordingPublisher:
23 """Collects published messages instead of sending them. Used by tests and local demos."""
25 def __init__(self) -> None:
26 self.published: list[dict[str, Any]] = []
27 self.fail_with: Exception | None = None
29 async def publish(
30 self,
31 *,
32 exchange: str,
33 routing_key: str,
34 payload: dict[str, Any],
35 message_id: str | None = None,
36 headers: dict[str, Any] | None = None,
37 expiration_ms: int | None = None,
38 ) -> None:
39 if self.fail_with is not None:
40 raise self.fail_with
41 self.published.append(
42 {
43 "exchange": exchange,
44 "routing_key": routing_key,
45 "payload": payload,
46 "message_id": message_id,
47 "headers": headers or {},
48 "expiration_ms": expiration_ms,
49 }
50 )
52 @property
53 def count(self) -> int:
54 return len(self.published)
56 def clear(self) -> None:
57 self.published.clear()