Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

105 KB
Raw Blame
//! Unit tests for the notification services.

mod access_request_delivery;
mod android;
mod ios_navigation;
mod status_updates;

use crate::domain::models::apple::APNSPushNotification;
use crate::domain::models::device::DeviceType;
use crate::domain::models::email_notification_digest::BulkDigestStateMachine;
use crate::domain::models::email_notification_digest::ports::DigestBatch;
use crate::domain::models::email_notification_digest::ports::{ClaimResult, DigestBatcher};
use crate::domain::models::mobile::NotifCollapseKey;
use crate::domain::models::queue_message::{
    ConnGatewayNotification, EmailContent, EmailCreateBundle, NotificationChannel, QueueMessage,
    RawQueueMessage, RealtimeNotif,
};
use crate::domain::models::request::{
    NotificationStatus, UpdateNotificationsForEntitiesRequest, UpdateNotificationsRequest,
};
use crate::domain::models::{
    DeviceEndpoint, Notification, NotificationExtEmail, NotificationExtIos,
    NotificationIdAndCollapseKey, RateLimitConfig, RateLimitExceeded, RateLimitKey,
    RateLimitResult, SendNotificationRequestBuilder, TaggedContent, UserNotificationRow,
};
use crate::domain::ports::{
    EmailSender, NotificationEgress, NotificationQueue, NotificationRepository, NotificationSender,
    RealtimeSender, SnsEndpointManager,
};
use crate::domain::service::{
    NotificationEgressService, NotificationIngress, NotificationIngressService, NotificationReader,
    NotificationReaderService, PlatformArnConfig,
};
use chrono::Utc;
use macro_user_id::cowlike::CowLike;
use macro_user_id::user_id::MacroUserIdStr;
use model_entity::{Entity, EntityType};
use rate_limit::domain::models::RateLimitOk;
use rootcause::{Report, report};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use uuid::Uuid;

/// A test notification type.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct TestNotification {
    message: String,
}

#[derive(Debug, Deserialize, PartialEq)]
#[serde(tag = "tag", content = "content", rename_all = "snake_case")]
enum TestNotifEvent {
    TestNotification(TestNotification),
}

impl Notification for TestNotification {
    const TYPE_NAME: &'static str = "test_notification";
}

impl NotificationExtIos for TestNotification {
    type NotifData = TestNotification;

    fn collapse_key(&self, _entity: &model_entity::Entity<'_>) -> NotifCollapseKey {
        NotifCollapseKey::new("test")
    }

    fn as_apns<'a>(
        &self,
        _sender: Option<MacroUserIdStr<'a>>,
        _entity: &model_entity::Entity<'_>,
        _notification_id: uuid::Uuid,
    ) -> Option<APNSPushNotification<Self::NotifData>> {
        Some(APNSPushNotification {
            aps: Default::default(),
            push_notification_data: self.clone(),
        })
    }
}

impl NotificationExtEmail for TestNotification {
    fn format_email(&self) -> crate::domain::models::queue_message::EmailContent {
        EmailContent::new("Test".to_string(), self.message.clone())
    }

    fn rate_limit_config() -> RateLimitConfig {
        RateLimitConfig {
            max_count: u64::MAX,
            window: Duration::from_hours(1),
        }
    }

    fn rate_limit_key(&self) -> RateLimitKey {
        RateLimitKey::from_str_hashed(&"test-key")
    }
}

/// Helper to create a test user ID.
fn test_user_id(email: &str) -> MacroUserIdStr<'static> {
    MacroUserIdStr::try_from_email(email).unwrap()
}

/// Mock repository that tracks calls.
struct MockRepository {
    withdrawn_notifications: HashSet<Uuid>,
    muted_users: HashSet<MacroUserIdStr<'static>>,
    unsubscribed_users: HashSet<MacroUserIdStr<'static>>,
    type_disabled_users: HashSet<MacroUserIdStr<'static>>,
    device_endpoints: HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>>,
    created_notifications: Mutex<Vec<Uuid>>,
    stored_collapse_keys: Mutex<Vec<(Uuid, Option<String>)>>,
    basic_notifications: Vec<NotificationIdAndCollapseKey>,
    digest_eligible_notification_ids: Option<HashSet<Uuid>>,
    entity_notifications: HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>,
    entity_notification_ids: Vec<Uuid>,
    updated_notifications: Option<Vec<UserNotificationRow<serde_json::Value>>>,
    entity_lookup_calls: Mutex<Vec<(String, Vec<(EntityType, String)>)>>,
    entity_lookup_filters: Mutex<Vec<(Vec<crate::domain::models::NotificationState>, bool)>>,
    basic_notification_calls: Mutex<Vec<Vec<Uuid>>>,
    mark_seen_calls: Mutex<Vec<(String, Vec<Uuid>)>>,
    mark_done_calls: Mutex<Vec<(String, Vec<Uuid>, bool)>>,
    deleted_device_endpoints: Mutex<Vec<String>>,
}

impl MockRepository {
    fn new() -> Self {
        Self {
            withdrawn_notifications: HashSet::new(),
            muted_users: HashSet::new(),
            unsubscribed_users: HashSet::new(),
            type_disabled_users: HashSet::new(),
            device_endpoints: HashMap::new(),
            created_notifications: Mutex::new(Vec::new()),
            stored_collapse_keys: Mutex::new(Vec::new()),
            basic_notifications: Vec::new(),
            digest_eligible_notification_ids: None,
            entity_notifications: HashMap::new(),
            entity_notification_ids: Vec::new(),
            updated_notifications: None,
            entity_lookup_calls: Mutex::new(Vec::new()),
            entity_lookup_filters: Mutex::new(Vec::new()),
            basic_notification_calls: Mutex::new(Vec::new()),
            mark_seen_calls: Mutex::new(Vec::new()),
            mark_done_calls: Mutex::new(Vec::new()),
            deleted_device_endpoints: Mutex::new(Vec::new()),
        }
    }

    fn with_basic_notification(mut self, id: Uuid, collapse_key: String) -> Self {
        self.basic_notifications.push(NotificationIdAndCollapseKey {
            id,
            apns_collapse_key: collapse_key,
        });
        self
    }

    fn with_digest_eligible_notification_ids(
        mut self,
        ids: impl IntoIterator<Item = Uuid>,
    ) -> Self {
        self.digest_eligible_notification_ids = Some(ids.into_iter().collect());
        self
    }

    fn with_entity_notifications(
        mut self,
        entity: Entity<'static>,
        notifications: Vec<UserNotificationRow<serde_json::Value>>,
    ) -> Self {
        self.entity_notifications.insert(entity, notifications);
        self
    }

    fn with_entity_notification_ids(mut self, notification_ids: Vec<Uuid>) -> Self {
        self.entity_notification_ids = notification_ids;
        self
    }

    fn with_updated_notifications(
        mut self,
        notifications: Vec<UserNotificationRow<serde_json::Value>>,
    ) -> Self {
        self.updated_notifications = Some(notifications);
        self
    }

    fn with_muted_user(mut self, user_id: MacroUserIdStr<'static>) -> Self {
        self.muted_users.insert(user_id);
        self
    }

    fn with_unsubscribed_user(mut self, user_id: MacroUserIdStr<'static>) -> Self {
        self.unsubscribed_users.insert(user_id);
        self
    }

    fn with_type_disabled_user(mut self, user_id: MacroUserIdStr<'static>) -> Self {
        self.type_disabled_users.insert(user_id);
        self
    }

    fn with_device_endpoint(
        mut self,
        user_id: MacroUserIdStr<'static>,
        endpoint: DeviceEndpoint,
    ) -> Self {
        self.device_endpoints
            .entry(user_id)
            .or_default()
            .push(endpoint);
        self
    }
}

struct MockStateMachine;

impl BulkDigestStateMachine for MockStateMachine {
    async fn ingest<T: Serialize + Send + Sync + 'static>(
        &self,
        _notif: UserNotificationRow<Arc<T>>,
    ) -> Result<crate::domain::models::email_notification_digest::StateMachineDecisionA, Report>
    {
        Err(report!("not implemented"))
    }
}

fn updated_notification(
    user_id: MacroUserIdStr<'_>,
    notification_id: Uuid,
    done: bool,
    viewed_at: Option<chrono::DateTime<Utc>>,
    updated_at: chrono::DateTime<Utc>,
) -> UserNotificationRow<serde_json::Value> {
    UserNotificationRow {
        owner_id: user_id.into_owned(),
        notification_id,
        notification_event_type: "test_notification".to_string(),
        entity: EntityType::Document.with_entity_str("doc-1").into_owned(),
        sent: true,
        state: if done {
            crate::domain::models::NotificationState::Done
        } else {
            crate::domain::models::NotificationState::Seen
        },
        created_at: updated_at,
        viewed_at,
        updated_at,
        deleted_at: None,
        notification_metadata: json!({ "message": "updated notification" }),
        sender_id: None,
    }
}

