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::*;
// ── previews_for_view_cursor: sent routing ──────────────────────────
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(
path = "../../../../fixtures",
scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
)
)]
async fn test_sent_view_without_filter_routes_to_dynamic_multi_inbox(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
// The sent tab sends no filter tree; the routing must still take the
// dynamic path and cover every linked inbox without a sender filter.
let repo = EmailPgRepo::new(pool);
let query = crate::domain::models::PreviewCursorQuery {
view: PreviewView::StandardLabel(PreviewViewStandardLabel::Sent),
link_ids: vec![
Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
],
limit: 50,
query: Query::new(None, SimpleSortMethod::UpdatedAt, None),
team_id: None,
};
let results = repo
.previews_for_view_cursor(
query,
MacroUserIdStr::parse_from_str("macro|[email protected]")?.to_owned(),
)
.await?;
let ids: Vec<String> = results.iter().map(|r| r.id.to_string()).collect();
assert_eq!(
ids,
vec![
"20000201-0000-0000-0000-000000000201".to_string(),
"20000002-0000-0000-0000-000000000002".to_string(),
],
"Sent view should return each inbox's sent thread, newest first"
);
Ok(())
}
// ── attachments_by_thread_ids ───────────────────────────────────────
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_attachments_by_thread_ids_single_thread(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
let thread1 = Uuid::parse_str("11111111-1111-1111-1111-111111111111")?;
let attachments = repo.attachments_by_thread_ids(&[thread1]).await?;
assert_eq!(
attachments.len(),
2,
"Thread 1 has 2 attachments (both on msg1)"
);
// Ordered by created_at ASC
assert_eq!(attachments[0].filename.as_deref(), Some("document.pdf"));
assert_eq!(
attachments[0].message_id,
Uuid::parse_str("ee000001-0000-0000-0000-000000000001")?
);
assert_eq!(attachments[1].filename.as_deref(), Some("image.png"));
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_attachments_by_thread_ids_multiple_threads(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
let thread1 = Uuid::parse_str("11111111-1111-1111-1111-111111111111")?;
let thread3 = Uuid::parse_str("33333333-3333-3333-3333-333333333333")?;
let attachments = repo.attachments_by_thread_ids(&[thread1, thread3]).await?;
assert_eq!(
attachments.len(),
3,
"Thread 1 has 2 attachments, thread 3 has 1"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_attachments_by_thread_ids_no_attachments(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
// Thread 2 only has msg3 (a draft) which has no provider attachments
let thread2 = Uuid::parse_str("22222222-2222-2222-2222-222222222222")?;
let attachments = repo.attachments_by_thread_ids(&[thread2]).await?;
assert!(
attachments.is_empty(),
"Thread with no provider attachments should return empty"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_attachments_by_thread_ids_empty_input(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
let attachments = repo.attachments_by_thread_ids(&[]).await?;
assert!(attachments.is_empty());
Ok(())
}
// ── contacts_by_thread_ids ──────────────────────────────────────────
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_contacts_by_thread_ids(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
let thread1 = Uuid::parse_str("11111111-1111-1111-1111-111111111111")?;
let contacts = repo.contacts_by_thread_ids(&[thread1]).await?;
// Thread 1 has msg1 (from alice) and msg2 (from bob), ordered by created_at ASC
assert_eq!(contacts.len(), 2, "Thread 1 has 2 sender contacts");
assert_eq!(
contacts[0].email_address.as_deref(),
Some("[email protected]")
);
// msg1 has from_name 'Alice S.' which overrides contact.name 'Alice Smith'
assert_eq!(contacts[0].name.as_deref(), Some("Alice S."));
assert_eq!(
contacts[0].sfs_photo_url.as_deref(),
Some("https://photos.example.com/alice.jpg")
);
assert_eq!(
contacts[1].email_address.as_deref(),
Some("[email protected]")
);
// msg2 has no from_name, so contact.name 'Bob Jones' is used
assert_eq!(contacts[1].name.as_deref(), Some("Bob Jones"));
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_contacts_by_thread_ids_no_from_contact(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
// Thread 2 only has msg3 which has no from_contact_id
let thread2 = Uuid::parse_str("22222222-2222-2222-2222-222222222222")?;
let contacts = repo.contacts_by_thread_ids(&[thread2]).await?;
assert!(
contacts.is_empty(),
"Thread with no sender contacts should return empty"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_contacts_by_thread_ids_multiple_threads(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
let thread1 = Uuid::parse_str("11111111-1111-1111-1111-111111111111")?;
let thread3 = Uuid::parse_str("33333333-3333-3333-3333-333333333333")?;
let contacts = repo.contacts_by_thread_ids(&[thread1, thread3]).await?;
// Thread 1: alice + bob, Thread 3: alice (no from_name override)
assert_eq!(
contacts.len(),
3,
"Should have 3 contacts across both threads"
);
// Thread 3's alice should use contact.name since msg4 has no from_name
let thread3_contact = &contacts[2];
assert_eq!(
thread3_contact.email_address.as_deref(),
Some("[email protected]")
);
assert_eq!(
thread3_contact.name.as_deref(),
Some("Alice Smith"),
"msg4 has no from_name, should use contact.name"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_message"))
)]
async fn test_contacts_by_thread_ids_empty_input(pool: Pool<Postgres>) -> anyhow::Result<()> {
let repo = EmailPgRepo::new(pool);
let contacts = repo.contacts_by_thread_ids(&[]).await?;
assert!(contacts.is_empty());
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_dynamic_query"))
)]
async fn returned_thread_without_message_timestamps_has_created_at(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let thread = Uuid::parse_str("20000003-0000-0000-0000-000000000003")?;
sqlx::query(
"UPDATE email_threads SET inbox_visible = true, reminder_returned_at = NOW() WHERE id = $1",
)
.bind(thread)
.execute(&pool)
.await?;
let rows = crate::outbound::email_pg_repo::preview_views::new_inbox::new_inbox_preview_cursor(
&pool,
&[Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?],
50,
&Query::new(None, SimpleSortMethod::UpdatedAt, ()),
)
.await?;
let returned = rows
.iter()
.find(|row| row.id == thread)
.expect("returned thread must remain readable");
let created: chrono::DateTime<chrono::Utc> =
sqlx::query_scalar("SELECT created_at FROM email_threads WHERE id = $1")
.bind(thread)
.fetch_one(&pool)
.await?;
assert_eq!(returned.created_at, created);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(
path = "../../../../fixtures",
scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
)
)]
async fn inbox_pages_through_tied_threads_across_links(pool: Pool<Postgres>) -> anyhow::Result<()> {
// Threads 4 and 5 (first inbox) and 201 (second inbox) share a timestamp,
// and their updated_at runs opposite to their ids.
sqlx::query(
"UPDATE email_threads
SET inbox_visible = true,
latest_inbound_message_ts = '2024-01-12 07:00:00+00',
updated_at = CASE id
WHEN '20000004-0000-0000-0000-000000000004' THEN '2024-03-03 00:00:00+00'::timestamptz
WHEN '20000005-0000-0000-0000-000000000005' THEN '2024-03-02 00:00:00+00'::timestamptz
ELSE '2024-03-01 00:00:00+00'::timestamptz
END
WHERE id IN (
'20000004-0000-0000-0000-000000000004',
'20000005-0000-0000-0000-000000000005',
'20000201-0000-0000-0000-000000000201'
)",
)
.execute(&pool)
.await?;
let link_ids = [
Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
];
let mut ids = Vec::new();
let mut query = Query::new(None, SimpleSortMethod::UpdatedAt, ());
loop {
let page =
crate::outbound::email_pg_repo::preview_views::new_inbox::new_inbox_preview_cursor(
&pool, &link_ids, 2, &query,
)
.await?;
let Some(last) = page.last() else { break };
query = Query::new(
Some(Cursor {
id: last.id,
limit: 2,
val: CursorVal {
sort_type: SimpleSortMethod::UpdatedAt,
last_val: last.sort_ts,
},
filter: (),
}),
SimpleSortMethod::UpdatedAt,
(),
);
ids.extend(page.iter().map(|row| row.id.to_string()));
}
assert_eq!(
ids,
vec![
"20000001-0000-0000-0000-000000000001",
"20000201-0000-0000-0000-000000000201",
"20000005-0000-0000-0000-000000000005",
"20000004-0000-0000-0000-000000000004",
"20000007-0000-0000-0000-000000000007",
],
"each inbox thread appears once, newest first, ties broken by id"
);
Ok(())
}