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
//! Tests for chat module
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use models_search_cursor::{SearchCursorOption, SearchMethodCursor};
use sqlx::{Pool, Postgres};
use uuid::Uuid;
use super::*;
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_empty_term(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
let result = search_chat_names(
&pool,
&user_id,
&[],
"".to_string(),
false,
&[],
false,
10,
None,
)
.await;
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
NameSearchError::EmptySearchTerm
));
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_ids_only_with_empty_ids(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
let result = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
true,
&[],
false,
10,
None,
)
.await;
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
NameSearchError::EmptyIdsWithIdsOnly
));
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_ids_only_mode(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search for "project" within specific chat IDs
let chat_ids = vec![
Uuid::parse_str("11111111-1111-1111-1111-111111111111")?,
Uuid::parse_str("22222222-2222-2222-2222-222222222222")?,
Uuid::parse_str("55555555-5555-5555-5555-555555555555")?, // Budget Discussion - won't match
];
let response = search_chat_names(
&pool,
&user_id,
&chat_ids,
"project".to_string(),
true,
&[],
false,
10,
None,
)
.await?;
// Should only return the 2 chats that match "project" from the provided IDs
assert_eq!(response.items.len(), 2);
// Verify results contain expected chats (ordered by updatedAt DESC)
assert_eq!(
response.items[0].entity_id.to_string(),
"22222222-2222-2222-2222-222222222222"
);
assert_eq!(response.items[0].entity_type, SearchEntityType::Chats);
assert_eq!(
response.items[0].name,
"<macro_em>Project</macro_em> Review Chat"
);
assert_eq!(
response.items[1].entity_id.to_string(),
"11111111-1111-1111-1111-111111111111"
);
assert_eq!(
response.items[1].name,
"<macro_em>Project</macro_em> Planning Chat"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_normal_mode_owned_chats(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search for "project" across all user1's owned chats
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
// Should return 3 chats matching "project" (2 lowercase + 1 uppercase)
assert_eq!(response.items.len(), 3);
// Verify ordering by updatedAt DESC
assert_eq!(
response.items[0].entity_id.to_string(),
"66666666-6666-6666-6666-666666666666"
);
assert_eq!(
response.items[0].name,
"IMPORTANT <macro_em>PROJECT</macro_em>"
);
assert_eq!(
response.items[1].entity_id.to_string(),
"22222222-2222-2222-2222-222222222222"
);
assert_eq!(
response.items[1].name,
"<macro_em>Project</macro_em> Review Chat"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_case_insensitive(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search with uppercase term should match both lowercase and uppercase names
let response = search_chat_names(
&pool,
&user_id,
&[],
"PROJECT".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 3);
// Search with lowercase term should also match both
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 3);
// Search with mixed case
let response = search_chat_names(
&pool,
&user_id,
&[],
"PrOjEcT".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 3);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_with_shared_chats(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Include a chat from user3 (shared via chat_ids parameter)
let shared_chat_ids = vec![Uuid::parse_str("99999999-9999-9999-9999-999999999999")?];
let response = search_chat_names(
&pool,
&user_id,
&shared_chat_ids,
"project".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
// Should return user1's 3 "project" chats + user3's 1 "project" chat
assert_eq!(response.items.len(), 4);
// Verify user3's chat is included
let user3_chat = response.items.iter().find(|r| {
r.entity_id
.to_string()
.eq("99999999-9999-9999-9999-999999999999")
});
assert!(user3_chat.is_some());
assert_eq!(
user3_chat.unwrap().name,
"User3 Shared <macro_em>Project</macro_em>"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_pagination_limit(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search with limit of 2
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&[],
false,
2,
None,
)
.await?;
assert_eq!(response.items.len(), 2);
// Should get the 2 most recently updated chats with "project"
assert_eq!(
response.items[0].entity_id.to_string(),
"66666666-6666-6666-6666-666666666666"
);
assert_eq!(
response.items[1].entity_id.to_string(),
"22222222-2222-2222-2222-222222222222"
);
// Should have a next_cursor since there are more results
assert!(response.cursor.has_more());
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_pagination_cursor(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// First page with limit of 2
let first_response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&[],
false,
2,
None,
)
.await?;
assert_eq!(first_response.items.len(), 2);
assert!(first_response.cursor.has_more());
// Extract cursor for second page
let cursor = match first_response.cursor {
SearchCursorOption::NotDone(c) => c,
SearchCursorOption::Done => panic!("Expected more results"),
};
// Second page using cursor
let second_response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&[],
false,
2,
cursor,
)
.await?;
assert_eq!(second_response.items.len(), 1);
// Should get the next chat (skipping the first 2)
assert_eq!(
second_response.items[0].entity_id.to_string(),
"11111111-1111-1111-1111-111111111111"
);
// Should NOT have next_cursor since we've reached the end
assert!(second_response.cursor.is_done());
// Verify no overlap between pages
let first_ids: Vec<String> = first_response
.items
.iter()
.map(|r| r.entity_id.to_string())
.collect();
let second_ids: Vec<String> = second_response
.items
.iter()
.map(|r| r.entity_id.to_string())
.collect();
for id in &first_ids {
assert!(
!second_ids.contains(id),
"Found duplicate ID between pages: {}",
id
);
}
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_no_results(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search for a term that doesn't match any chats
let response = search_chat_names(
&pool,
&user_id,
&[],
"nonexistent".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 0);
assert!(response.cursor.is_done());
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_partial_match(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search for partial term "meet" should match "meeting"
let response = search_chat_names(
&pool,
&user_id,
&[],
"meet".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 2);
assert_eq!(
response.items[0].entity_id.to_string(),
"44444444-4444-4444-4444-444444444444"
);
assert_eq!(
response.items[0].name,
"Client <macro_em>Meet</macro_em>ing"
);
assert_eq!(
response.items[1].entity_id.to_string(),
"33333333-3333-3333-3333-333333333333"
);
assert_eq!(
response.items[1].name,
"Team <macro_em>Meet</macro_em>ing Chat"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_user_isolation(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search for "User2" - user1 should not see user2's private chats
let response = search_chat_names(
&pool,
&user_id,
&[],
"User2".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
// Should return 0 results (user2's chats are not owned by user1 and not shared)
assert_eq!(response.items.len(), 0);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_excludes_soft_deleted(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Search for "project" - should NOT include soft-deleted chat
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&[],
false,
10,
None,
)
.await?;
// Should NOT include the deleted chat (id: 77777777-7777-7777-7777-777777777777)
assert!(!response.items.iter().any(|r| {
r.entity_id
.to_string()
.eq("77777777-7777-7777-7777-777777777777")
}));
// Should include non-deleted chats
assert!(response.items.iter().any(|r| {
r.entity_id
.to_string()
.eq("11111111-1111-1111-1111-111111111111")
}));
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat"))
)]
async fn test_search_chat_names_rejects_thread_cursor(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
let cursor = SearchMethodCursor::Thread {
thread_id: Uuid::new_v4(),
message_id: Uuid::new_v4(),
};
let result = search_chat_names(
&pool,
&user_id,
&[],
"test".to_string(),
false,
&[],
false,
10,
Some(cursor),
)
.await;
assert!(matches!(
result.unwrap_err(),
NameSearchError::IncompatibleCursor
));
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat", "chat_tags"))
)]
async fn test_search_chat_names_tag_filter(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Only the tagged chat matches when a held option id is passed.
let tagged = vec!["cccccccc-0000-0000-0000-000000000001".to_string()];
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&tagged,
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 1);
assert_eq!(
response.items[0].entity_id.to_string(),
"22222222-2222-2222-2222-222222222222"
);
// An option id no chat holds filters everything out.
let unheld = vec!["cccccccc-0000-0000-0000-000000000099".to_string()];
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&unheld,
false,
10,
None,
)
.await?;
assert!(response.items.is_empty());
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat", "chat_tags"))
)]
async fn test_search_chat_names_tag_filter_ids_only(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
let chat_ids = vec![
Uuid::parse_str("11111111-1111-1111-1111-111111111111")?,
Uuid::parse_str("22222222-2222-2222-2222-222222222222")?,
];
// Any of the passed option ids matches (OR semantics).
let tagged = vec![
"cccccccc-0000-0000-0000-000000000002".to_string(),
"cccccccc-0000-0000-0000-000000000099".to_string(),
];
let response = search_chat_names(
&pool,
&user_id,
&chat_ids,
"project".to_string(),
true,
&tagged,
false,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 1);
assert_eq!(
response.items[0].entity_id.to_string(),
"22222222-2222-2222-2222-222222222222"
);
Ok(())
}
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../fixtures", scripts("chat", "chat_tags"))
)]
async fn test_search_chat_names_tag_filter_match_all(pool: Pool<Postgres>) -> anyhow::Result<()> {
let user_id = MacroUserId::parse_from_str("macro|[email protected]")
.map(|l| l.lowercase())
.unwrap();
// Every option id must be held. The two ids live on different property
// rows (definitions), so the check must span rows.
let across_rows = vec![
"cccccccc-0000-0000-0000-000000000001".to_string(),
"cccccccc-0000-0000-0000-000000000003".to_string(),
];
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&across_rows,
true,
10,
None,
)
.await?;
assert_eq!(response.items.len(), 1);
assert_eq!(
response.items[0].entity_id.to_string(),
"22222222-2222-2222-2222-222222222222"
);
// One held plus one unheld id matches nothing under match-all, while the
// same pair would match under any.
let with_unheld = vec![
"cccccccc-0000-0000-0000-000000000001".to_string(),
"cccccccc-0000-0000-0000-000000000099".to_string(),
];
let response = search_chat_names(
&pool,
&user_id,
&[],
"project".to_string(),
false,
&with_unheld,
true,
10,
None,
)
.await?;
assert!(response.items.is_empty());
Ok(())
}