impl NotificationRepository for MockRepository {
    async fn get_muted_users<'a>(
        &self,
        _user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        Ok(self.muted_users.clone())
    }

    async fn get_unsubscribed_users<'a>(
        &self,
        _item_id: &str,
        _user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        Ok(self.unsubscribed_users.clone())
    }

    async fn create_notification<'a, T: Serialize + Send + Sync>(
        &self,
        request: SendNotificationRequestBuilder<'a, TaggedContent<T>>,
        notification_id: Uuid,
        _service_sender: &str,
        apns_collapse_key: Option<&str>,
    ) -> Result<Option<Vec<UserNotificationRow<Arc<T>>>>, Report> {
        if request.recipient_ids.is_empty() {
            return Ok(None);
        }
        self.created_notifications
            .lock()
            .unwrap()
            .push(notification_id);
        self.stored_collapse_keys
            .lock()
            .unwrap()
            .push((notification_id, apns_collapse_key.map(String::from)));
        let entity = request.notification_entity.clone().into_owned();
        let sender_id = request.sender_id.as_ref().map(|id| id.clone().into_owned());
        let notification_metadata = Arc::new(request.notification.content);
        let rows = request
            .recipient_ids
            .iter()
            .map(|recipient| UserNotificationRow {
                owner_id: recipient.clone().into_owned(),
                notification_id,
                notification_event_type: request.notification.tag.as_ref().to_string(),
                entity: entity.clone(),
                sent: false,
                state: crate::domain::models::NotificationState::Unseen,
                created_at: Utc::now(),
                viewed_at: None,
                updated_at: Utc::now(),
                deleted_at: None,
                notification_metadata: notification_metadata.clone(),
                sender_id: sender_id.clone(),
            })
            .collect();
        Ok(Some(rows))
    }

    async fn notification_is_withdrawn(&self, notification_id: Uuid) -> Result<bool, Report> {
        Ok(self.withdrawn_notifications.contains(&notification_id))
    }

    async fn complete_notification_publication(
        &self,
        _notification_id: Uuid,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn update_sent_status<'a>(
        &self,
        _notification_id: Uuid,
        _user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn get_device_endpoints<'a>(
        &self,
        user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>>, Report> {
        Ok(self
            .device_endpoints
            .iter()
            .filter(|(user, _)| user_ids.contains(user))
            .map(|(user, endpoints)| (user.clone(), endpoints.clone()))
            .collect())
    }

    async fn mark_notifications_seen(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
    ) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
        self.mark_seen_calls
            .lock()
            .unwrap()
            .push((user_id.to_string(), notification_ids.to_vec()));
        if let Some(notifications) = &self.updated_notifications {
            return Ok(notifications.clone());
        }
        let now = Utc::now();
        Ok(notification_ids
            .iter()
            .map(|id| updated_notification(user_id.clone(), *id, false, Some(now), now))
            .collect())
    }

    async fn mark_notifications_done(
        &self,
        user_id: &MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
        done: bool,
    ) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
        self.mark_done_calls.lock().unwrap().push((
            user_id.to_string(),
            notification_ids.to_vec(),
            done,
        ));
        if let Some(notifications) = &self.updated_notifications {
            return Ok(notifications.clone());
        }
        let now = Utc::now();
        Ok(notification_ids
            .iter()
            .map(|id| updated_notification(user_id.clone(), *id, done, None, now))
            .collect())
    }

    async fn get_notification_ids_for_entities(
        &self,
        user_id: MacroUserIdStr<'_>,
        entities: &[model_entity::Entity<'_>],
        status: &NotificationStatus,
    ) -> Result<Vec<Uuid>, Report> {
        let (states, include_unviewed) = status.entity_update_filter();
        self.entity_lookup_filters
            .lock()
            .unwrap()
            .push((states.to_vec(), include_unviewed));
        self.entity_lookup_calls.lock().unwrap().push((
            user_id.to_string(),
            entities
                .iter()
                .map(|entity| (entity.entity_type, entity.entity_id.to_string()))
                .collect(),
        ));
        Ok(self.entity_notification_ids.clone())
    }

    async fn get_basic_notifications(
        &self,
        notification_ids: &[Uuid],
    ) -> Result<Vec<NotificationIdAndCollapseKey>, Report> {
        self.basic_notification_calls
            .lock()
            .unwrap()
            .push(notification_ids.to_vec());
        Ok(self
            .basic_notifications
            .iter()
            .filter(|notification| notification_ids.contains(&notification.id))
            .cloned()
            .collect())
    }

    async fn get_digest_eligible_notification_ids(
        &self,
        _user_id: MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
    ) -> Result<HashSet<Uuid>, Report> {
        Ok(self
            .digest_eligible_notification_ids
            .clone()
            .unwrap_or_else(|| notification_ids.iter().copied().collect()))
    }

    async fn get_user_notifications<T: DeserializeOwned + Send>(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _limit: u32,
        _cursor: models_pagination::Query<Uuid, models_pagination::CreatedAt, ()>,
        _filters: crate::domain::models::request::NotificationListFilters,
    ) -> Result<Vec<UserNotificationRow<T>>, Report> {
        Ok(vec![])
    }

    async fn get_user_notifications_by_event_item_ids<T: DeserializeOwned + Send>(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _event_item_ids: &[Uuid],
        _limit: u32,
        _cursor: models_pagination::Query<Uuid, models_pagination::CreatedAt, ()>,
        _filters: crate::domain::models::request::NotificationListFilters,
    ) -> Result<Vec<UserNotificationRow<T>>, Report> {
        Ok(vec![])
    }

    async fn get_entity_notifications_batch(
        &self,
        _user_id: MacroUserIdStr<'_>,
        entities: Vec<Entity<'static>>,
        _query: crate::domain::models::entity_query::EntityNotificationQuery,
    ) -> Result<HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>, Report> {
        Ok(entities
            .into_iter()
            .map(|entity| {
                let notifications = self
                    .entity_notifications
                    .get(&entity)
                    .cloned()
                    .unwrap_or_default();
                (entity, notifications)
            })
            .collect())
    }

    async fn get_user_notification_by_id<T: DeserializeOwned + Send>(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _notification_id: Uuid,
    ) -> Result<Option<UserNotificationRow<T>>, Report> {
        Ok(None)
    }

    async fn delete_user_notification(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _notification_id: Uuid,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn bulk_delete_user_notifications(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _notification_ids: &[Uuid],
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn delete_all_user_notifications(
        &self,
        _user_id: MacroUserIdStr<'_>,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn get_device_endpoint(
        &self,
        _device_token: &str,
        _device_type: &DeviceType,
    ) -> Result<Option<String>, Report> {
        Ok(None)
    }

    async fn upsert_device(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _device_token: &str,
        _device_endpoint: &str,
        _device_type: &DeviceType,
        _native_call_capable: bool,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn delete_user_devices_by_token(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _device_token: &str,
        _device_type: &DeviceType,
    ) -> Result<Vec<String>, Report> {
        Ok(Vec::new())
    }

    async fn delete_stale_devices_by_token(
        &self,
        _device_token: &str,
        _device_type: &DeviceType,
        _active_endpoint: &str,
    ) -> Result<Vec<String>, Report> {
        Ok(Vec::new())
    }

    async fn delete_device_by_endpoint(&self, endpoint_arn: &str) -> Result<(), Report> {
        self.deleted_device_endpoints
            .lock()
            .unwrap()
            .push(endpoint_arn.to_string());
        Ok(())
    }

    async fn get_users_with_type_disabled<'a>(
        &self,
        _notification_event_type: &str,
        _user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        Ok(self.type_disabled_users.clone())
    }

    async fn get_disabled_notification_types(
        &self,
        _user_id: MacroUserIdStr<'_>,
    ) -> Result<Vec<crate::domain::models::DisabledNotificationType>, Report> {
        Ok(vec![])
    }

    async fn disable_notification_type(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _notification_event_type: &str,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn enable_notification_type(
        &self,
        _user_id: MacroUserIdStr<'_>,
        _notification_event_type: &str,
    ) -> Result<(), Report> {
        Ok(())
    }
}

impl NotificationRepository for std::sync::Arc<MockRepository> {
    async fn get_muted_users<'a>(
        &self,
        user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        (**self).get_muted_users(user_ids).await
    }

    async fn get_unsubscribed_users<'a>(
        &self,
        item_id: &str,
        user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        (**self).get_unsubscribed_users(item_id, user_ids).await
    }

    async fn create_notification<'a, T: Serialize + Send + Sync>(
        &self,
        request: SendNotificationRequestBuilder<'a, TaggedContent<T>>,
        notification_id: Uuid,
        service_sender: &str,
        apns_collapse_key: Option<&str>,
    ) -> Result<Option<Vec<UserNotificationRow<Arc<T>>>>, Report> {
        (**self)
            .create_notification(request, notification_id, service_sender, apns_collapse_key)
            .await
    }

    async fn notification_is_withdrawn(&self, notification_id: Uuid) -> Result<bool, Report> {
        (**self).notification_is_withdrawn(notification_id).await
    }

    async fn complete_notification_publication(&self, notification_id: Uuid) -> Result<(), Report> {
        (**self)
            .complete_notification_publication(notification_id)
            .await
    }

    async fn update_sent_status<'a>(
        &self,
        notification_id: Uuid,
        user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<(), Report> {
        (**self).update_sent_status(notification_id, user_ids).await
    }

    async fn get_device_endpoints<'a>(
        &self,
        user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashMap<MacroUserIdStr<'static>, Vec<DeviceEndpoint>>, Report> {
        (**self).get_device_endpoints(user_ids).await
    }

    async fn mark_notifications_seen(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
    ) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
        (**self)
            .mark_notifications_seen(user_id, notification_ids)
            .await
    }

    async fn mark_notifications_done(
        &self,
        user_id: &MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
        done: bool,
    ) -> Result<Vec<UserNotificationRow<serde_json::Value>>, Report> {
        (**self)
            .mark_notifications_done(user_id, notification_ids, done)
            .await
    }

    async fn get_notification_ids_for_entities(
        &self,
        user_id: MacroUserIdStr<'_>,
        entities: &[model_entity::Entity<'_>],
        status: &NotificationStatus,
    ) -> Result<Vec<Uuid>, Report> {
        (**self)
            .get_notification_ids_for_entities(user_id, entities, status)
            .await
    }

    async fn get_basic_notifications(
        &self,
        notification_ids: &[Uuid],
    ) -> Result<Vec<NotificationIdAndCollapseKey>, Report> {
        (**self).get_basic_notifications(notification_ids).await
    }

    async fn get_digest_eligible_notification_ids(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
    ) -> Result<HashSet<Uuid>, Report> {
        (**self)
            .get_digest_eligible_notification_ids(user_id, notification_ids)
            .await
    }

    async fn get_user_notifications<T: DeserializeOwned + Send>(
        &self,
        user_id: MacroUserIdStr<'_>,
        limit: u32,
        cursor: models_pagination::Query<Uuid, models_pagination::CreatedAt, ()>,
        filters: crate::domain::models::request::NotificationListFilters,
    ) -> Result<Vec<UserNotificationRow<T>>, Report> {
        (**self)
            .get_user_notifications(user_id, limit, cursor, filters)
            .await
    }

    async fn get_user_notifications_by_event_item_ids<T: DeserializeOwned + Send>(
        &self,
        user_id: MacroUserIdStr<'_>,
        event_item_ids: &[Uuid],
        limit: u32,
        cursor: models_pagination::Query<Uuid, models_pagination::CreatedAt, ()>,
        filters: crate::domain::models::request::NotificationListFilters,
    ) -> Result<Vec<UserNotificationRow<T>>, Report> {
        (**self)
            .get_user_notifications_by_event_item_ids(
                user_id,
                event_item_ids,
                limit,
                cursor,
                filters,
            )
            .await
    }

    async fn get_entity_notifications_batch(
        &self,
        user_id: MacroUserIdStr<'_>,
        entities: Vec<Entity<'static>>,
        query: crate::domain::models::entity_query::EntityNotificationQuery,
    ) -> Result<HashMap<Entity<'static>, Vec<UserNotificationRow<serde_json::Value>>>, Report> {
        (**self)
            .get_entity_notifications_batch(user_id, entities, query)
            .await
    }

    async fn get_user_notification_by_id<T: DeserializeOwned + Send>(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_id: Uuid,
    ) -> Result<Option<UserNotificationRow<T>>, Report> {
        (**self)
            .get_user_notification_by_id(user_id, notification_id)
            .await
    }

    async fn delete_user_notification(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_id: Uuid,
    ) -> Result<(), Report> {
        (**self)
            .delete_user_notification(user_id, notification_id)
            .await
    }

    async fn bulk_delete_user_notifications(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_ids: &[Uuid],
    ) -> Result<(), Report> {
        (**self)
            .bulk_delete_user_notifications(user_id, notification_ids)
            .await
    }

    async fn delete_all_user_notifications(
        &self,
        user_id: MacroUserIdStr<'_>,
    ) -> Result<(), Report> {
        (**self).delete_all_user_notifications(user_id).await
    }

    async fn get_device_endpoint(
        &self,
        device_token: &str,
        device_type: &DeviceType,
    ) -> Result<Option<String>, Report> {
        (**self)
            .get_device_endpoint(device_token, device_type)
            .await
    }

    async fn upsert_device(
        &self,
        user_id: MacroUserIdStr<'_>,
        device_token: &str,
        device_endpoint: &str,
        device_type: &DeviceType,
        native_call_capable: bool,
    ) -> Result<(), Report> {
        (**self)
            .upsert_device(
                user_id,
                device_token,
                device_endpoint,
                device_type,
                native_call_capable,
            )
            .await
    }

    async fn delete_user_devices_by_token(
        &self,
        user_id: MacroUserIdStr<'_>,
        device_token: &str,
        device_type: &DeviceType,
    ) -> Result<Vec<String>, Report> {
        (**self)
            .delete_user_devices_by_token(user_id, device_token, device_type)
            .await
    }

    async fn delete_stale_devices_by_token(
        &self,
        device_token: &str,
        device_type: &DeviceType,
        active_endpoint: &str,
    ) -> Result<Vec<String>, Report> {
        (**self)
            .delete_stale_devices_by_token(device_token, device_type, active_endpoint)
            .await
    }

    async fn delete_device_by_endpoint(&self, endpoint_arn: &str) -> Result<(), Report> {
        (**self).delete_device_by_endpoint(endpoint_arn).await
    }

    async fn get_users_with_type_disabled<'a>(
        &self,
        notification_event_type: &str,
        user_ids: &[MacroUserIdStr<'a>],
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        (**self)
            .get_users_with_type_disabled(notification_event_type, user_ids)
            .await
    }

    async fn get_disabled_notification_types(
        &self,
        user_id: MacroUserIdStr<'_>,
    ) -> Result<Vec<crate::domain::models::DisabledNotificationType>, Report> {
        (**self).get_disabled_notification_types(user_id).await
    }

    async fn disable_notification_type(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_event_type: &str,
    ) -> Result<(), Report> {
        (**self)
            .disable_notification_type(user_id, notification_event_type)
            .await
    }

    async fn enable_notification_type(
        &self,
        user_id: MacroUserIdStr<'_>,
        notification_event_type: &str,
    ) -> Result<(), Report> {
        (**self)
            .enable_notification_type(user_id, notification_event_type)
            .await
    }
}

