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.

14.7 KB
Raw Blame
use super::*;
use crate::domain::models::android::FCMMessage;
use crate::domain::models::email_notification_digest::ports::NotificationSendChecker;
use crate::domain::models::email_notification_digest::{
    BatchSend, BulkDigestEgressStateMachine, DontSend, ResumeMachineBRequest,
};
use crate::domain::models::mobile::MessageAttributes;
use crate::domain::models::queue_message::{DeliverySuccess, UserApnsEndpoints};

#[tokio::test]
async fn queues_android_and_ios_together_after_recipient_filtering() {
    let user = test_user_id("[email protected]");
    let muted = test_user_id("[email protected]");
    let queue = Arc::new(MockQueue::new());
    let repo = Arc::new(
        MockRepository::new()
            .with_device_endpoint(user.clone(), DeviceEndpoint::Ios("ios".into()))
            .with_device_endpoint(user.clone(), DeviceEndpoint::Android("android".into()))
            .with_device_endpoint(user.clone(), DeviceEndpoint::IosVoip("voip".into()))
            .with_device_endpoint(muted.clone(), DeviceEndpoint::Android("muted".into()))
            .with_muted_user(muted.clone()),
    );
    let service = NotificationIngressService::new(repo.clone(), queue.clone(), MockStateMachine);
    service
        .send_notification(
            SendNotificationRequestBuilder {
                notification_entity: EntityType::Document.with_entity_str("doc"),
                secondary_notification_entity: None,
                notification: TestNotification {
                    message: "Hello".into(),
                },
                sender_id: None,
                recipient_ids: HashSet::from([user.clone(), muted]),
            }
            .into_request()
            .with_apns(),
        )
        .await
        .unwrap();
    let published = queue.get_published();
    assert_eq!(published.len(), 1);
    let recipients = published[0]["content"]["Ios"]["ios_device_endpoints"]
        .as_object()
        .unwrap();
    assert_eq!(recipients.len(), 1);
    assert_eq!(recipients[user.as_ref()]["endpoints"], json!(["ios"]));
    assert_eq!(
        recipients[user.as_ref()]["android_endpoints"],
        json!(["android"])
    );
    assert!(repo.stored_collapse_keys.lock().unwrap()[0].1.is_some());
}

#[tokio::test]
async fn android_only_alerts_keep_the_identifier_needed_for_clearing() {
    let user = test_user_id("[email protected]");
    let repo = Arc::new(
        MockRepository::new()
            .with_device_endpoint(user.clone(), DeviceEndpoint::Android("android".into())),
    );
    let queue = Arc::new(MockQueue::new());
    let service = NotificationIngressService::new(repo.clone(), queue.clone(), MockStateMachine);
    service
        .send_notification(
            SendNotificationRequestBuilder {
                notification_entity: EntityType::Document.with_entity_str("doc"),
                secondary_notification_entity: None,
                notification: TestNotification {
                    message: "Hello".into(),
                },
                sender_id: None,
                recipient_ids: HashSet::from([user]),
            }
            .into_request()
            .with_apns(),
        )
        .await
        .unwrap();
    assert_eq!(queue.get_published().len(), 1);
    assert!(repo.stored_collapse_keys.lock().unwrap()[0].1.is_some());
}

#[derive(Default)]
struct CaptureMobile {
    android: Mutex<Vec<(String, serde_json::Value)>>,
    ios: Mutex<Vec<String>>,
}

#[derive(Default)]
struct CaptureFallback {
    groups: Mutex<Vec<usize>>,
    fallback_queued: Mutex<bool>,
}

impl BulkDigestEgressStateMachine for CaptureFallback {
    async fn continue_machine<N: NotificationSendChecker>(
        &self,
        req: ResumeMachineBRequest<N>,
    ) -> (
        Vec<Result<N::Ok, N::Err>>,
        either::Either<DontSend, Result<BatchSend<()>, Report>>,
    ) {
        self.groups.lock().unwrap().push(req.send_notifs.len());
        let result = MockEgressStateMachine.continue_machine(req).await;
        *self.fallback_queued.lock().unwrap() = result.1.is_right();
        result
    }
}

impl NotificationSender for CaptureMobile {
    async fn send_ios_push_notification<T: Serialize + Send + Sync>(
        &self,
        endpoint: &str,
        _: &APNSPushNotification<T>,
        _: &MessageAttributes,
    ) -> Result<String, Report> {
        self.ios.lock().unwrap().push(endpoint.to_owned());
        rootcause::bail!("simulated iOS failure")
    }

    async fn send_android_push_notification<T: Serialize + Send + Sync>(
        &self,
        endpoint: &str,
        notification: &FCMMessage<T>,
        _: &MessageAttributes,
    ) -> Result<String, Report> {
        self.android
            .lock()
            .unwrap()
            .push((endpoint.into(), serde_json::to_value(notification).unwrap()));
        Ok("android-message-id".into())
    }
}

