Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use super::*;
use chrono::Utc;
use std::sync::{Arc, Mutex};
use uuid::Uuid;
#[derive(Clone, Default)]
struct DeliveryLog {
live: Arc<Mutex<HashSet<String>>>,
notices: Arc<Mutex<Vec<(String, CommentNotificationReason)>>>,
live_failure: bool,
}
impl MessageRealtime for DeliveryLog {
async fn subscribers(&self, _: &MessageParent) -> Result<HashSet<String>, rootcause::Report> {
Ok(["viewer", "revoked"].map(str::to_owned).into())
}
async fn send(
&self,
_: &MessageEvent,
users: HashSet<String>,
) -> Result<(), rootcause::Report> {
if self.live_failure {
return Err(rootcause::report!("gateway unavailable"));
}
*self.live.lock().unwrap() = users;
Ok(())
}
}
impl DiscussionNotifier for DeliveryLog {
async fn send(
&self,
notification: DiscussionNotification<'_>,
) -> Result<(), rootcause::Report> {
self.notices
.lock()
.unwrap()
.push((notification.recipient, notification.reason));
Ok(())
}
}
struct Access;
impl MessageAudienceAccess for Access {
async fn viewers(
&self,
_: &MessageParent,
mut users: HashSet<String>,
) -> Result<HashSet<String>, rootcause::Report> {
users.remove("revoked");
Ok(users)
}
}
struct Context;
impl DiscussionContextReader for Context {
async fn context(
&self,
_: &MessageParent,
_: Uuid,
) -> Result<DiscussionContext, rootcause::Report> {
Ok(DiscussionContext {
name: "Document".into(),
owner: Some("owner".into()),
file_type: None,
is_task: false,
participants: vec!["revoked".into(), "viewer".into()],
assignees: vec![],
sender_profile_picture: None,
link_share_access: Some(entity_access::domain::models::AccessLevel::View),
})
}
}
fn event() -> MessageEvent {
let actor = "macro|[email protected]".to_owned();
let parent = MessageParent::parse("document", "legacy-document").unwrap();
let message = Message {
kind: MessageKind::Message,
id: Uuid::from_u128(3),
parent: parent.clone(),
thread_id: Some(Uuid::from_u128(2)),
sender_id: actor.clone().try_into().unwrap(),
bot_profile: None,
mentions: vec![],
guest_name: None,
imported_author: None,
triggered_via: None,
triggered_by: None,
content: "A private discussion".into(),
created_at: Utc::now(),
updated_at: Utc::now(),
edited_at: None,
deleted_at: None,
attachments: vec![],
reactions: vec![],
};
MessageEvent {
parent,
actor,
nonce: None,
change: MessageChange::Posted {
notification_policy: Default::default(),
message,
mentions: vec![
SimpleMention {
entity_type: "user".into(),
entity_id: "viewer".into(),
},
SimpleMention {
entity_type: "user".into(),
entity_id: "revoked".into(),
},
],
},
}
}
#[tokio::test]
async fn revoked_viewers_receive_neither_content_nor_notifications() {
let log = DeliveryLog::default();
DiscussionDelivery::new(Context, Access, log.clone(), log.clone())
.publish(event())
.await
.unwrap();
assert_eq!(
*log.live.lock().unwrap(),
HashSet::from(["viewer".to_owned()])
);
assert_eq!(
*log.notices.lock().unwrap(),
vec![
("owner".into(), CommentNotificationReason::Owner),
("viewer".into(), CommentNotificationReason::Mention),
]
);
}
#[tokio::test]
async fn realtime_outage_does_not_skip_notifications() {
let log = DeliveryLog {
live_failure: true,
..Default::default()
};
assert!(
DiscussionDelivery::new(Context, Access, log.clone(), log.clone())
.publish(event())
.await
.is_err()
);
assert_eq!(log.notices.lock().unwrap().len(), 2);
}
#[tokio::test]
async fn edits_and_reactions_only_publish_live_updates() {
let log = DeliveryLog::default();
let mut event = event();
let MessageChange::Posted { message, .. } = event.change else {
unreachable!()
};
event.change = MessageChange::ReactionChanged {
message,
emoji: "👍".to_string(),
added: true,
};
DiscussionDelivery::new(Context, Access, log.clone(), log.clone())
.publish(event)
.await
.unwrap();
assert!(!log.live.lock().unwrap().is_empty());
assert!(log.notices.lock().unwrap().is_empty());
}
#[derive(Clone, Default)]
struct Shares(Arc<Mutex<Vec<Uuid>>>);
impl DiscussionMentionSharing for Shares {
async fn grant(
&self,
document: Uuid,
_: Vec<String>,
_: entity_access::domain::models::AccessLevel,
) -> Result<(), rootcause::Report> {
self.0.lock().unwrap().push(document);
Ok(())
}
}
#[tokio::test]
async fn document_mentions_inherit_link_sharing_for_uuid_parents() {
let log = DeliveryLog::default();
let shares = Shares::default();
let delivery =
DiscussionDelivery::new(Context, Access, log.clone(), log).with_sharing(shares.clone());
delivery.publish(event()).await.unwrap();
assert!(shares.0.lock().unwrap().is_empty());
let mut document_event = event();
let document = Uuid::from_u128(22);
document_event.parent = MessageParent::parse("document", &document.to_string()).unwrap();
delivery.publish(document_event).await.unwrap();
assert_eq!(*shares.0.lock().unwrap(), vec![document]);
}
#[tokio::test]
async fn initiative_mentions_recheck_access_without_granting_document_shares() {
let log = DeliveryLog::default();
let shares = Shares::default();
let delivery = DiscussionDelivery::new(Context, Access, log.clone(), log.clone())
.with_sharing(shares.clone());
let mut event = event();
event.parent = MessageParent::Initiative(Uuid::from_u128(23));
delivery.publish(event).await.unwrap();
assert!(shares.0.lock().unwrap().is_empty());
assert_eq!(
*log.live.lock().unwrap(),
HashSet::from(["viewer".to_owned()])
);
assert!(
!log.notices
.lock()
.unwrap()
.iter()
.any(|(user, _)| user == "revoked")
);
}
#[derive(Clone, Default)]
struct Sink(Arc<Mutex<Vec<MessageParent>>>);
impl MessageEventPublisher for Sink {
async fn publish(&self, event: MessageEvent) -> Result<(), rootcause::Report> {
self.0.lock().unwrap().push(event.parent);
Ok(())
}
}
#[tokio::test]
async fn document_events_never_reach_channel_delivery_and_vice_versa() {
let channels = Sink::default();
let discussions = Sink::default();
let publisher = ParentMessagePublisher::new(channels.clone(), discussions.clone());
let document = event();
let mut channel = event();
channel.parent = MessageParent::Channel(Uuid::from_u128(4));
publisher.publish(document.clone()).await.unwrap();
publisher.publish(channel.clone()).await.unwrap();
let mut initiative = event();
initiative.parent = MessageParent::Initiative(Uuid::from_u128(5));
publisher.publish(initiative.clone()).await.unwrap();
assert_eq!(*channels.0.lock().unwrap(), vec![channel.parent]);
assert_eq!(
*discussions.0.lock().unwrap(),
vec![document.parent, initiative.parent]
);
}
#[tokio::test]
async fn channel_events_are_rejected_by_discussion_delivery() {
let log = DeliveryLog::default();
let mut channel = event();
channel.parent = MessageParent::Channel(Uuid::from_u128(4));
assert!(
DiscussionDelivery::new(Context, Access, log.clone(), log.clone())
.publish(channel)
.await
.is_err()
);
assert!(log.live.lock().unwrap().is_empty());
assert!(log.notices.lock().unwrap().is_empty());
}
struct OwnerlessContext;
impl DiscussionContextReader for OwnerlessContext {
async fn context(
&self,
parent: &MessageParent,
root: Uuid,
) -> Result<DiscussionContext, rootcause::Report> {
let mut context = Context.context(parent, root).await?;
context.owner = None;
context.link_share_access = None;
Ok(context)
}
}
#[tokio::test]
async fn crm_records_without_an_owner_only_notify_mentions_and_participants() {
let log = DeliveryLog::default();
let shares = Shares::default();
let delivery = DiscussionDelivery::new(OwnerlessContext, Access, log.clone(), log.clone())
.with_sharing(shares.clone());
let mut event = event();
event.parent = MessageParent::CrmContact(Uuid::from_u128(24));
delivery.publish(event).await.unwrap();
assert!(shares.0.lock().unwrap().is_empty());
assert_eq!(
*log.notices.lock().unwrap(),
vec![("viewer".into(), CommentNotificationReason::Mention)]
);
}
#[tokio::test]
async fn call_chat_only_reaches_authorized_live_subscribers() {
let log = DeliveryLog::default();
let mut event = event();
event.parent = MessageParent::Call(Uuid::from_u128(9));
if let MessageChange::Posted { message, .. } = &mut event.change {
message.parent = event.parent.clone();
}
DiscussionDelivery::new(Context, Access, log.clone(), log.clone())
.publish(event)
.await
.unwrap();
assert_eq!(
*log.live.lock().unwrap(),
HashSet::from(["viewer".to_owned()])
);
assert!(log.notices.lock().unwrap().is_empty());
}