/// Mock queue that tracks published messages.
struct MockQueue {
    /// Stores serialized messages as JSON strings for inspection.
    published: Mutex<Vec<serde_json::Value>>,
}

impl MockQueue {
    fn new() -> Self {
        Self {
            published: Mutex::new(Vec::new()),
        }
    }

    fn get_published(&self) -> Vec<serde_json::Value> {
        self.published.lock().unwrap().clone()
    }
}

impl NotificationQueue for MockQueue {
    async fn publish<'a, T: serde::Serialize + Send + Sync, U: serde::Serialize + Send + Sync>(
        &self,
        messages: Vec<QueueMessage<'a, T, U>>,
    ) -> Result<(), Report> {
        let mut published = self.published.lock().unwrap();
        for message in messages {
            let json = serde_json::to_value(&message).unwrap();
            published.push(json);
        }
        Ok(())
    }

    async fn receive_messages(&self) -> Result<Vec<RawQueueMessage>, Report> {
        Ok(Vec::new())
    }

    async fn delete_message(&self, _receipt_handle: &str) -> Result<(), Report> {
        Ok(())
    }
}

impl NotificationQueue for std::sync::Arc<MockQueue> {
    async fn publish<'a, T: serde::Serialize + Send + Sync, U: serde::Serialize + Send + Sync>(
        &self,
        messages: Vec<QueueMessage<'a, T, U>>,
    ) -> Result<(), Report> {
        (**self).publish(messages).await
    }

    async fn receive_messages(&self) -> Result<Vec<RawQueueMessage>, Report> {
        (**self).receive_messages().await
    }

    async fn delete_message(&self, receipt_handle: &str) -> Result<(), Report> {
        (**self).delete_message(receipt_handle).await
    }
}

/// No-op mock for SNS endpoint management used in reader service tests.
struct MockSnsEndpoint;

impl SnsEndpointManager for MockSnsEndpoint {
    async fn create_platform_endpoint(
        &self,
        _platform_arn: &str,
        _token: &str,
    ) -> Result<String, Report> {
        Ok(String::new())
    }

    async fn get_endpoint_attributes(
        &self,
        _endpoint_arn: &str,
    ) -> Result<HashMap<String, String>, Report> {
        Ok(HashMap::new())
    }

    async fn set_endpoint_attributes(
        &self,
        _endpoint_arn: &str,
        _attributes: HashMap<String, String>,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn delete_endpoint(&self, _endpoint_arn: &str) -> Result<(), Report> {
        Ok(())
    }
}

fn test_platform_config() -> PlatformArnConfig {
    PlatformArnConfig {
        apns_platform_arn: "arn:aws:sns:us-east-1:000:app/APNS/test".to_string(),
        fcm_platform_arn: "arn:aws:sns:us-east-1:000:app/GCM/test".to_string(),
        apns_voip_platform_arn: "arn:aws:sns:us-east-1:000:app/APNS_VOIP/test".to_string(),
    }
}

#[tokio::test]
async fn test_send_notification_success() {
    let service =
        NotificationIngressService::new(MockRepository::new(), MockQueue::new(), MockStateMachine);

    let recipient = test_user_id("[email protected]");
    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([recipient.clone()]),
    }
    .into_request();

    let result = service.send_notification(request).await.unwrap().unwrap();

    assert!(result.notified_recipients.contains(&recipient));
}

#[tokio::test]
async fn test_sender_excluded_from_recipients() {
    let service =
        NotificationIngressService::new(MockRepository::new(), MockQueue::new(), MockStateMachine);

    let sender = test_user_id("[email protected]");
    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: Some(sender.clone()),
        recipient_ids: HashSet::from([sender.clone()]),
    }
    .into_request();

    let result = service.send_notification(request).await.unwrap();

    // Sender should be excluded, no valid recipients remain
    assert!(result.is_none());
}

#[tokio::test]
async fn test_muted_user_excluded() {
    let muted_user = test_user_id("[email protected]");
    let service = NotificationIngressService::new(
        MockRepository::new().with_muted_user(muted_user.clone()),
        MockQueue::new(),
        MockStateMachine,
    );

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([muted_user]),
    }
    .into_request();

    let result = service.send_notification(request).await.unwrap();

    // Muted user should be excluded, no valid recipients remain
    assert!(result.is_none());
}

#[tokio::test]
async fn test_unsubscribed_user_excluded() {
    let unsubscribed_user = test_user_id("[email protected]");
    let service = NotificationIngressService::new(
        MockRepository::new().with_unsubscribed_user(unsubscribed_user.clone()),
        MockQueue::new(),
        MockStateMachine,
    );

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([unsubscribed_user]),
    }
    .into_request();

    let result = service.send_notification(request).await.unwrap();

    // Unsubscribed user should be excluded, no valid recipients remain
    assert!(result.is_none());
}

#[tokio::test]
async fn test_type_disabled_user_excluded() {
    let disabled_user = test_user_id("[email protected]");
    let service = NotificationIngressService::new(
        MockRepository::new().with_type_disabled_user(disabled_user.clone()),
        MockQueue::new(),
        MockStateMachine,
    );

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([disabled_user]),
    }
    .into_request();

    let result = service.send_notification(request).await.unwrap();

    // User with type disabled should be excluded, no valid recipients remain
    assert!(result.is_none());
}

#[tokio::test]
async fn test_queue_message_conn_gateway_only() {
    use std::sync::Arc;

    let queue = Arc::new(MockQueue::new());
    let service =
        NotificationIngressService::new(MockRepository::new(), queue.clone(), MockStateMachine);

    let recipient = test_user_id("[email protected]");
    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([recipient.clone()]),
    }
    .into_request()
    .with_conn_gateway();

    service.send_notification(request).await.unwrap();

    let published = queue.get_published();
    assert_eq!(published.len(), 1);

    let msg = &published[0];
    assert_eq!(msg["message_type"], "test_notification");
    assert!(msg["content"]["ConnGateway"].is_object());
}

#[tokio::test]
async fn test_queue_message_email_per_recipient() {
    use std::sync::Arc;

    let queue = Arc::new(MockQueue::new());
    let service =
        NotificationIngressService::new(MockRepository::new(), queue.clone(), MockStateMachine);

    let recipient1 = test_user_id("[email protected]");
    let recipient2 = test_user_id("[email protected]");
    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([recipient1.clone(), recipient2.clone()]),
    }
    .into_request()
    .with_email();

    service.send_notification(request).await.unwrap();

    let published = queue.get_published();
    // Email is 1:1, so we should have 2 messages (one per recipient)
    assert_eq!(published.len(), 2);

    for msg in &published {
        assert_eq!(msg["message_type"], "test_notification");
        assert!(msg["content"]["Email"].is_object());
    }
}

#[tokio::test]
async fn test_queue_message_multiple_channels() {
    use std::sync::Arc;

    let recipient = test_user_id("[email protected]");
    let queue = Arc::new(MockQueue::new());
    let repo = MockRepository::new().with_device_endpoint(
        recipient.clone(),
        DeviceEndpoint::Ios("arn:aws:sns:test".to_string()),
    );
    let service = NotificationIngressService::new(repo, queue.clone(), MockStateMachine);

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([recipient.clone()]),
    }
    .into_request()
    .with_conn_gateway()
    .with_apns()
    .with_email();

    service.send_notification(request).await.unwrap();

    let published = queue.get_published();
    // Should have 3 messages: 1 conn_gateway + 1 iOS + 1 email
    assert_eq!(published.len(), 3);

    let has_conn_gateway = published
        .iter()
        .any(|m| m["content"]["ConnGateway"].is_object());
    let has_ios = published.iter().any(|m| m["content"]["Ios"].is_object());
    let has_email = published.iter().any(|m| m["content"]["Email"].is_object());

    assert!(has_conn_gateway, "Should have ConnGateway message");
    assert!(has_ios, "Should have iOS message");
    assert!(has_email, "Should have Email message");
}

