//! Integration tests for contact repository operations. use goingson_core::{NewContact, NewContactEmail, NewContactPhone, UpdateContact}; use crate::test_utils::setup_test_state; #[tokio::test] async fn create_and_list_contact() { let (state, user_id) = setup_test_state().await; let new_contact = NewContact { display_name: "Jane Smith".to_string(), nickname: Some("Janie".to_string()), company: Some("Acme Corp".to_string()), title: Some("Engineer".to_string()), notes: "Met at conference".to_string(), tags: vec!["work".to_string()], birthday: None, timezone: None, is_implicit: false, }; let contact = state.contacts.create(user_id, new_contact).await.unwrap(); assert_eq!(contact.display_name, "Jane Smith"); assert_eq!(contact.nickname, Some("Janie".to_string())); assert_eq!(contact.company, Some("Acme Corp".to_string())); let contacts = state.contacts.list_all(user_id).await.unwrap(); assert_eq!(contacts.len(), 1); assert_eq!(contacts[0].display_name, "Jane Smith"); } #[tokio::test] async fn add_email_and_phone() { let (state, user_id) = setup_test_state().await; let new_contact = NewContact { display_name: "John Doe".to_string(), nickname: None, company: None, title: None, notes: String::new(), tags: vec![], birthday: None, timezone: None, is_implicit: false, }; let contact = state.contacts.create(user_id, new_contact).await.unwrap(); // Add email let email = state .contacts .add_email( contact.id, user_id, NewContactEmail { address: "john@example.com".to_string(), label: "Work".to_string(), is_primary: true, }, ) .await .unwrap(); assert_eq!(email.address, "john@example.com"); assert!(email.is_primary); // Add phone let phone = state .contacts .add_phone( contact.id, user_id, NewContactPhone { number: "+1-555-0100".to_string(), label: "Mobile".to_string(), is_primary: true, }, ) .await .unwrap(); assert_eq!(phone.number, "+1-555-0100"); // Verify on get let fetched = state .contacts .get_by_id(contact.id, user_id) .await .unwrap() .unwrap(); assert_eq!(fetched.emails.len(), 1); assert_eq!(fetched.emails[0].address, "john@example.com"); assert_eq!(fetched.phones.len(), 1); assert_eq!(fetched.phones[0].number, "+1-555-0100"); } #[tokio::test] async fn update_contact() { let (state, user_id) = setup_test_state().await; let new_contact = NewContact { display_name: "Original Name".to_string(), nickname: None, company: None, title: None, notes: String::new(), tags: vec![], birthday: None, timezone: None, is_implicit: false, }; let contact = state.contacts.create(user_id, new_contact).await.unwrap(); let update = UpdateContact { display_name: "Updated Name".to_string(), nickname: Some("Nick".to_string()), company: Some("New Corp".to_string()), title: Some("Manager".to_string()), notes: "Updated notes".to_string(), tags: vec!["updated".to_string()], birthday: None, timezone: None, }; let updated = state .contacts .update(contact.id, user_id, update) .await .unwrap() .unwrap(); assert_eq!(updated.display_name, "Updated Name"); assert_eq!(updated.company, Some("New Corp".to_string())); assert_eq!(updated.title, Some("Manager".to_string())); } #[tokio::test] async fn delete_contact() { let (state, user_id) = setup_test_state().await; let new_contact = NewContact { display_name: "To Delete".to_string(), nickname: None, company: None, title: None, notes: String::new(), tags: vec![], birthday: None, timezone: None, is_implicit: false, }; let contact = state.contacts.create(user_id, new_contact).await.unwrap(); let deleted = state.contacts.delete(contact.id, user_id).await.unwrap(); assert!(deleted); let fetched = state .contacts .get_by_id(contact.id, user_id) .await .unwrap(); assert!(fetched.is_none()); } #[tokio::test] async fn find_contact_by_email() { let (state, user_id) = setup_test_state().await; let new_contact = NewContact { display_name: "Findable Person".to_string(), nickname: None, company: None, title: None, notes: String::new(), tags: vec![], birthday: None, timezone: None, is_implicit: false, }; let contact = state.contacts.create(user_id, new_contact).await.unwrap(); // Add an email address state .contacts .add_email( contact.id, user_id, NewContactEmail { address: "findable@example.com".to_string(), label: "Work".to_string(), is_primary: true, }, ) .await .unwrap(); // Find by email let found = state .contacts .find_by_email(user_id, "findable@example.com") .await .unwrap(); assert!(found.is_some()); assert_eq!(found.unwrap().display_name, "Findable Person"); // Not found with different email let not_found = state .contacts .find_by_email(user_id, "nonexistent@example.com") .await .unwrap(); assert!(not_found.is_none()); }