"""Deterministic Messaging & Notification Sandbox (Wave 0). Simulates SMS, Email, and In-App notification dispatch and persistent outbox events for Operational Alerts and Executive Escalations. Provides deterministic recording, retrieval, and reset capabilities for offline testing. """ from datetime import datetime, timezone from typing import Dict, List, Optional import uuid class NotificationMock: def __init__(self): self._outbox: List[Dict] = [] self._delivery_failures: Dict[str, str] = {} # recipient -> reason to simulate failure def reset_for_tests(self): """Resets in-memory outbox state between tests.""" self._outbox.clear() self._delivery_failures.clear() def simulate_recipient_failure(self, recipient: str, reason: str = "GATEWAY_TIMEOUT"): """Configures simulated delivery failure for a specific recipient.""" self._delivery_failures[recipient] = reason def dispatch_alert_notification( self, tenant_id: str, alert_id: str, category: str, severity: str, title: str, message: str, recipients: List[str], channels: Optional[List[str]] = None, ) -> List[Dict]: """Dispatches alert notifications across specified channels (default: IN_APP, SMS).""" if channels is None: channels = ["IN_APP", "SMS"] if severity in ("CRITICAL", "WARNING") else ["IN_APP"] dispatched_events = [] now = datetime.now(timezone.utc).isoformat() for recipient in recipients: for channel in channels: event_id = f"notif-{uuid.uuid4().hex[:10]}" failure_reason = self._delivery_failures.get(recipient) status = "FAILED" if failure_reason else "DELIVERED" event = { "notification_id": event_id, "tenant_id": tenant_id, "alert_id": alert_id, "category": category, "severity": severity, "title": title, "message": message, "recipient": recipient, "channel": channel, "status": status, "failure_reason": failure_reason, "created_at": now, "delivered_at": now if status == "DELIVERED" else None, } self._outbox.append(event) dispatched_events.append(event) return dispatched_events def get_dispatched_notifications( self, tenant_id: Optional[str] = None, alert_id: Optional[str] = None, channel: Optional[str] = None, ) -> List[Dict]: """Queries dispatched notifications filtered by tenant, alert, or channel.""" results = self._outbox if tenant_id: results = [e for e in results if e["tenant_id"] == tenant_id] if alert_id: results = [e for e in results if e["alert_id"] == alert_id] if channel: results = [e for e in results if e["channel"] == channel] return results def get_recipient_inbox(self, recipient: str) -> List[Dict]: """Queries notifications received by a given user/recipient.""" return [e for e in self._outbox if e["recipient"] == recipient] def record_event(self, tenant_id: str, event_type: str, payload: Dict) -> Dict: """Records an outbox/audit notification event.""" now = datetime.now(timezone.utc).isoformat() event = { "notification_id": f"evt-{uuid.uuid4().hex[:10]}", "tenant_id": tenant_id, "event_type": event_type, "payload": payload, "created_at": now, "status": "DELIVERED", } self._outbox.append(event) return event # Global singleton instance notification_sandbox = NotificationMock() notification_mock = notification_sandbox