#[tokio::test]
async fn test_apns_enqueues_correct_data_for_multiple_users() {
    use std::sync::Arc;

    let user1 = test_user_id("[email protected]");
    let user2 = test_user_id("[email protected]");
    let user3 = test_user_id("[email protected]");

    let queue = Arc::new(MockQueue::new());
    let repo = MockRepository::new()
        .with_device_endpoint(
            user1.clone(),
            DeviceEndpoint::Ios(
                "arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice-device".to_string(),
            ),
        )
        .with_device_endpoint(
            user2.clone(),
            DeviceEndpoint::Ios(
                "arn:aws:sns:us-east-1:111:endpoint/APNS/app/bob-device".to_string(),
            ),
        )
        .with_device_endpoint(
            user2.clone(),
            DeviceEndpoint::Ios(
                "arn:aws:sns:us-east-1:111:endpoint/APNS/app/bob-device-2".to_string(),
            ),
        )
        .with_device_endpoint(
            user3.clone(),
            DeviceEndpoint::Ios(
                "arn:aws:sns:us-east-1:111:endpoint/APNS/app/charlie-device".to_string(),
            ),
        );

    let service = NotificationIngressService::new(repo, queue.clone(), MockStateMachine);

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("doc_123"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "You were mentioned".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([user1.clone(), user2.clone(), user3.clone()]),
    }
    .into_request()
    .with_apns();

    service.send_notification(request).await.unwrap();

    let published = queue.get_published();
    assert_eq!(
        published.len(),
        1,
        "APNS produces a single queue message for all recipients"
    );

    let msg = &published[0];
    assert_eq!(msg["message_type"], "test_notification");

    // The message should be an Ios variant
    let ios = &msg["content"]["Ios"];
    assert!(ios.is_object(), "Expected Ios notification channel");

    // Verify the APNS notification payload contains the notification data
    // push_notification_data is #[serde(flatten)]'d so fields appear directly on notif
    let apns_notif = &ios["notif"];
    assert_eq!(
        apns_notif["message"], "You were mentioned",
        "APNS payload should contain the flattened notification data"
    );

    // Verify message attributes
    let attrs = &ios["attributes"];
    assert_eq!(attrs["push_type"], "Alert");
    let expected_key = NotifCollapseKey::new("test").into_hashed().into_inner();
    assert_eq!(attrs["collapse_key"], expected_key);

    // Verify all device endpoints from all users are included (now keyed by user)
    let endpoints_map = ios["ios_device_endpoints"]
        .as_object()
        .expect("ios_device_endpoints should be an object keyed by user ID");

    // Collect all endpoints across all users
    let all_endpoints: Vec<&str> = endpoints_map
        .values()
        .flat_map(|user| {
            user["endpoints"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap())
        })
        .collect();

    assert_eq!(
        all_endpoints.len(),
        4,
        "Should include all 4 device endpoints across 3 users"
    );
    assert!(
        all_endpoints.contains(&"arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice-device"),
        "Should include alice's device"
    );
    assert!(
        all_endpoints.contains(&"arn:aws:sns:us-east-1:111:endpoint/APNS/app/bob-device"),
        "Should include bob's first device"
    );
    assert!(
        all_endpoints.contains(&"arn:aws:sns:us-east-1:111:endpoint/APNS/app/bob-device-2"),
        "Should include bob's second device"
    );
    assert!(
        all_endpoints.contains(&"arn:aws:sns:us-east-1:111:endpoint/APNS/app/charlie-device"),
        "Should include charlie's device"
    );
}

#[tokio::test]
async fn test_apns_collapse_key_stored_on_create() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");

    let repo = Arc::new(MockRepository::new().with_device_endpoint(
        user.clone(),
        DeviceEndpoint::Ios("arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice".to_string()),
    ));
    let queue = Arc::new(MockQueue::new());
    let service = NotificationIngressService::new(repo.clone(), queue, MockStateMachine);

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("doc_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([user]),
    }
    .into_request()
    .with_apns();

    service.send_notification(request).await.unwrap();

    let collapse_keys = repo.stored_collapse_keys.lock().unwrap();
    assert_eq!(collapse_keys.len(), 1);
    assert_eq!(
        collapse_keys[0].1,
        Some(NotifCollapseKey::new("test").into_hashed().into_inner()),
        "APNS collapse key should be stored when creating the notification"
    );
}

#[tokio::test]
async fn test_no_apns_collapse_key_when_apns_not_enabled() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");

    let repo = Arc::new(MockRepository::new());
    let queue = Arc::new(MockQueue::new());
    let service = NotificationIngressService::new(repo.clone(), queue, MockStateMachine);

    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("doc_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([user]),
    }
    .into_request()
    .with_conn_gateway();

    service.send_notification(request).await.unwrap();

    let collapse_keys = repo.stored_collapse_keys.lock().unwrap();
    assert_eq!(collapse_keys.len(), 1);
    assert_eq!(
        collapse_keys[0].1, None,
        "No APNS collapse key should be stored when APNS is not enabled"
    );
}

// ============================================================================
// Egress Service Tests
// ============================================================================

/// Mock realtime sender that always succeeds.
struct MockRealtimeSender;

impl RealtimeSender for MockRealtimeSender {
    async fn send_notifications<'a, T: Serialize + Send + Sync>(
        &self,
        _recipients: &[MacroUserIdStr<'a>],
        _notification: &T,
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        Ok(HashSet::new())
    }
}

/// Mock mobile push sender.
struct MockMobileSender;

impl NotificationSender for MockMobileSender {
    async fn send_ios_push_notification<T: Serialize + Send + Sync>(
        &self,
        _endpoint_arn: &str,
        _notification: &crate::domain::models::apple::APNSPushNotification<T>,
        _attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        Ok("mock-message-id".to_string())
    }

    async fn send_android_push_notification<T: Serialize + Send + Sync>(
        &self,
        _endpoint_arn: &str,
        _notification: &crate::domain::models::android::FCMMessage<T>,
        _attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        Ok("mock-message-id".to_string())
    }
}

/// Mock email sender.
struct MockEmailSender;

impl EmailSender for MockEmailSender {
    async fn send_email(
        &self,
        _recipient: MacroUserIdStr<'_>,
        _notification_type: &str,
        _content: &crate::domain::models::queue_message::EmailContent,
    ) -> Result<(), Report> {
        Ok(())
    }
}

/// Mock rate limit port that can be configured to allow or exceed.
struct MockRateLimitPort {
    should_exceed: bool,
}

impl rate_limit::RateLimitPort for MockRateLimitPort {
    async fn check(
        &self,
        key: RateLimitKey,
        config: RateLimitConfig,
    ) -> Result<RateLimitResult, Report> {
        if self.should_exceed {
            Ok(RateLimitResult::Err(RateLimitExceeded {
                current_count: config.max_count.saturating_add(1),
                max_count: config.max_count,
                retry_after: config.window,
            }))
        } else {
            Ok(RateLimitResult::Ok(RateLimitOk::new_testing_value(
                1, key, config,
            )))
        }
    }

    async fn decrement(&self, _key: &RateLimitKey) -> Result<(), Report> {
        Ok(())
    }
}

fn allowing_rate_limiter() -> rate_limit::RateLimitServiceImpl<MockRateLimitPort> {
    rate_limit::RateLimitServiceImpl {
        repo: MockRateLimitPort {
            should_exceed: false,
        },
    }
}

fn exceeding_rate_limiter() -> rate_limit::RateLimitServiceImpl<MockRateLimitPort> {
    rate_limit::RateLimitServiceImpl {
        repo: MockRateLimitPort {
            should_exceed: true,
        },
    }
}

/// Mock egress state machine that forwards sends without recording message IDs or batching.
struct MockDigestBatcher;

impl DigestBatcher for MockDigestBatcher {
    async fn add_to_digest(
        &self,
        _notification: &UserNotificationRow<serde_json::Value>,
        _send_after: Duration,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn claim_ready_digest(&self) -> Result<ClaimResult<DigestBatch>, Report> {
        Ok(ClaimResult::Empty)
    }
}

struct MockEgressStateMachine;

impl crate::domain::models::email_notification_digest::BulkDigestEgressStateMachine
    for MockEgressStateMachine
{
    async fn continue_machine<
        N: crate::domain::models::email_notification_digest::ports::NotificationSendChecker,
    >(
        &self,
        req: crate::domain::models::email_notification_digest::ResumeMachineBRequest<N>,
    ) -> (
        Vec<Result<N::Ok, N::Err>>,
        either::Either<
            crate::domain::models::email_notification_digest::DontSend,
            Result<crate::domain::models::email_notification_digest::BatchSend<()>, Report>,
        >,
    ) {
        let mut results = Vec::with_capacity(req.send_notifs.len());
        let mut any_succeeded = false;

        for send_notif in req.send_notifs {
            match send_notif.send_notification().await {
                Ok(ok) => {
                    results.push(Ok(ok));
                    any_succeeded = true;
                }
                Err(err) => {
                    results.push(Err(err));
                }
            }
        }

        let decision = if any_succeeded {
            either::Either::Left(crate::domain::models::email_notification_digest::DontSend::new())
        } else {
            either::Either::Right(Ok(
                crate::domain::models::email_notification_digest::BatchSend::from_inner(()),
            ))
        };

        (results, decision)
    }
}

fn create_egress_service<R: rate_limit::RateLimitService>(
    rate_limiter: R,
) -> NotificationEgressService<
    MockQueue,
    MockRepository,
    MockRealtimeSender,
    MockMobileSender,
    MockEmailSender,
    R,
    MockEgressStateMachine,
    MockDigestBatcher,
> {
    NotificationEgressService {
        queue: MockQueue::new(),
        repository: MockRepository::new(),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter,
        state_machine: MockEgressStateMachine,
        digest_batcher: MockDigestBatcher,
    }
}

fn create_mock_notif<T: Notification>(meta: T) -> RealtimeNotif<TaggedContent<T>> {
    RealtimeNotif {
        notification_id: Uuid::nil(),
        notification_event_type: "testing".to_string(),
        entity: EntityType::Document.with_entity_str("testing"),
        sent: false,
        state: crate::domain::models::NotificationState::Unseen,
        created_at: Utc::now(),
        viewed_at: None,
        updated_at: Utc::now(),
        deleted_at: None,
        notification_metadata: TaggedContent::new(meta),
        sender_id: None,
    }
}

#[tokio::test]
async fn test_egress_rate_limit_exceeded() {
    let service = create_egress_service(exceeding_rate_limiter());

    let recipient = test_user_id("[email protected]");
    let email = EmailCreateBundle::new(&TestNotification {
        message: "Hello".to_string(),
    })
    .with_recipient(recipient);
    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::Email(email),
    );

    let results = service.deliver_notification(message).await;

    // Should have exactly one error result for rate limit exceeded
    assert_eq!(results.len(), 1);
    assert!(results[0].is_err());

    let err = results[0].as_ref().unwrap_err();
    assert!(
        err.to_string().contains("rate limit"),
        "Error should mention rate limit: {}",
        err
    );
}

#[tokio::test]
async fn test_egress_rate_limit_allowed() {
    let service = create_egress_service(allowing_rate_limiter());

    let recipient = test_user_id("[email protected]");
    let email = EmailCreateBundle::new(&TestNotification {
        message: "Hello".to_string(),
    })
    .with_recipient(recipient);
    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::Email(email),
    );

    let results = service.deliver_notification(message).await;

    // Should succeed
    assert_eq!(results.len(), 1);
    assert!(results[0].is_ok());
}

#[tokio::test]
async fn test_egress_conn_gateway_not_rate_limited() {
    let service = create_egress_service(exceeding_rate_limiter());

    let recipient = test_user_id("[email protected]");
    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::ConnGateway(
            ConnGatewayNotification {
                notif: create_mock_notif(TestNotification {
                    message: "Hello".to_string(),
                }),
                recipients: vec![recipient],
            }
            .testing_to_value(),
        ),
    );

    let results = service.deliver_notification(message).await;

    // Should succeed - ConnGateway messages are not rate limited
    assert_eq!(results.len(), 1);
    assert!(results[0].is_ok());
}

