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
#[allow(unused_imports)]
use super::*;
use crate::user::get_user_name::get_user_names_with_email;
use macro_user_id::cowlike::CowLike;
use macro_user_id::user_id::MacroUserId;
use non_empty::NonEmpty;
use sqlx::{Pool, Postgres};
fn parse_user_ids(
ids: Vec<&str>,
) -> anyhow::Result<NonEmpty<Vec<MacroUserId<macro_user_id::lowercased::Lowercase<'static>>>>> {
NonEmpty::new(
ids.into_iter()
.map(|id| MacroUserId::parse_from_str(id).map(|u| u.lowercase().into_owned()))
.collect::<Result<Vec<_>, _>>()?,
)
.map_err(|e| anyhow::anyhow!("Empty user_ids: {}", e))
}
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn test_get_user_names_with_email_basic(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_profile_ids = parse_user_ids(vec![
"macro|[email protected]",
"macro|[email protected]",
])?;
let mut names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
names.sort_by(|a, b| a.id.cmp(&b.id));
assert_eq!(names.len(), 2);
// Sorted: macro|[email protected], macro|[email protected]
assert_eq!(names[0].id, "macro|[email protected]");
assert_eq!(names[0].first_name, Some("JohnMacroContact".to_string()));
assert_eq!(names[0].last_name, Some("DoeMacroContact".to_string()));
assert_eq!(names[1].id, "macro|[email protected]");
assert_eq!(names[1].first_name, Some("JaneMacroContact".to_string()));
assert_eq!(names[1].last_name, Some("SmithMacroContact".to_string()));
Ok(())
}
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn test_get_user_names_with_email_fallback_to_contact(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
// User with N/A name should fall back to email contact name
let user_profile_ids = parse_user_ids(vec!["macro|[email protected]"])?;
let names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
assert_eq!(names.len(), 1);
assert_eq!(names[0].id, "macro|[email protected]");
assert_eq!(names[0].first_name, Some("BobEmailContact".to_string()));
assert_eq!(names[0].last_name, Some("JohnsonEmailContact".to_string()));
Ok(())
}
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn test_get_user_names_with_email_contact_only(pool: Pool<Postgres>) -> anyhow::Result<()> {
// User not in User table, only in email_contacts
let user_profile_ids = parse_user_ids(vec!["macro|[email protected]"])?;
let names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
assert_eq!(names.len(), 1);
assert_eq!(names[0].id, "macro|[email protected]");
assert_eq!(names[0].first_name, Some("AliceEmailContact".to_string()));
assert_eq!(names[0].last_name, Some("WilliamsEmailContact".to_string()));
Ok(())
}
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn test_get_user_names_with_email_mixed(pool: Pool<Postgres>) -> anyhow::Result<()> {
// Mix of users with names, N/A fallback, and contact-only
let user_profile_ids = parse_user_ids(vec![
"macro|[email protected]",
"macro|[email protected]",
"macro|[email protected]",
])?;
let mut names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
names.sort_by(|a, b| a.id.cmp(&b.id));
assert_eq!(names.len(), 3);
// Sorted: macro|[email protected], macro|[email protected], macro|[email protected]
assert_eq!(names[0].id, "macro|[email protected]");
assert_eq!(names[0].first_name, Some("AliceEmailContact".to_string()));
assert_eq!(names[0].last_name, Some("WilliamsEmailContact".to_string()));
assert_eq!(names[1].id, "macro|[email protected]");
assert_eq!(names[1].first_name, Some("JohnMacroContact".to_string()));
assert_eq!(names[1].last_name, Some("DoeMacroContact".to_string()));
assert_eq!(names[2].id, "macro|[email protected]");
assert_eq!(names[2].first_name, Some("BobEmailContact".to_string()));
assert_eq!(names[2].last_name, Some("JohnsonEmailContact".to_string()));
Ok(())
}
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn test_get_user_names_with_email_not_found(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_profile_ids = parse_user_ids(vec!["macro|[email protected]"])?;
let names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
// Should return empty list for users that don't exist
assert_eq!(names.len(), 0);
Ok(())
}
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn deduplicates_requested_user_ids(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_profile_ids = parse_user_ids(vec![
"macro|[email protected]",
"macro|[email protected]",
])?;
let names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
assert_eq!(names.len(), 1);
assert_eq!(names[0].id, "macro|[email protected]");
Ok(())
}
/// only fallback to using email contact names if the user has neither first nor last name set
/// in macro. don't use the email contact last name with the macro first name if exists.
#[sqlx::test(fixtures(path = "../../../fixtures", scripts("user_names_with_email")))]
async fn uses_macro_names_if_either_first_or_last_present(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let user_profile_ids = parse_user_ids(vec![
"macro|[email protected]",
"macro|[email protected]",
])?;
let mut names =
get_user_names_with_email(&pool, "macro|[email protected]", user_profile_ids)
.await?;
names.sort_by(|a, b| a.id.cmp(&b.id));
let u4 = names
.iter()
.find(|n| n.id == "macro|[email protected]")
.expect("user_profile_4 should be returned");
assert_eq!(u4.first_name.as_deref(), Some("OnlyFirstMacro"));
assert_eq!(u4.last_name.as_deref(), None);
let u5 = names
.iter()
.find(|n| n.id == "macro|[email protected]")
.expect("user_profile_5 should be returned");
assert_eq!(u5.first_name.as_deref(), None);
assert_eq!(u5.last_name.as_deref(), Some("OnlyLastMacro"));
Ok(())
}
#[sqlx::test]
async fn test_get_user_name_without_info_row(pool: Pool<Postgres>) -> anyhow::Result<()> {
// The macro_user_info row is created lazily by the first name write, so a
// brand-new user has none — the lookup must report "no name yet", not
// error.
let user_id = "00000000-0000-0000-0000-000000000042";
let name = get_user_name(&pool, user_id).await?;
assert_eq!(name.id, user_id);
assert_eq!(name.first_name, None);
assert_eq!(name.last_name, None);
Ok(())
}