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 std::sync::Mutex;
use macro_event_broker::{EventBrokerError, MacroEvent, MacroEventBroker};
use serde_json::{Value, json};
use super::*;
#[derive(Clone, Debug)]
struct PublishedEvent {
topic: String,
key: String,
envelope: Value,
}
#[derive(Default)]
struct RecordingEventBroker {
published: Mutex<Vec<PublishedEvent>>,
}
impl MacroEventBroker for RecordingEventBroker {
fn send_event<E: MacroEvent + ?Sized>(
&self,
event: &E,
) -> Result<tokio::task::JoinHandle<Result<(), EventBrokerError>>, EventBrokerError> {
self.published
.lock()
.expect("published events lock should not be poisoned")
.push(PublishedEvent {
topic: event.topic().to_string(),
key: event.key().to_string(),
envelope: serde_json::to_value(event.event())?,
});
Ok(tokio::spawn(async { Ok(()) }))
}
}
fn owner() -> MacroUserIdStr<'static> {
MacroUserIdStr::try_from("macro|[email protected]".to_string()).expect("valid owner")
}
fn thread_ids(count: usize) -> Vec<Uuid> {
(1..=count)
.map(|value| Uuid::from_u128(value as u128))
.collect()
}
fn assert_published_batches(thread_count: usize, expected_batch_sizes: &[usize]) {
let broker = RecordingEventBroker::default();
let link_id = Uuid::from_u128(u128::MAX);
let owner = owner();
let thread_ids = thread_ids(thread_count);
publish_thread_reindex_batches(&broker, link_id, &owner, &thread_ids);
let published = broker
.published
.lock()
.expect("published events lock should not be poisoned");
let actual_batch_sizes: Vec<usize> = published
.iter()
.map(|event| {
event.envelope["metadata"]["thread_ids"]
.as_array()
.expect("thread_ids should be an array")
.len()
})
.collect();
assert_eq!(actual_batch_sizes, expected_batch_sizes);
let published_thread_ids: Vec<Uuid> = published
.iter()
.flat_map(|event| {
assert_eq!(event.topic, "macro.email");
assert_eq!(event.key, link_id.to_string());
assert_eq!(event.envelope["schema_version"], json!(1));
assert_eq!(
event.envelope["event_type"],
json!("email.threads_reindex_requested")
);
assert_eq!(
event.envelope["metadata"]["link_id"],
json!(link_id.to_string())
);
assert_eq!(
event.envelope["metadata"]["owner"],
json!(owner.to_string())
);
assert_eq!(
event.envelope["metadata"]["reason"],
json!("contacts_changed")
);
let batch = event.envelope["metadata"]["thread_ids"]
.as_array()
.expect("thread_ids should be an array");
assert!(!batch.is_empty());
assert!(batch.len() <= REINDEX_BATCH_SIZE);
batch.iter().map(|thread_id| {
Uuid::parse_str(thread_id.as_str().expect("thread id should be a string"))
.expect("thread id should be a UUID")
})
})
.collect();
assert_eq!(published_thread_ids, thread_ids);
}
fn other_contact(email: Option<&str>, name: Option<&str>) -> Contact {
Contact {
id: Uuid::from_u128(1),
link_id: Uuid::from_u128(2),
name: name.map(str::to_owned),
email_address: email.map(str::to_owned),
original_photo_url: None,
sfs_photo_url: None,
}
}
#[test]
fn other_contact_at_a_shared_sender_address_keeps_no_name() {
let contact = normalize_other_contact(other_contact(
Some("[email protected]"),
Some("Nathan Flurry"),
));
assert_eq!(contact.name, None);
assert_eq!(
contact.email_address.as_deref(),
Some("[email protected]")
);
}
#[test]
fn other_contact_at_a_personal_address_keeps_its_name() {
let contact = normalize_other_contact(other_contact(
Some("[email protected]"),
Some("Nathan Flurry"),
));
assert_eq!(contact.name.as_deref(), Some("Nathan Flurry"));
}
#[test]
fn other_contact_drops_the_relay_suffix_from_its_name() {
let contact = normalize_other_contact(other_contact(
Some("[email protected]"),
Some("Nathan Flurry via Cal.com"),
));
assert_eq!(contact.name.as_deref(), Some("Nathan Flurry"));
}
#[test]
fn other_contact_without_an_address_is_left_alone() {
let contact = normalize_other_contact(other_contact(None, Some("Nathan Flurry")));
assert_eq!(contact.name.as_deref(), Some("Nathan Flurry"));
}
#[tokio::test]
async fn publishes_no_batches_for_zero_threads() {
assert_published_batches(0, &[]);
}
#[tokio::test]
async fn publishes_one_batch_for_one_thread() {
assert_published_batches(1, &[1]);
}
#[tokio::test]
async fn publishes_one_full_batch_for_fifty_threads() {
assert_published_batches(50, &[50]);
}
#[tokio::test]
async fn publishes_a_second_batch_for_fifty_one_threads() {
assert_published_batches(51, &[50, 1]);
}
#[tokio::test]
async fn publishes_ordered_bounded_batches_for_larger_inputs() {
assert_published_batches(137, &[50, 50, 37]);
}
fn link_for(macro_id: &str, email: &str) -> Link {
let macro_id = MacroUserIdStr::try_from(macro_id.to_string()).expect("valid macro id");
let email_address = macro_user_id::email::EmailStr::try_from(email.to_string()).expect("email");
Link {
id: Uuid::nil(),
is_primary: Link::derive_is_primary(¯o_id, &email_address),
macro_id,
fusionauth_user_id: "fa".to_string(),
email_address,
provider: models_email::service::link::UserProvider::Gmail,
is_sync_active: true,
needs_reauth: false,
last_sync_error_at: None,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
}
}
#[test]
fn address_book_connections_cover_saved_contacts_and_skip_noise() {
let link = link_for("macro|[email protected]", "[email protected]");
let contacts = vec![
other_contact(Some("[email protected]"), Some("Alice")),
other_contact(Some("[email protected]"), Some("Alice again")),
other_contact(Some("[email protected]"), Some("Me")),
other_contact(Some("[email protected]"), None),
other_contact(Some(" "), None),
other_contact(None, Some("No address")),
other_contact(Some("[email protected]"), Some("Friend")),
];
let connections = address_book_connections(&link, &contacts);
let second: Vec<String> = connections
.iter()
.map(|connection| connection.second.to_string())
.collect();
assert_eq!(
second,
vec!["macro|[email protected]", "macro|[email protected]"]
);
assert!(
connections
.iter()
.all(|connection| connection.first == link.macro_id)
);
}