// ============================================================================
// Notification Reader Tests
// ============================================================================

#[tokio::test]
async fn test_get_entity_notifications_batch_skips_invalid_tagged_metadata() {
    let user = test_user_id("[email protected]");
    let entity_ref = EntityType::Document.with_entity_string("doc-1".to_string());
    let now = Utc::now();
    let valid_id = Uuid::now_v7();
    let invalid_id = Uuid::now_v7();
    let mut valid = updated_notification(user.clone(), valid_id, false, None, now);
    valid.notification_metadata = json!({ "message": "hello" });
    let mut invalid = updated_notification(user.clone(), invalid_id, false, None, now);
    invalid.notification_metadata = json!({ "unexpected": true });

    let service = NotificationReaderService {
        repository: Arc::new(
            MockRepository::new()
                .with_entity_notifications(entity_ref.clone(), vec![invalid, valid]),
        ),
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notifications = service
        .get_entity_notifications_batch::<TestNotifEvent>(
            user,
            vec![entity_ref.clone()],
            Default::default(),
        )
        .await
        .expect("invalid metadata does not fail the batch");

    assert_eq!(notifications[&entity_ref].len(), 1);
    let notification = &notifications[&entity_ref][0];
    assert_eq!(notification.notification_id, valid_id);
    assert_eq!(
        notification.notification_metadata,
        TestNotifEvent::TestNotification(TestNotification {
            message: "hello".to_string(),
        })
    );
}

#[tokio::test]
async fn test_get_entity_notifications_batch_deserializes_tagged_metadata() {
    let user = test_user_id("[email protected]");
    let entity_ref = EntityType::Document.with_entity_string("doc-1".to_string());
    let notification_id = Uuid::now_v7();
    let now = Utc::now();
    let mut notification = updated_notification(user.clone(), notification_id, false, None, now);
    notification.notification_metadata = json!({ "message": "hello" });

    let service = NotificationReaderService {
        repository: Arc::new(
            MockRepository::new().with_entity_notifications(entity_ref.clone(), vec![notification]),
        ),
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notifications = service
        .get_entity_notifications_batch::<TestNotifEvent>(
            user,
            vec![entity_ref.clone()],
            Default::default(),
        )
        .await
        .expect("tagged notification metadata deserializes");

    let notification = &notifications[&entity_ref][0];
    assert_eq!(notification.notification_id, notification_id);
    assert_eq!(
        notification.notification_metadata,
        TestNotifEvent::TestNotification(TestNotification {
            message: "hello".to_string(),
        })
    );
}

// ============================================================================
// Mark Notifications Seen Tests
// ============================================================================

#[tokio::test]
async fn test_mark_seen_clears_push_only_when_enabled() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif_id = Uuid::now_v7();

    let repo = Arc::new(
        MockRepository::new()
            .with_basic_notification(notif_id, "collapse_key_1".to_string())
            .with_device_endpoint(
                user.clone(),
                DeviceEndpoint::Ios(
                    "arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice".to_string(),
                ),
            ),
    );
    let queue = Arc::new(MockQueue::new());
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: queue.clone(),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notification_ids = [notif_id];
    service
        .update_notifications(UpdateNotificationsRequest {
            user_id: user.clone(),
            notification_ids: &notification_ids,
            status: NotificationStatus::Seen,
        })
        .await
        .unwrap();

    // Verify DB was updated
    let mark_seen_calls = repo.mark_seen_calls.lock().unwrap();
    assert_eq!(mark_seen_calls.len(), 1);
    assert_eq!(mark_seen_calls[0].1, vec![notif_id]);

    let published = queue.get_published();
    if !cfg!(feature = "clear_ios_push") {
        assert!(
            published.is_empty(),
            "Should not clear push when clear_ios_push is disabled"
        );
        return;
    }

    // Verify queue message was published
    assert_eq!(published.len(), 1);

    let msg = &published[0];
    assert_eq!(msg["message_type"], "clear_push_notification");

    // Should be an Ios variant with background push
    let ios = &msg["content"]["Ios"];
    assert!(ios.is_object(), "Expected Ios notification channel");

    // Verify silent background push payload
    let aps = &ios["notif"]["aps"];
    assert_eq!(aps["content-available"], 1);
    assert!(aps.get("alert").is_none() || aps["alert"].is_null());

    // Verify collapse key in attributes
    let attrs = &ios["attributes"];
    assert_eq!(attrs["push_type"], "Background");
    assert_eq!(attrs["collapse_key"], "collapse_key_1");

    // Verify identifier in custom data
    assert_eq!(ios["notif"]["identifier"], "collapse_key_1");

    // Verify device endpoint (now keyed by user)
    let endpoints_map = ios["ios_device_endpoints"].as_object().unwrap();
    let all_endpoints: Vec<&str> = endpoints_map
        .values()
        .flat_map(|user| {
            user["endpoints"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap())
        })
        .collect();
    assert_eq!(all_endpoints.len(), 1);
    assert_eq!(
        all_endpoints[0],
        "arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice"
    );
}

#[tokio::test]
async fn test_mark_seen_skips_push_when_no_collapse_key() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif_id = Uuid::now_v7();

    // No basic notifications with collapse keys (DB query filters them out)
    let repo = Arc::new(MockRepository::new().with_device_endpoint(
        user.clone(),
        DeviceEndpoint::Ios("arn:aws:sns:us-east-1:111:endpoint/APNS/app/bob".to_string()),
    ));
    let queue = Arc::new(MockQueue::new());
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: queue.clone(),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notification_ids = [notif_id];
    service
        .update_notifications(UpdateNotificationsRequest {
            user_id: user.clone(),
            notification_ids: &notification_ids,
            status: NotificationStatus::Seen,
        })
        .await
        .unwrap();

    // DB should still be updated
    let mark_seen_calls = repo.mark_seen_calls.lock().unwrap();
    assert_eq!(mark_seen_calls.len(), 1);

    // But no queue message should be published
    let published = queue.get_published();
    assert!(
        published.is_empty(),
        "Should not publish when no collapse keys"
    );
}

#[tokio::test]
async fn test_update_notifications_and_return_preserves_requested_order() {
    let user = test_user_id("[email protected]");
    let first = Uuid::now_v7();
    let second = Uuid::now_v7();
    let repo = Arc::new(MockRepository::new());
    let service = NotificationReaderService {
        repository: repo,
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };
    let notification_ids = [second, first];

    let updated = service
        .update_notifications_and_return::<TestNotifEvent>(UpdateNotificationsRequest {
            user_id: user.clone(),
            notification_ids: &notification_ids,
            status: NotificationStatus::Done(false),
        })
        .await
        .unwrap();

    assert_eq!(
        updated
            .iter()
            .map(|notification| notification.notification_id)
            .collect::<Vec<_>>(),
        notification_ids
    );
    assert!(
        updated
            .iter()
            .all(|notification| notification.owner_id == user)
    );
    assert!(updated.iter().all(|notification| matches!(
        &notification.notification_metadata,
        TestNotifEvent::TestNotification(TestNotification { message })
            if message == "updated notification"
    )));
}

#[tokio::test]
async fn test_update_notifications_and_return_skips_invalid_tagged_metadata() {
    let user = test_user_id("[email protected]");
    let valid_id = Uuid::now_v7();
    let invalid_id = Uuid::now_v7();
    let now = Utc::now();
    let valid = updated_notification(user.clone(), valid_id, false, None, now);
    let mut invalid = updated_notification(user.clone(), invalid_id, false, None, now);
    invalid.notification_event_type = "unknown_notification".to_string();

    let repo = Arc::new(MockRepository::new().with_updated_notifications(vec![invalid, valid]));
    let service = NotificationReaderService {
        repository: repo,
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let updated = service
        .update_notifications_and_return::<TestNotifEvent>(UpdateNotificationsRequest {
            user_id: user,
            notification_ids: &[invalid_id, valid_id],
            status: NotificationStatus::Done(false),
        })
        .await
        .expect("invalid metadata does not fail an already-committed update");

    assert_eq!(updated.len(), 1);
    assert_eq!(updated[0].notification_id, valid_id);
}

#[tokio::test]
async fn test_update_notifications_for_entities_uses_single_batch_lookup() {
    let user = test_user_id("[email protected]");
    let first = Uuid::now_v7();
    let second = Uuid::now_v7();
    let repo = Arc::new(MockRepository::new().with_entity_notification_ids(vec![first, second]));
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let updated = service
        .update_notifications_for_entities::<TestNotifEvent>(
            UpdateNotificationsForEntitiesRequest {
                user_id: user.clone(),
                entities: vec![
                    EntityType::ChannelMessage.with_entity_str("message-1"),
                    EntityType::Document.with_entity_str("document-1"),
                ],
                status: NotificationStatus::Seen,
            },
        )
        .await
        .unwrap();

    assert_eq!(
        updated
            .iter()
            .map(|notification| notification.notification_id)
            .collect::<Vec<_>>(),
        vec![first, second]
    );
    assert_eq!(
        repo.entity_lookup_calls.lock().unwrap().as_slice(),
        [(
            user.to_string(),
            vec![
                (EntityType::ChannelMessage, "message-1".to_string()),
                (EntityType::Document, "document-1".to_string()),
            ],
        )]
    );
    assert_eq!(
        repo.mark_seen_calls.lock().unwrap().as_slice(),
        [(user.to_string(), vec![first, second])]
    );
    assert_eq!(
        repo.entity_lookup_filters.lock().unwrap().as_slice(),
        [(vec![crate::domain::models::NotificationState::Unseen], true)]
    );
}

#[tokio::test]
async fn test_update_notifications_for_entities_supports_done_status() {
    let user = test_user_id("[email protected]");
    let notification_id = Uuid::now_v7();
    let repo = Arc::new(MockRepository::new().with_entity_notification_ids(vec![notification_id]));
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    service
        .update_notifications_for_entities::<TestNotifEvent>(
            UpdateNotificationsForEntitiesRequest {
                user_id: user.clone(),
                entities: vec![EntityType::Document.with_entity_str("document-1")],
                status: NotificationStatus::Done(true),
            },
        )
        .await
        .unwrap();

    assert_eq!(
        repo.mark_done_calls.lock().unwrap().as_slice(),
        [(user.to_string(), vec![notification_id], true)]
    );
    assert_eq!(
        repo.entity_lookup_filters.lock().unwrap().as_slice(),
        [(
            crate::domain::models::NotificationState::ACTIVE.to_vec(),
            false
        )]
    );
}

#[tokio::test]
async fn test_update_notifications_for_entities_noops_when_no_notifications_match() {
    let user = test_user_id("[email protected]");
    let repo = Arc::new(MockRepository::new());
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: Arc::new(MockQueue::new()),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let updated = service
        .update_notifications_for_entities::<TestNotifEvent>(
            UpdateNotificationsForEntitiesRequest {
                user_id: user,
                entities: vec![EntityType::Document.with_entity_str("doc-without-notifications")],
                status: NotificationStatus::Done(true),
            },
        )
        .await
        .unwrap();

    assert!(updated.is_empty());
    assert!(repo.mark_done_calls.lock().unwrap().is_empty());
    assert!(repo.basic_notification_calls.lock().unwrap().is_empty());
}

#[tokio::test]
async fn test_mark_seen_skips_push_when_no_device_endpoints() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif_id = Uuid::now_v7();

    let repo = Arc::new(
        MockRepository::new().with_basic_notification(notif_id, "collapse_key_1".to_string()),
        // No device endpoints registered
    );
    let queue = Arc::new(MockQueue::new());
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: queue.clone(),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notification_ids = [notif_id];
    service
        .update_notifications(UpdateNotificationsRequest {
            user_id: user.clone(),
            notification_ids: &notification_ids,
            status: NotificationStatus::Seen,
        })
        .await
        .unwrap();

    // DB should still be updated
    let mark_seen_calls = repo.mark_seen_calls.lock().unwrap();
    assert_eq!(mark_seen_calls.len(), 1);

    // But no queue message should be published
    let published = queue.get_published();
    assert!(
        published.is_empty(),
        "Should not publish when no device endpoints"
    );
}

