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::helpers::*;
use crate::domain::companies_repo::CompaniesRepository;
use crate::domain::model::CrmError;
use crate::outbound::companies_repo::CompaniesRepositoryImpl;
use chrono::Utc;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::PgPool;
use uuid::Uuid;
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_inserts_contact(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
let now = Utc::now();
let contact = repo
.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
now,
false,
)
.await?;
// Email is stored lowercased; the contact starts visible and both
// interaction endpoints seed from `now`.
assert_eq!(contact.email.as_deref(), Some("[email protected]"));
assert_eq!(contact.name.as_deref(), Some("Jane"));
assert_eq!(contact.company_id, Some(company_id));
assert!(!contact.hidden);
assert_eq!(
contact.first_interaction.timestamp_micros(),
now.timestamp_micros()
);
assert_eq!(
contact.last_interaction.timestamp_micros(),
now.timestamp_micros()
);
let listed = repo
.list_contacts_for_company(&team_id, &company_id, false)
.await?;
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].id, contact.id);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_rejects_duplicate_email(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
repo.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
// Case-insensitive: normalization lowercases before the insert.
let err = repo
.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane 2",
Utc::now(),
false,
)
.await
.unwrap_err();
assert!(matches!(err, CrmError::ContactAlreadyExistsForCompany));
assert_eq!(count_contacts(&pool, company_id).await?, 1);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_accepts_explicit_cross_domain_links(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
for email in ["[email protected]", "[email protected]", "[email protected]"] {
let contact = repo
.create_contact_for_company(
&team_id,
&company_id,
Some(email),
"Jane",
Utc::now(),
false,
)
.await?;
assert_eq!(contact.email.as_deref(), Some(email));
assert_eq!(contact.company_id, Some(company_id));
}
assert_eq!(count_contacts(&pool, company_id).await?, 3);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_accepts_any_company_domain(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
// Multi-domain company: any of its domains is a valid email domain,
// matched case-insensitively.
let company_id = insert_company(&pool, team_id, true, &["example.com", "Acme.io"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
repo.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
repo.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
assert_eq!(count_contacts(&pool, company_id).await?, 2);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_accepts_personal_email_at_any_company(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let solo_id = insert_company(&pool, team_id, true, &[]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
for company in [company_id, solo_id] {
let contact = repo
.create_contact_for_company(
&team_id,
&company,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
assert_eq!(contact.email.as_deref(), Some("[email protected]"));
}
let err = repo
.create_contact_for_company(
&team_id,
&solo_id,
Some("[email protected]"),
"Jane again",
Utc::now(),
false,
)
.await
.unwrap_err();
assert!(matches!(err, CrmError::ContactAlreadyExistsForCompany));
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_without_email(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
let company_id = insert_company(&pool, team_id, true, &[]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
// Email-less contacts never conflict with each other.
for name in ["Jane", "John"] {
let contact = repo
.create_contact_for_company(&team_id, &company_id, None, name, Utc::now(), false)
.await?;
assert_eq!(contact.email, None);
assert_eq!(contact.name.as_deref(), Some(name));
}
assert_eq!(count_contacts(&pool, company_id).await?, 2);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_cross_team_company_not_found(pool: PgPool) -> anyhow::Result<()> {
let team_a = Uuid::now_v7();
let team_b = Uuid::now_v7();
seed_team(&pool, team_a, "macro|[email protected]").await?;
seed_team(&pool, team_b, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_a).await?;
enable_crm_for_team(&pool, team_b).await?;
let company_id = insert_company(&pool, team_a, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
let err = repo
.create_contact_for_company(
&team_b,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
true,
)
.await
.unwrap_err();
assert!(matches!(err, CrmError::CompanyNotFoundForTeam));
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_hidden_company_semantics(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team_id).await?;
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
repo.set_company_hidden(&team_id, &company_id, true).await?;
// Members can't reach a hidden company.
let err = repo
.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await
.unwrap_err();
assert!(matches!(err, CrmError::CompanyNotFoundForTeam));
// Admin/owner can, and the new contact inherits `hidden`.
let contact = repo
.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
true,
)
.await?;
assert!(contact.hidden);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn create_contact_requires_crm_enabled(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
seed_team(&pool, team_id, "macro|[email protected]").await?;
// Company exists but no team_crm_settings row: killswitch off.
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
let err = repo
.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await
.unwrap_err();
assert!(matches!(err, CrmError::CrmDisabledForTeam));
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn populate_merges_into_manual_contact(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
let owner = "macro|[email protected]";
seed_team(&pool, team_id, owner).await?;
enable_crm_for_team(&pool, team_id).await?;
let link_id = insert_email_link(&pool, owner, "[email protected]").await?;
let company_id = insert_company(&pool, team_id, true, &["example.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
let contact = repo
.create_contact_for_company(
&team_id,
&company_id,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
// A populate for the same email upserts onto the manual row: the
// manual name wins (first non-NULL), no second contact appears.
let now = Utc::now();
repo.populate_contact(
&team_id,
&link_id,
"example.com",
"[email protected]",
Some("Jane Doe (observed)"),
now,
now,
true,
)
.await?;
assert_eq!(count_contacts(&pool, company_id).await?, 1);
let listed = repo
.list_contacts_for_company(&team_id, &company_id, false)
.await?;
assert_eq!(listed[0].id, contact.id);
assert_eq!(listed[0].name.as_deref(), Some("Jane"));
// The populate attached a source row; tearing that link down again
// must keep the manual contact (manually_created shields it from
// the no-sources orphan cleanup).
let outcome = repo
.depopulate_contact(&team_id, &link_id, "example.com", "[email protected]")
.await?;
// Only the source row goes; the manual contact and its company stay.
assert!(outcome.source_deleted);
assert!(!outcome.contact_deleted);
assert!(!outcome.company_deleted);
assert_eq!(count_contacts(&pool, company_id).await?, 1);
assert_eq!(count_sources_for_company(&pool, company_id).await?, 0);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn manual_rows_survive_link_teardown(pool: PgPool) -> anyhow::Result<()> {
let team_id = Uuid::now_v7();
let owner = "macro|[email protected]";
seed_team(&pool, team_id, owner).await?;
enable_crm_for_team(&pool, team_id).await?;
let link_id = insert_email_link(&pool, owner, "[email protected]").await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
// A fully manual company (no contacts) and a derived company with a
// populated contact + a manual contact side by side.
let manual_company = repo
.create_company_for_team(&team_id, Some("manual.com"), "Manual Co", Utc::now())
.await?;
let derived_company = insert_company(&pool, team_id, true, &["example.com"]).await?;
insert_contact_with_source(&pool, derived_company, "[email protected]", link_id).await?;
let manual_contact = repo
.create_contact_for_company(
&team_id,
&derived_company,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
repo.depopulate_link_in_team(&team_id, &link_id).await?;
// The populated contact (last source dropped) is gone; the manual
// contact keeps its company alive; the empty manual company also
// survives.
let listed = repo
.list_contacts_for_company(&team_id, &derived_company, false)
.await?;
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].id, manual_contact.id);
assert_eq!(
count_companies_for_domain(&pool, team_id, "manual.com").await?,
1
);
let _ = manual_company;
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn shared_email_preserves_every_company_opt_out(pool: PgPool) -> anyhow::Result<()> {
let team = Uuid::now_v7();
seed_team(&pool, team, "macro|[email protected]").await?;
enable_crm_for_team(&pool, team).await?;
let enabled = insert_company(&pool, team, true, &["example.com"]).await?;
let disabled = insert_company(&pool, team, false, &["other.com"]).await?;
let repo = CompaniesRepositoryImpl::new(pool.clone());
for company in [enabled, disabled] {
repo.create_contact_for_company(
&team,
&company,
Some("[email protected]"),
"Jane",
Utc::now(),
false,
)
.await?;
}
let inputs = vec![
"[email protected]".to_string(),
"[email protected]".to_string(),
"[email protected]".to_string(),
];
let before = repo.crm_scope_precheck(&team, &[], &inputs).await?;
assert_eq!(before.addresses.len(), inputs.len());
assert_eq!(
before
.addresses
.iter()
.map(|row| &row.address)
.collect::<Vec<_>>(),
inputs.iter().collect::<Vec<_>>()
);
assert!(before.addresses[0].exists);
assert!(!before.addresses[0].email_sync);
assert!(!before.addresses[1].exists);
assert!(!before.addresses[2].email_sync);
repo.set_company_hidden(&team, &disabled, true).await?;
let after = repo.crm_scope_precheck(&team, &[], &inputs).await?;
assert_eq!(after.addresses.len(), inputs.len());
assert!(after.addresses[0].contact_hidden);
assert!(after.addresses[0].company_hidden);
assert!(!after.addresses[0].email_sync);
Ok(())
}