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 macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::PgPool;
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("team_share"))
)]
async fn exact_direct_levels_and_attributable_deletion(pool: PgPool) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
acquire_guard(&mut tx).await?;
let id = Uuid::from_u128(0x20000000000000000000000000000002);
let team = Uuid::from_u128(0x10000000000000000000000000000001);
upsert_direct(
tx.as_mut(),
&id,
EntityType::Document,
team,
AccessLevel::View,
)
.await?;
assert_eq!(
direct_level(tx.as_mut(), &id, EntityType::Document, team).await?,
Some(AccessLevel::View)
);
delete_direct(tx.as_mut(), &id, EntityType::Document, team).await?;
assert_eq!(
direct_level(tx.as_mut(), &id, EntityType::Document, team).await?,
None
);
assert_eq!(
sqlx::query_scalar!("SELECT count(*) FROM entity_access")
.fetch_one(tx.as_mut())
.await?,
Some(2)
);
tx.rollback().await?;
assert_eq!(
sqlx::query_scalar!("SELECT count(*) FROM entity_access")
.fetch_one(&pool)
.await?,
Some(3)
);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn guard_uses_documented_key_and_transaction_lifetime(
pool: PgPool,
) -> Result<(), sqlx::Error> {
let mut first = pool.begin().await?;
acquire_guard(&mut first).await?;
acquire_guard(&mut first).await?;
let mut second = pool.begin().await?;
assert_eq!(
sqlx::query_scalar!("SELECT pg_try_advisory_xact_lock(1413824845, 1)")
.fetch_one(second.as_mut())
.await?,
Some(false)
);
first.rollback().await?;
assert_eq!(
sqlx::query_scalar!("SELECT pg_try_advisory_xact_lock(1413824845, 1)")
.fetch_one(second.as_mut())
.await?,
Some(true)
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("team_share"))
)]
async fn same_team_refresh_drops_inherited_rows_outside_the_tree(
pool: PgPool,
) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
acquire_guard(&mut tx).await?;
let project_id = Uuid::from_u128(0x20000000_0000_0000_0000_000000000001);
let team_id = Uuid::from_u128(0x10000000_0000_0000_0000_000000000001);
let stale_id = Uuid::from_u128(0x20000000_0000_0000_0000_000000000099);
sqlx::query!(
r#"INSERT INTO entity_access
(entity_id, entity_type, source_id, source_type, access_level, granted_from_project_id)
VALUES ($1, 'email_thread', $2, 'team', 'edit', $3)"#,
stale_id,
team_id.to_string(),
project_id.to_string(),
)
.execute(tx.as_mut())
.await?;
replace_project_contributions(
&mut tx,
&project_id,
Some(team_id),
Some((team_id, AccessLevel::View)),
)
.await?;
let inherited = sqlx::query!(
r#"SELECT entity_id, entity_type, access_level AS "access_level: AccessLevel"
FROM entity_access
WHERE granted_from_project_id = $1 AND source_type = 'team' AND source_id = $2
ORDER BY entity_type, entity_id"#,
project_id.to_string(),
team_id.to_string(),
)
.fetch_all(tx.as_mut())
.await?;
assert!(
inherited
.iter()
.all(|row| row.entity_id != stale_id && row.access_level == AccessLevel::View)
);
assert_eq!(
inherited
.iter()
.map(|row| (row.entity_id, row.entity_type.as_str()))
.collect::<Vec<_>>(),
vec![
(
Uuid::from_u128(0x20000000_0000_0000_0000_000000000004),
"chat"
),
(
Uuid::from_u128(0x20000000_0000_0000_0000_000000000002),
"document"
),
(
Uuid::from_u128(0x20000000_0000_0000_0000_000000000005),
"email_thread"
),
(
Uuid::from_u128(0x20000000_0000_0000_0000_000000000003),
"project"
),
]
);
Ok(())
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn guard_rejects_stale_snapshot_isolation(pool: PgPool) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
sqlx::query!("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ")
.execute(tx.as_mut())
.await?;
assert!(matches!(
acquire_guard(&mut tx).await,
Err(sqlx::Error::InvalidArgument(_))
));
Ok(())
}