//! Integration tests for annotation methods on SqliteTaskRepository. mod common; use goingson_core::TaskRepository; use goingson_db_sqlite::SqliteTaskRepository; #[tokio::test] async fn test_add_annotation_and_get() { let pool = common::setup_test_db().await; let user_id = common::create_test_user(&pool).await; let task_id = common::create_test_task(&pool, user_id).await; let repo = SqliteTaskRepository::new(pool); let annotation = repo .add_annotation(task_id, user_id, "First note") .await .expect("Failed to add annotation"); assert!(annotation.is_some(), "Should return Some for valid task/user"); let annotation = annotation.unwrap(); assert_eq!(annotation.task_id, task_id); assert_eq!(annotation.note, "First note"); let annotations = repo .get_annotations_for_task(task_id) .await .expect("Failed to get annotations"); assert_eq!(annotations.len(), 1); assert_eq!(annotations[0].id, annotation.id); assert_eq!(annotations[0].note, "First note"); } #[tokio::test] async fn test_annotations_ordered_by_timestamp_desc() { let pool = common::setup_test_db().await; let user_id = common::create_test_user(&pool).await; let task_id = common::create_test_task(&pool, user_id).await; let repo = SqliteTaskRepository::new(pool); repo.add_annotation(task_id, user_id, "Older note") .await .expect("Failed to add annotation") .unwrap(); // Sleep to ensure a different second-precision timestamp tokio::time::sleep(std::time::Duration::from_secs(1)).await; let second = repo .add_annotation(task_id, user_id, "Newer note") .await .expect("Failed to add annotation") .unwrap(); let annotations = repo .get_annotations_for_task(task_id) .await .expect("Failed to get annotations"); assert_eq!(annotations.len(), 2); // Newest first (ORDER BY timestamp DESC) assert_eq!(annotations[0].id, second.id); assert_eq!(annotations[0].note, "Newer note"); assert_eq!(annotations[1].note, "Older note"); assert!(annotations[0].timestamp >= annotations[1].timestamp); } #[tokio::test] async fn test_add_annotation_wrong_user_returns_none() { let pool = common::setup_test_db().await; let user1 = common::create_test_user(&pool).await; let user2 = common::create_test_user(&pool).await; let task_id = common::create_test_task(&pool, user1).await; let repo = SqliteTaskRepository::new(pool); let result = repo .add_annotation(task_id, user2, "Unauthorized note") .await .expect("Should not error, just return None"); assert!(result.is_none(), "Should return None when user doesn't own the task"); } #[tokio::test] async fn test_delete_annotation() { let pool = common::setup_test_db().await; let user_id = common::create_test_user(&pool).await; let task_id = common::create_test_task(&pool, user_id).await; let repo = SqliteTaskRepository::new(pool); let annotation = repo .add_annotation(task_id, user_id, "To be deleted") .await .expect("Failed to add annotation") .unwrap(); let deleted = repo .delete_annotation(annotation.id, user_id) .await .expect("Failed to delete annotation"); assert!(deleted, "Should return true for successful deletion"); let annotations = repo .get_annotations_for_task(task_id) .await .expect("Failed to get annotations"); assert!(annotations.is_empty(), "Annotation should be gone after deletion"); } #[tokio::test] async fn test_delete_annotation_wrong_user_returns_false() { let pool = common::setup_test_db().await; let user1 = common::create_test_user(&pool).await; let user2 = common::create_test_user(&pool).await; let task_id = common::create_test_task(&pool, user1).await; let repo = SqliteTaskRepository::new(pool); let annotation = repo .add_annotation(task_id, user1, "User1's note") .await .expect("Failed to add annotation") .unwrap(); let deleted = repo .delete_annotation(annotation.id, user2) .await .expect("Should not error, just return false"); assert!(!deleted, "Should return false when user doesn't own the annotation"); // Verify the annotation still exists let annotations = repo .get_annotations_for_task(task_id) .await .expect("Failed to get annotations"); assert_eq!(annotations.len(), 1, "Annotation should still exist after unauthorized delete"); } #[tokio::test] async fn test_get_annotations_empty_task() { let pool = common::setup_test_db().await; let user_id = common::create_test_user(&pool).await; let task_id = common::create_test_task(&pool, user_id).await; let repo = SqliteTaskRepository::new(pool); let annotations = repo .get_annotations_for_task(task_id) .await .expect("Failed to get annotations"); assert!(annotations.is_empty(), "New task should have no annotations"); }