#[tokio::test]
async fn delivers_android_even_when_the_same_users_ios_endpoint_fails() {
    let queue = Arc::new(MockQueue::new());
    let user = test_user_id("[email protected]");
    let ingress = NotificationIngressService::new(
        MockRepository::new()
            .with_device_endpoint(user.clone(), DeviceEndpoint::Ios("ios".into()))
            .with_device_endpoint(user.clone(), DeviceEndpoint::Android("android".into())),
        queue.clone(),
        MockStateMachine,
    );
    ingress
        .send_notification(
            SendNotificationRequestBuilder {
                notification_entity: EntityType::Document.with_entity_str("doc"),
                secondary_notification_entity: None,
                notification: TestNotification {
                    message: "Hello".into(),
                },
                sender_id: None,
                recipient_ids: HashSet::from([user.clone()]),
            }
            .into_request()
            .with_apns(),
        )
        .await
        .unwrap();
    let egress = NotificationEgressService {
        queue: MockQueue::new(),
        repository: MockRepository::new(),
        realtime: MockRealtimeSender,
        mobile: CaptureMobile::default(),
        email: MockEmailSender,
        rate_limiter: allowing_rate_limiter(),
        state_machine: CaptureFallback::default(),
        digest_batcher: MockDigestBatcher,
    };
    let mut queued = queue.get_published().remove(0);
    queued["content"]["Ios"]["ios_device_endpoints"][user.as_ref()]["digest_state"] = json!({
        "inner": updated_notification(user.clone(), Uuid::now_v7(), false, None, Utc::now())
    });
    let message = serde_json::from_value(queued).unwrap();
    let results = egress.deliver_notification(message).await;
    assert_eq!(results.len(), 2);
    assert!(
        results
            .iter()
            .any(|result| matches!(result, Ok(DeliverySuccess::Android)))
    );
    assert!(results.iter().any(Result::is_err));
    let android = egress.mobile.android.lock().unwrap();
    assert_eq!(android.len(), 1);
    assert_eq!(android[0].0, "android");
    assert_eq!(android[0].1["data"]["message"], "Hello");
    assert_eq!(egress.mobile.ios.lock().unwrap().as_slice(), ["ios"]);
    assert_eq!(egress.state_machine.groups.lock().unwrap().as_slice(), [2]);
    assert!(!*egress.state_machine.fallback_queued.lock().unwrap());
}

#[tokio::test]
async fn seen_and_done_deliver_silent_clear_to_android_only_users() {
    for status in [NotificationStatus::Seen, NotificationStatus::Done(true)] {
        let user = test_user_id("[email protected]");
        let id = Uuid::now_v7();
        let queue = Arc::new(MockQueue::new());
        let reader = NotificationReaderService {
            repository: MockRepository::new()
                .with_basic_notification(id, "identifier".into())
                .with_device_endpoint(user.clone(), DeviceEndpoint::Android("android".into())),
            queue: queue.clone(),
            sns_endpoint: MockSnsEndpoint,
            platform_config: test_platform_config(),
            realtime: crate::domain::ports::NoopNotificationRealtimePublisher,
        };
        reader
            .update_notifications(UpdateNotificationsRequest {
                user_id: user,
                notification_ids: &[id],
                status,
            })
            .await
            .unwrap();
        let mut messages = queue.get_published();
        if !cfg!(feature = "clear_ios_push") {
            assert!(messages.is_empty());
            continue;
        }
        assert_eq!(messages.len(), 1);
        let egress = NotificationEgressService {
            queue: MockQueue::new(),
            repository: MockRepository::new(),
            realtime: MockRealtimeSender,
            mobile: CaptureMobile::default(),
            email: MockEmailSender,
            rate_limiter: allowing_rate_limiter(),
            state_machine: MockEgressStateMachine,
            digest_batcher: MockDigestBatcher,
        };
        let results = egress
            .deliver_notification(serde_json::from_value(messages.remove(0)).unwrap())
            .await;
        assert!(matches!(results.as_slice(), [Ok(DeliverySuccess::Android)]));
        let android = egress.mobile.android.lock().unwrap();
        assert_eq!(android[0].1["identifier"], "identifier");
        assert!(android[0].1["title"].is_null());
        assert!(android[0].1["body"].is_null());
    }
}