#[tokio::test]
async fn test_mark_done_updates_db_and_clears_push_only_when_enabled() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif_id = Uuid::now_v7();

    let repo = Arc::new(
        MockRepository::new()
            .with_basic_notification(notif_id, "collapse_key_1".to_string())
            .with_device_endpoint(
                user.clone(),
                DeviceEndpoint::Ios(
                    "arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice".to_string(),
                ),
            ),
    );
    let queue = Arc::new(MockQueue::new());
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: queue.clone(),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notification_ids = [notif_id];
    service
        .update_notifications(UpdateNotificationsRequest {
            user_id: user.clone(),
            notification_ids: &notification_ids,
            status: NotificationStatus::Done(true),
        })
        .await
        .unwrap();

    // Verify done was called (not seen)
    let mark_seen_calls = repo.mark_seen_calls.lock().unwrap();
    assert!(mark_seen_calls.is_empty(), "Should not call mark_seen");

    let mark_done_calls = repo.mark_done_calls.lock().unwrap();
    assert_eq!(mark_done_calls.len(), 1);
    assert_eq!(mark_done_calls[0].1, vec![notif_id]);
    assert!(mark_done_calls[0].2, "Should mark as done=true");

    let published = queue.get_published();
    if cfg!(feature = "clear_ios_push") {
        assert_eq!(published.len(), 1);
        assert_eq!(published[0]["message_type"], "clear_push_notification");
    } else {
        assert!(
            published.is_empty(),
            "Should not clear push when clear_ios_push is disabled"
        );
    }
}

#[tokio::test]
async fn test_mark_undone_updates_db_no_push_clear() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif_id = Uuid::now_v7();

    let repo = Arc::new(
        MockRepository::new()
            .with_basic_notification(notif_id, "collapse_key_1".to_string())
            .with_device_endpoint(
                user.clone(),
                DeviceEndpoint::Ios(
                    "arn:aws:sns:us-east-1:111:endpoint/APNS/app/alice".to_string(),
                ),
            ),
    );
    let queue = Arc::new(MockQueue::new());
    let service = NotificationReaderService {
        repository: repo.clone(),
        queue: queue.clone(),
        sns_endpoint: MockSnsEndpoint,
        platform_config: test_platform_config(),
        realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
    };

    let notification_ids = [notif_id];
    service
        .update_notifications(UpdateNotificationsRequest {
            user_id: user.clone(),
            notification_ids: &notification_ids,
            status: NotificationStatus::Done(false),
        })
        .await
        .unwrap();

    // Verify done was called with false
    let mark_done_calls = repo.mark_done_calls.lock().unwrap();
    assert_eq!(mark_done_calls.len(), 1);
    assert!(!mark_done_calls[0].2, "Should mark as done=false");

    // Verify NO push clearing was published (Done(false) should not clear push)
    let published = queue.get_published();
    assert!(
        published.is_empty(),
        "Should not clear push when marking undone"
    );
}

/// Mock mobile sender that tracks attempted endpoints and can fail specific ones.
struct TrackingMobileSender {
    /// Endpoints that were attempted (for verification).
    attempted_endpoints: Mutex<Vec<String>>,
    /// Endpoints that should fail when attempted.
    failing_endpoints: HashSet<String>,
    /// Endpoints that should fail as disabled or deleted.
    unavailable_endpoints: HashSet<String>,
}

impl TrackingMobileSender {
    fn new(failing_endpoints: HashSet<String>) -> Self {
        Self {
            attempted_endpoints: Mutex::new(Vec::new()),
            failing_endpoints,
            unavailable_endpoints: HashSet::new(),
        }
    }

    fn with_unavailable_endpoints(mut self, endpoints: HashSet<String>) -> Self {
        self.unavailable_endpoints = endpoints;
        self
    }

    fn get_attempted_endpoints(&self) -> Vec<String> {
        self.attempted_endpoints.lock().unwrap().clone()
    }
}

impl NotificationSender for TrackingMobileSender {
    async fn send_ios_push_notification<T: Serialize + Send + Sync>(
        &self,
        endpoint_arn: &str,
        _notification: &crate::domain::models::apple::APNSPushNotification<T>,
        _attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        // Track that this endpoint was attempted
        self.attempted_endpoints
            .lock()
            .unwrap()
            .push(endpoint_arn.to_string());

        // Fail if this endpoint is in the failing set
        if self.failing_endpoints.contains(endpoint_arn) {
            rootcause::bail!("Simulated APNS failure for endpoint: {}", endpoint_arn);
        }
        if self.unavailable_endpoints.contains(endpoint_arn) {
            return Err(report!("Simulated disabled endpoint: {}", endpoint_arn)
                .context(crate::domain::models::mobile::PushEndpointUnavailable)
                .into_dynamic());
        }

        Ok(format!("msg-id-{endpoint_arn}"))
    }

    async fn send_android_push_notification<T: Serialize + Send + Sync>(
        &self,
        _endpoint_arn: &str,
        _notification: &crate::domain::models::android::FCMMessage<T>,
        _attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        Ok("mock-android-msg-id".to_string())
    }
}

#[tokio::test]
async fn test_egress_ios_attempts_all_endpoints_even_if_some_fail() {
    use crate::domain::models::apple::{APNSPushNotification, Aps};
    use crate::domain::models::mobile::{MessageAttributes, PushType};
    use crate::domain::models::queue_message::{APNSTargets, UserApnsEndpoints};

    let endpoint1 = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/device1";
    let endpoint2 = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/device2";
    let endpoint3 = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/device3";
    let endpoint4 = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/device4";

    // Configure endpoints 1 and 3 to fail
    let failing_endpoints: HashSet<String> = [endpoint1.to_string(), endpoint3.to_string()].into();

    let mobile_sender = std::sync::Arc::new(TrackingMobileSender::new(failing_endpoints));
    let service = NotificationEgressService {
        queue: MockQueue::new(),
        repository: MockRepository::new(),
        realtime: MockRealtimeSender,
        mobile: mobile_sender.clone(),
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: MockDigestBatcher,
    };

    let user1 = test_user_id("[email protected]");
    let user2 = test_user_id("[email protected]");
    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::Ios(Box::new(APNSTargets {
            notif: APNSPushNotification {
                aps: Aps::default(),
                push_notification_data: json!({"message": "Hello"}),
            },
            attributes: MessageAttributes {
                push_type: PushType::Alert,
                collapse_key: "test_collapse".to_string(),
            },
            ios_device_endpoints: HashMap::from([
                (
                    user1,
                    UserApnsEndpoints {
                        android_endpoints: Vec::new(),
                        endpoints: vec![endpoint1.to_string(), endpoint2.to_string()],
                        digest_state: None,
                    },
                ),
                (
                    user2,
                    UserApnsEndpoints {
                        android_endpoints: Vec::new(),
                        endpoints: vec![endpoint3.to_string(), endpoint4.to_string()],
                        digest_state: None,
                    },
                ),
            ]),
        })),
    );

    let results = service.deliver_notification(message).await;

    // Verify ALL 4 endpoints were attempted
    let attempted = mobile_sender.get_attempted_endpoints();
    assert_eq!(
        attempted.len(),
        4,
        "Should attempt delivery to all 4 endpoints, but only attempted: {:?}",
        attempted
    );
    assert!(
        attempted.contains(&endpoint1.to_string()),
        "Should attempt endpoint1"
    );
    assert!(
        attempted.contains(&endpoint2.to_string()),
        "Should attempt endpoint2"
    );
    assert!(
        attempted.contains(&endpoint3.to_string()),
        "Should attempt endpoint3"
    );
    assert!(
        attempted.contains(&endpoint4.to_string()),
        "Should attempt endpoint4"
    );

    // Verify we got 4 results (one per endpoint)
    assert_eq!(results.len(), 4, "Should have 4 results (one per endpoint)");

    // Verify 2 succeeded and 2 failed
    let successes = results.iter().filter(|r| r.is_ok()).count();
    let failures = results.iter().filter(|r| r.is_err()).count();
    assert_eq!(successes, 2, "Should have 2 successful deliveries");
    assert_eq!(failures, 2, "Should have 2 failed deliveries");
}

#[tokio::test]
async fn test_egress_removes_unavailable_push_endpoints() {
    use crate::domain::models::apple::{APNSPushNotification, Aps};
    use crate::domain::models::mobile::{MessageAttributes, PushType};
    use crate::domain::models::queue_message::{APNSTargets, UserApnsEndpoints};

    let healthy = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/healthy";
    let disabled = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/disabled";
    let flaky = "arn:aws:sns:us-east-1:111:endpoint/APNS/app/flaky";

    let mobile_sender = std::sync::Arc::new(
        TrackingMobileSender::new([flaky.to_string()].into())
            .with_unavailable_endpoints([disabled.to_string()].into()),
    );
    let repository = Arc::new(MockRepository::new());
    let service = NotificationEgressService {
        queue: MockQueue::new(),
        repository: repository.clone(),
        realtime: MockRealtimeSender,
        mobile: mobile_sender.clone(),
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: MockDigestBatcher,
    };

    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::Ios(Box::new(APNSTargets {
            notif: APNSPushNotification {
                aps: Aps::default(),
                push_notification_data: json!({"message": "Hello"}),
            },
            attributes: MessageAttributes {
                push_type: PushType::Alert,
                collapse_key: "test_collapse".to_string(),
            },
            ios_device_endpoints: HashMap::from([
                (
                    test_user_id("[email protected]"),
                    UserApnsEndpoints {
                        android_endpoints: Vec::new(),
                        endpoints: vec![healthy.to_string(), disabled.to_string()],
                        digest_state: None,
                    },
                ),
                (
                    test_user_id("[email protected]"),
                    UserApnsEndpoints {
                        android_endpoints: Vec::new(),
                        endpoints: vec![flaky.to_string()],
                        digest_state: None,
                    },
                ),
            ]),
        })),
    );

    let results = service.deliver_notification(message).await;

    assert_eq!(results.iter().filter(|r| r.is_ok()).count(), 1);
    assert_eq!(mobile_sender.get_attempted_endpoints().len(), 3);
    assert_eq!(
        *repository.deleted_device_endpoints.lock().unwrap(),
        vec![disabled.to_string()],
        "only the disabled endpoint's registration should be removed"
    );
}