#[tokio::test]
async fn native_call_exclusions_keep_only_unhandled_mobile_platforms() {
    use crate::domain::models::MobilePushRecipients;
    use crate::domain::models::queue_message::IngressQueueMessage;

    for (ios, android) in [(false, false), (true, false), (false, true), (true, true)] {
        let user = test_user_id("[email protected]");
        let exclusions = MobilePushRecipients {
            ios: if ios {
                HashSet::from([user.clone()])
            } else {
                HashSet::new()
            },
            android: if android {
                HashSet::from([user.clone()])
            } else {
                HashSet::new()
            },
        };
        let repo = Arc::new(
            MockRepository::new()
                .with_device_endpoint(user.clone(), DeviceEndpoint::Ios("ios".into()))
                .with_device_endpoint(
                    user.clone(),
                    DeviceEndpoint::AndroidNativeCall("android".into()),
                ),
        );
        let queue = Arc::new(MockQueue::new());
        let service = NotificationIngressService::new(repo, queue.clone(), MockStateMachine);
        let request = SendNotificationRequestBuilder {
            notification_entity: EntityType::Document.with_entity_str("doc"),
            secondary_notification_entity: None,
            notification: TestNotification {
                message: "Hello".into(),
            },
            sender_id: None,
            recipient_ids: HashSet::from([user.clone()]),
        }
        .into_request()
        .with_mobile_push_exclusions(exclusions.clone())
        .with_apns()
        .with_conn_gateway();
        // Production crosses the ingress queue as a type-erased serialized request.
        let queued = IngressQueueMessage::from_request(&request).unwrap();
        assert_eq!(queued.request.mobile_push_exclusions, exclusions);
        service.process_from_queue(queued.request).await.unwrap();
        let published = queue.get_published();
        assert!(
            published
                .iter()
                .any(|message| message["content"]["ConnGateway"].is_object())
        );
        let mobile = published
            .iter()
            .find(|message| message["content"]["Ios"].is_object());
        if ios && android {
            assert!(mobile.is_none());
        } else {
            let endpoints: UserApnsEndpoints = serde_json::from_value(
                mobile.unwrap()["content"]["Ios"]["ios_device_endpoints"][user.as_ref()].clone(),
            )
            .unwrap();
            assert_eq!(
                endpoints.endpoints,
                if ios {
                    Vec::<String>::new()
                } else {
                    vec!["ios".into()]
                }
            );
            assert_eq!(
                endpoints.android_endpoints,
                if android {
                    Vec::<String>::new()
                } else {
                    vec!["android".into()]
                }
            );
        }
    }
}

#[test]
fn queued_requests_without_mobile_exclusions_keep_existing_delivery() {
    use crate::domain::models::MobilePushRecipients;
    use crate::domain::models::queue_message::IngressQueueMessage;
    let request = SendNotificationRequestBuilder {
        notification_entity: EntityType::Document.with_entity_str("doc"),
        secondary_notification_entity: None,
        notification: TestNotification {
            message: "Hello".into(),
        },
        sender_id: None,
        recipient_ids: HashSet::from([test_user_id("[email protected]")]),
    }
    .into_request()
    .with_apns();
    let mut serialized =
        serde_json::to_value(IngressQueueMessage::from_request(&request).unwrap()).unwrap();
    serialized["request"]
        .as_object_mut()
        .unwrap()
        .remove("mobile_push_exclusions");
    let decoded: IngressQueueMessage = serde_json::from_value(serialized).unwrap();
    assert_eq!(
        decoded.request.mobile_push_exclusions,
        MobilePushRecipients::default()
    );
}

#[tokio::test]
async fn native_android_exclusion_preserves_ordinary_alerts_for_legacy_devices() {
    use crate::domain::models::MobilePushRecipients;
    let user = test_user_id("[email protected]");
    let queue = Arc::new(MockQueue::new());
    let service = NotificationIngressService::new(
        MockRepository::new()
            .with_device_endpoint(
                user.clone(),
                DeviceEndpoint::Android("legacy-android".into()),
            )
            .with_device_endpoint(
                user.clone(),
                DeviceEndpoint::AndroidNativeCall("native-android".into()),
            ),
        queue.clone(),
        MockStateMachine,
    );
    service
        .send_notification(
            SendNotificationRequestBuilder {
                notification_entity: EntityType::Document.with_entity_str("doc"),
                secondary_notification_entity: None,
                notification: TestNotification {
                    message: "Hello".into(),
                },
                sender_id: None,
                recipient_ids: HashSet::from([user.clone()]),
            }
            .into_request()
            .with_apns()
            .with_mobile_push_exclusions(MobilePushRecipients {
                ios: HashSet::new(),
                android: HashSet::from([user.clone()]),
            }),
        )
        .await
        .unwrap();
    let published = queue.get_published();
    assert_eq!(published.len(), 1);
    assert_eq!(
        published[0]["content"]["Ios"]["ios_device_endpoints"][user.as_ref()]["android_endpoints"],
        json!(["legacy-android"])
    );
}