// --- poll_email_digests tests ---

struct ReadyDigestBatcher {
    batch: Mutex<Option<DigestBatch>>,
}

impl DigestBatcher for ReadyDigestBatcher {
    async fn add_to_digest(
        &self,
        _notification: &UserNotificationRow<serde_json::Value>,
        _send_after: Duration,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn claim_ready_digest(&self) -> Result<ClaimResult<DigestBatch>, Report> {
        match self.batch.lock().unwrap().take() {
            Some(batch) => Ok(ClaimResult::Ready(batch)),
            None => Ok(ClaimResult::Empty),
        }
    }
}

#[tokio::test]
async fn test_poll_email_digests_sends_email_for_ready_batch() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif = UserNotificationRow {
        owner_id: user.clone(),
        notification_id: Uuid::nil(),
        notification_event_type: "test_notification".to_string(),
        entity: EntityType::Document.with_entity_str("doc-1"),
        sent: false,
        state: crate::domain::models::NotificationState::Unseen,
        created_at: Utc::now(),
        viewed_at: None,
        updated_at: Utc::now(),
        deleted_at: None,
        notification_metadata: serde_json::to_value(TestNotification {
            message: "hello from digest".to_string(),
        })
        .unwrap(),
        sender_id: None,
    };

    let batch = DigestBatch {
        user_id: user.clone(),
        notifications: vec![notif.into_tagged()],
    };

    let batcher = ReadyDigestBatcher {
        batch: Mutex::new(Some(batch)),
    };

    let queue = Arc::new(MockQueue::new());
    let service = NotificationEgressService {
        queue: queue.clone(),
        repository: MockRepository::new(),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: batcher,
    };

    fn digest_to_notif(batch: DigestBatch) -> Result<TestNotification, Report> {
        Ok(TestNotification {
            message: format!("You have {} notification(s)", batch.notifications.len()),
        })
    }

    service.poll_email_digests(&digest_to_notif).await.unwrap();

    let published = queue.get_published();
    assert_eq!(published.len(), 1);
    assert!(published[0]["content"]["Email"].is_object());
}

#[tokio::test]
async fn test_poll_email_digests_skips_when_all_notifications_ineligible() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif = UserNotificationRow {
        owner_id: user.clone(),
        notification_id: Uuid::nil(),
        notification_event_type: "test_notification".to_string(),
        entity: EntityType::Document.with_entity_str("doc-1"),
        sent: false,
        state: crate::domain::models::NotificationState::Unseen,
        created_at: Utc::now(),
        viewed_at: None,
        updated_at: Utc::now(),
        deleted_at: None,
        notification_metadata: serde_json::to_value(TestNotification {
            message: "already read".to_string(),
        })
        .unwrap(),
        sender_id: None,
    };

    let batcher = ReadyDigestBatcher {
        batch: Mutex::new(Some(DigestBatch {
            user_id: user,
            notifications: vec![notif.into_tagged()],
        })),
    };

    let queue = Arc::new(MockQueue::new());
    let service = NotificationEgressService {
        queue: queue.clone(),
        repository: MockRepository::new().with_digest_eligible_notification_ids([]),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: batcher,
    };

    let digest_to_notif = |_batch: DigestBatch| -> Result<TestNotification, Report> {
        panic!("digest should not be rendered when no notifications remain eligible")
    };

    let result = service.poll_email_digests(&digest_to_notif).await.unwrap();

    assert!(matches!(result, ClaimResult::Ready(())));
    assert!(queue.get_published().is_empty());
}

#[tokio::test]
async fn test_poll_email_digests_filters_ineligible_notifications_before_rendering() {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    let user = test_user_id("[email protected]");
    let eligible_id = Uuid::parse_str("0193b1ea-c742-7589-893b-2b4a509c1e77").unwrap();
    let ineligible_id = Uuid::parse_str("0193b1ea-c742-7589-893b-2b4a509c1e78").unwrap();

    let make_notif = |notification_id, message: &str| UserNotificationRow {
        owner_id: user.clone(),
        notification_id,
        notification_event_type: "test_notification".to_string(),
        entity: EntityType::Document.with_entity_str("doc-1"),
        sent: false,
        state: crate::domain::models::NotificationState::Unseen,
        created_at: Utc::now(),
        viewed_at: None,
        updated_at: Utc::now(),
        deleted_at: None,
        notification_metadata: serde_json::to_value(TestNotification {
            message: message.to_string(),
        })
        .unwrap(),
        sender_id: None,
    };

    let batcher = ReadyDigestBatcher {
        batch: Mutex::new(Some(DigestBatch {
            user_id: user.clone(),
            notifications: vec![
                make_notif(eligible_id, "keep").into_tagged(),
                make_notif(ineligible_id, "drop").into_tagged(),
            ],
        })),
    };

    let rendered_count = Arc::new(AtomicUsize::new(0));
    let rendered_count_for_closure = rendered_count.clone();
    let digest_to_notif = move |batch: DigestBatch| -> Result<TestNotification, Report> {
        rendered_count_for_closure.store(batch.notifications.len(), Ordering::SeqCst);
        Ok(TestNotification {
            message: format!("You have {} notification(s)", batch.notifications.len()),
        })
    };

    let queue = Arc::new(MockQueue::new());
    let service = NotificationEgressService {
        queue: queue.clone(),
        repository: MockRepository::new().with_digest_eligible_notification_ids([eligible_id]),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: batcher,
    };

    service.poll_email_digests(&digest_to_notif).await.unwrap();

    assert_eq!(rendered_count.load(Ordering::SeqCst), 1);
    assert_eq!(queue.get_published().len(), 1);
}

#[tokio::test]
async fn test_poll_email_digests_noop_when_empty() {
    let service = create_egress_service(allowing_rate_limiter());

    fn digest_to_notif(_batch: DigestBatch) -> Result<TestNotification, Report> {
        panic!("should not be called when empty")
    }

    // MockDigestBatcher always returns Empty
    service.poll_email_digests(&digest_to_notif).await.unwrap();
}

// ============================================================================
// Poll-and-deliver tests (timeout + iOS delete)
// ============================================================================

/// Mock queue for poll_and_deliver tests that returns preset messages and tracks deletes.
struct EgressTestQueue {
    messages: Mutex<Vec<RawQueueMessage>>,
    deleted_handles: Mutex<Vec<String>>,
}

impl EgressTestQueue {
    fn new(messages: Vec<RawQueueMessage>) -> Self {
        Self {
            messages: Mutex::new(messages),
            deleted_handles: Mutex::new(Vec::new()),
        }
    }

    fn deleted_handles(&self) -> Vec<String> {
        self.deleted_handles.lock().unwrap().clone()
    }
}

impl NotificationQueue for EgressTestQueue {
    async fn publish<'a, T: Serialize + Send + Sync, U: Serialize + Send + Sync>(
        &self,
        _messages: Vec<QueueMessage<'a, T, U>>,
    ) -> Result<(), Report> {
        Ok(())
    }

    async fn receive_messages(&self) -> Result<Vec<RawQueueMessage>, Report> {
        Ok(self.messages.lock().unwrap().drain(..).collect())
    }

    async fn delete_message(&self, receipt_handle: &str) -> Result<(), Report> {
        self.deleted_handles
            .lock()
            .unwrap()
            .push(receipt_handle.to_string());
        Ok(())
    }
}

/// Realtime sender that hangs indefinitely (simulates a stuck connection).
struct HangingRealtimeSender;

impl RealtimeSender for HangingRealtimeSender {
    async fn send_notifications<'a, T: Serialize + Send + Sync>(
        &self,
        _recipients: &[MacroUserIdStr<'a>],
        _notification: &T,
    ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {
        // Sleep longer than DELIVERY_TIMEOUT to guarantee the timeout fires.
        tokio::time::sleep(super::egress::DELIVERY_TIMEOUT + Duration::from_secs(1)).await;
        Ok(HashSet::new())
    }
}

/// Mobile sender that always fails.
struct FailingMobileSender;

impl NotificationSender for FailingMobileSender {
    async fn send_ios_push_notification<T: Serialize + Send + Sync>(
        &self,
        _endpoint_arn: &str,
        _notification: &crate::domain::models::apple::APNSPushNotification<T>,
        _attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        rootcause::bail!("Simulated APNS failure")
    }

    async fn send_android_push_notification<T: Serialize + Send + Sync>(
        &self,
        _endpoint_arn: &str,
        _notification: &crate::domain::models::android::FCMMessage<T>,
        _attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        rootcause::bail!("Simulated FCM failure")
    }
}

#[tokio::test]
async fn test_poll_and_deliver_deletes_rate_limited_message() {
    let recipient = test_user_id("[email protected]");
    let email = EmailCreateBundle::new(&TestNotification {
        message: "Hello".to_string(),
    })
    .with_recipient(recipient);
    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::Email(email),
    );

    let queue = EgressTestQueue::new(vec![RawQueueMessage {
        body: message,
        receipt_handle: "receipt-rate-limited".to_string(),
    }]);
    let service = NotificationEgressService {
        queue,
        repository: MockRepository::new(),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: exceeding_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: MockDigestBatcher,
    };

    let results = service.poll_and_deliver().await;

    assert_eq!(results.len(), 1);
    assert!(results[0].is_err());
    assert_eq!(
        service.queue.deleted_handles(),
        vec!["receipt-rate-limited"],
        "rate-limited messages must be deleted instead of retried"
    );
}

#[tokio::test]
async fn test_poll_and_deliver_times_out_slow_delivery() {
    tokio::time::pause();

    let recipient = test_user_id("[email protected]");
    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::ConnGateway(
            ConnGatewayNotification {
                notif: create_mock_notif(TestNotification {
                    message: "Hello".to_string(),
                }),
                recipients: vec![recipient],
            }
            .testing_to_value(),
        ),
    );

    let queue = EgressTestQueue::new(vec![RawQueueMessage {
        body: message,
        receipt_handle: "receipt-timeout".to_string(),
    }]);

    let service = NotificationEgressService {
        queue,
        repository: MockRepository::new(),
        realtime: HangingRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: MockDigestBatcher,
    };

    let results = service.poll_and_deliver().await;

    assert_eq!(results.len(), 1);
    assert!(results[0].is_err());

    let err = results[0].as_ref().unwrap_err();
    assert!(
        err.to_string().contains("timeout"),
        "Expected timeout error, got: {}",
        err
    );

    // Timed out messages should NOT be deleted (not an iOS failure)
    let deleted = service.queue.deleted_handles();
    assert!(
        deleted.is_empty(),
        "Timed out message should not be deleted from queue"
    );
}

#[tokio::test]
async fn test_poll_and_deliver_deletes_message_when_all_ios_failures() {
    use crate::domain::models::apple::{APNSPushNotification, Aps};
    use crate::domain::models::mobile::{MessageAttributes, PushType};
    use crate::domain::models::queue_message::{APNSTargets, UserApnsEndpoints};

    let user = test_user_id("[email protected]");

    let message = QueueMessage::new_test(
        "test_notification".to_string(),
        NotificationChannel::Ios(Box::new(APNSTargets {
            notif: APNSPushNotification {
                aps: Aps::default(),
                push_notification_data: json!({"message": "Hello"}),
            },
            attributes: MessageAttributes {
                push_type: PushType::Alert,
                collapse_key: "test_collapse".to_string(),
            },
            ios_device_endpoints: HashMap::from([(
                user,
                UserApnsEndpoints {
                    android_endpoints: Vec::new(),
                    endpoints: vec![
                        "arn:endpoint/device1".to_string(),
                        "arn:endpoint/device2".to_string(),
                    ],
                    digest_state: None,
                },
            )]),
        })),
    );

    let queue = EgressTestQueue::new(vec![RawQueueMessage {
        body: message,
        receipt_handle: "receipt-ios".to_string(),
    }]);

    let service = NotificationEgressService {
        queue,
        repository: MockRepository::new(),
        realtime: MockRealtimeSender,
        mobile: FailingMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: MockDigestBatcher,
    };

    let results = service.poll_and_deliver().await;

    // All results should be failures
    assert!(!results.is_empty());
    assert!(results.iter().all(|r| r.is_err()));

    // Message should be deleted because all failures are iOS
    let deleted = service.queue.deleted_handles();
    assert_eq!(
        deleted,
        vec!["receipt-ios"],
        "Message should be deleted when all failures are iOS"
    );
}

// ============================================================================
// Ingress Queue Tests (SqsNotificationIngress + process_from_queue)
// ============================================================================

/// Mock ingress queue that tracks published messages.
struct MockIngressQueue {
    published: Mutex<Vec<crate::domain::models::queue_message::IngressQueueMessage>>,
}

impl MockIngressQueue {
    fn new() -> Self {
        Self {
            published: Mutex::new(Vec::new()),
        }
    }

    fn get_published_count(&self) -> usize {
        self.published.lock().unwrap().len()
    }
}

impl crate::domain::ports::NotificationIngressQueue for MockIngressQueue {
    async fn publish(
        &self,
        message: crate::domain::models::queue_message::IngressQueueMessage,
    ) -> Result<(), Report> {
        self.published.lock().unwrap().push(message);
        Ok(())
    }

    async fn receive_messages(
        &self,
    ) -> Result<Vec<crate::domain::models::queue_message::RawIngressQueueMessage>, Report> {
        Ok(Vec::new())
    }

    async fn delete_message(&self, _receipt_handle: &str) -> Result<(), Report> {
        Ok(())
    }
}

impl crate::domain::ports::NotificationIngressQueue for Arc<MockIngressQueue> {
    async fn publish(
        &self,
        message: crate::domain::models::queue_message::IngressQueueMessage,
    ) -> Result<(), Report> {
        (**self).publish(message).await
    }

    async fn receive_messages(
        &self,
    ) -> Result<Vec<crate::domain::models::queue_message::RawIngressQueueMessage>, Report> {
        (**self).receive_messages().await
    }

    async fn delete_message(&self, receipt_handle: &str) -> Result<(), Report> {
        (**self).delete_message(receipt_handle).await
    }
}

#[tokio::test]
async fn test_sqs_notification_ingress_publishes_to_queue() {
    use crate::domain::service::SqsNotificationIngress;

    let queue = Arc::new(MockIngressQueue::new());
    let ingress = SqsNotificationIngress {
        queue: queue.clone(),
    };

    let recipient = test_user_id("[email protected]");
    let request = SendNotificationRequestBuilder {
        notification_entity: model_entity::EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello via queue".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([recipient]),
    }
    .into_request()
    .with_conn_gateway();

    let result = ingress.send_notification(request).await.unwrap();

    // SqsNotificationIngress always returns Ok(None)
    assert!(result.is_none());

    // Verify message was published to the queue
    assert_eq!(queue.get_published_count(), 1);
}

#[tokio::test]
async fn test_process_from_queue_with_value_types() {
    let queue = Arc::new(MockQueue::new());
    let service =
        NotificationIngressService::new(MockRepository::new(), queue.clone(), MockStateMachine);

    let recipient = test_user_id("[email protected]");

    // Build a typed request, then type-erase it through IngressQueueMessage
    let typed_request = SendNotificationRequestBuilder {
        notification_entity: model_entity::EntityType::Document.with_entity_str("entity_1"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello from queue".to_string(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([recipient.clone()]),
    }
    .into_request()
    .with_conn_gateway();

    let ingress_msg =
        crate::domain::models::queue_message::IngressQueueMessage::from_request(&typed_request)
            .unwrap();

    // Process the type-erased request
    let result = service
        .process_from_queue(ingress_msg.request)
        .await
        .unwrap()
        .unwrap();

    assert!(result.notified_recipients.contains(&recipient));

    // Verify a queue message was published to the delivery queue
    let published = queue.get_published();
    assert_eq!(published.len(), 1);
    assert!(published[0]["content"]["ConnGateway"].is_object());
}

impl NotificationSender for std::sync::Arc<TrackingMobileSender> {
    async fn send_ios_push_notification<T: Serialize + Send + Sync>(
        &self,
        endpoint_arn: &str,
        notification: &crate::domain::models::apple::APNSPushNotification<T>,
        attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        (**self)
            .send_ios_push_notification(endpoint_arn, notification, attributes)
            .await
    }

    async fn send_android_push_notification<T: Serialize + Send + Sync>(
        &self,
        endpoint_arn: &str,
        notification: &crate::domain::models::android::FCMMessage<T>,
        attributes: &crate::domain::models::mobile::MessageAttributes,
    ) -> Result<String, Report> {
        (**self)
            .send_android_push_notification(endpoint_arn, notification, attributes)
            .await
    }
}

// ============================================================================
// Egress Type-Disabled Digest Guard Tests
// ============================================================================

#[tokio::test]
async fn test_poll_email_digests_skips_publish_when_user_disabled_type() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let notif = UserNotificationRow {
        owner_id: user.clone(),
        notification_id: Uuid::nil(),
        notification_event_type: "test_notification".to_string(),
        entity: EntityType::Document.with_entity_str("doc-1"),
        sent: false,
        state: crate::domain::models::NotificationState::Unseen,
        created_at: Utc::now(),
        viewed_at: None,
        updated_at: Utc::now(),
        deleted_at: None,
        notification_metadata: serde_json::to_value(TestNotification {
            message: "hello from digest".to_string(),
        })
        .unwrap(),
        sender_id: None,
    };

    let batch = DigestBatch {
        user_id: user.clone(),
        notifications: vec![notif.into_tagged()],
    };

    let batcher = ReadyDigestBatcher {
        batch: Mutex::new(Some(batch)),
    };

    let queue = Arc::new(MockQueue::new());
    let service = NotificationEgressService {
        queue: queue.clone(),
        repository: MockRepository::new().with_type_disabled_user(user),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: batcher,
    };

    fn digest_to_notif(batch: DigestBatch) -> Result<TestNotification, Report> {
        Ok(TestNotification {
            message: format!("You have {} notification(s)", batch.notifications.len()),
        })
    }

    let result = service.poll_email_digests(&digest_to_notif).await.unwrap();

    // Should still return Ready (batch was consumed) but nothing published
    assert!(matches!(result, ClaimResult::Ready(())));
    assert!(
        queue.get_published().is_empty(),
        "No message should be published when user has disabled the notification type"
    );
}

#[tokio::test]
async fn test_poll_email_digests_publishes_when_user_has_not_disabled_type() {
    use std::sync::Arc;

    let user = test_user_id("[email protected]");
    let other_user = test_user_id("[email protected]");
    let notif = UserNotificationRow {
        owner_id: user.clone(),
        notification_id: Uuid::nil(),
        notification_event_type: "test_notification".to_string(),
        entity: EntityType::Document.with_entity_str("doc-1"),
        sent: false,
        state: crate::domain::models::NotificationState::Unseen,
        created_at: Utc::now(),
        viewed_at: None,
        updated_at: Utc::now(),
        deleted_at: None,
        notification_metadata: serde_json::to_value(TestNotification {
            message: "hello from digest".to_string(),
        })
        .unwrap(),
        sender_id: None,
    };

    let batch = DigestBatch {
        user_id: user.clone(),
        notifications: vec![notif.into_tagged()],
    };

    let batcher = ReadyDigestBatcher {
        batch: Mutex::new(Some(batch)),
    };

    let queue = Arc::new(MockQueue::new());
    // A different user has the type disabled, not the digest recipient
    let service = NotificationEgressService {
        queue: queue.clone(),
        repository: MockRepository::new().with_type_disabled_user(other_user),
        realtime: MockRealtimeSender,
        mobile: MockMobileSender,
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: MockEgressStateMachine,
        digest_batcher: batcher,
    };

    fn digest_to_notif(batch: DigestBatch) -> Result<TestNotification, Report> {
        Ok(TestNotification {
            message: format!("You have {} notification(s)", batch.notifications.len()),
        })
    }

    let result = service.poll_email_digests(&digest_to_notif).await.unwrap();

    assert!(matches!(result, ClaimResult::Ready(())));
    let published = queue.get_published();
    assert_eq!(
        published.len(),
        1,
        "Message should be published when user has not disabled the type"
    );
    assert!(published[0]["content"]["Email"].is_object());
}

#[tokio::test]
async fn connected_email_destination_keeps_the_macro_owner_as_notification_recipient() {
    let queue = std::sync::Arc::new(MockQueue::new());
    let service =
        NotificationIngressService::new(MockRepository::new(), queue.clone(), MockStateMachine);
    let owner = test_user_id("[email protected]");
    let destination = test_user_id("[email protected]");
    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::CalendarEvent.with_entity_str("event"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Work meeting".into(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([owner.clone()]),
    }
    .into_request()
    .with_email_destination(&owner, destination.clone())
    .unwrap();
    let result = service.send_notification(request).await.unwrap();
    assert!(result.unwrap().notified_recipients.contains(&owner));
    let published = queue.get_published();
    assert_eq!(published.len(), 1);
    assert_eq!(published[0]["content"]["Email"]["to"], destination.as_ref());
}

#[test]
fn email_destination_cannot_redirect_multiple_or_unrelated_recipients() {
    let owner = test_user_id("[email protected]");
    let other = test_user_id("[email protected]");
    for recipients in [
        HashSet::from([owner.clone(), other.clone()]),
        HashSet::from([other]),
    ] {
        let request = SendNotificationRequestBuilder {
            notification_entity: EntityType::CalendarEvent.with_entity_str("event"),
            secondary_notification_entity: None,
            notification: TestNotification {
                message: "Meeting".into(),
            },
            sender_id: None,
            recipient_ids: recipients,
        }
        .into_request();
        assert!(
            request
                .with_email_destination(&owner, test_user_id("[email protected]"))
                .is_err()
        );
    }
}