use std::fs; use pinakes_core::integrity::detect_orphans; mod common; use common::{create_test_media_item, setup_test_storage}; #[tokio::test] async fn test_detect_orphaned_files() { let (storage, temp_dir) = setup_test_storage().await; let nonexistent_path = temp_dir.path().join("nonexistent.mp3"); let orphaned_item = create_test_media_item(nonexistent_path, "hash1"); storage.insert_media(&orphaned_item).await.unwrap(); let report = detect_orphans(&storage).await.unwrap(); assert_eq!(report.orphaned_ids.len(), 1); assert_eq!(report.orphaned_ids[0], orphaned_item.id); } #[tokio::test] async fn test_detect_untracked_files() { let (storage, temp_dir) = setup_test_storage().await; let root_dir = temp_dir.path().join("media"); fs::create_dir(&root_dir).unwrap(); storage.add_root_dir(root_dir.clone()).await.unwrap(); let tracked_file = root_dir.join("tracked.mp3"); let untracked_file = root_dir.join("untracked.mp3"); fs::write(&tracked_file, b"tracked content").unwrap(); fs::write(&untracked_file, b"untracked content").unwrap(); let tracked_item = create_test_media_item(tracked_file.clone(), "hash_tracked"); storage.insert_media(&tracked_item).await.unwrap(); let report = detect_orphans(&storage).await.unwrap(); assert_eq!(report.untracked_paths.len(), 1); assert!(report.untracked_paths.contains(&untracked_file)); } #[tokio::test] async fn test_detect_moved_files() { let (storage, temp_dir) = setup_test_storage().await; let old_path = temp_dir.path().join("old_location.mp3"); fs::write(&old_path, b"content").unwrap(); let old_item = create_test_media_item(old_path.clone(), "hash_unique"); storage.insert_media(&old_item).await.unwrap(); fs::remove_file(&old_path).unwrap(); let report = detect_orphans(&storage).await.unwrap(); assert_eq!(report.orphaned_ids.len(), 1); assert_eq!(report.moved_files.len(), 0); } #[tokio::test] async fn test_ignore_patterns_respected() { let (storage, temp_dir) = setup_test_storage().await; let root_dir = temp_dir.path().join("media"); fs::create_dir(&root_dir).unwrap(); storage.add_root_dir(root_dir.clone()).await.unwrap(); let hidden_dir = root_dir.join(".hidden"); fs::create_dir(&hidden_dir).unwrap(); let hidden_file = hidden_dir.join("hidden.mp3"); fs::write(&hidden_file, b"hidden content").unwrap(); let normal_file = root_dir.join("normal.mp3"); fs::write(&normal_file, b"normal content").unwrap(); let report = detect_orphans(&storage).await.unwrap(); assert_eq!(report.untracked_paths.len(), 1); assert!(report.untracked_paths.contains(&normal_file)); assert!(!report.untracked_paths.contains(&hidden_file)); } #[tokio::test] async fn test_only_supported_media_types() { let (storage, temp_dir) = setup_test_storage().await; let root_dir = temp_dir.path().join("media"); fs::create_dir(&root_dir).unwrap(); storage.add_root_dir(root_dir.clone()).await.unwrap(); let mp3_file = root_dir.join("audio.mp3"); let txt_file = root_dir.join("readme.txt"); let exe_file = root_dir.join("program.exe"); fs::write(&mp3_file, b"audio").unwrap(); fs::write(&txt_file, b"text").unwrap(); fs::write(&exe_file, b"binary").unwrap(); let report = detect_orphans(&storage).await.unwrap(); assert!(report.untracked_paths.len() <= 2); assert!(!report.untracked_paths.contains(&exe_file)); } #[tokio::test] async fn test_complete_orphan_workflow() { let (storage, temp_dir) = setup_test_storage().await; let root_dir = temp_dir.path().join("media"); fs::create_dir(&root_dir).unwrap(); storage.add_root_dir(root_dir.clone()).await.unwrap(); let orphaned_path = root_dir.join("orphaned.mp3"); let orphaned_item = create_test_media_item(orphaned_path.clone(), "hash_orphaned"); storage.insert_media(&orphaned_item).await.unwrap(); let untracked_path = root_dir.join("untracked.mp3"); fs::write(&untracked_path, b"untracked").unwrap(); let another_orphaned = root_dir.join("another_orphaned.mp3"); let another_item = create_test_media_item(another_orphaned.clone(), "hash_another"); storage.insert_media(&another_item).await.unwrap(); let tracked_path = root_dir.join("tracked.mp3"); fs::write(&tracked_path, b"tracked").unwrap(); let tracked_item = create_test_media_item(tracked_path.clone(), "hash_tracked"); storage.insert_media(&tracked_item).await.unwrap(); let report = detect_orphans(&storage).await.unwrap(); assert_eq!(report.orphaned_ids.len(), 2); assert!(report.orphaned_ids.contains(&orphaned_item.id)); assert!(report.orphaned_ids.contains(&another_item.id)); assert_eq!(report.untracked_paths.len(), 1); assert!(report.untracked_paths.contains(&untracked_path)); assert_eq!(report.moved_files.len(), 0); } #[tokio::test] async fn test_large_directory_performance() { let (storage, temp_dir) = setup_test_storage().await; let root_dir = temp_dir.path().join("media"); fs::create_dir(&root_dir).unwrap(); storage.add_root_dir(root_dir.clone()).await.unwrap(); for i in 0..1000 { let file_path = root_dir.join(format!("file_{}.mp3", i)); fs::write(&file_path, format!("content {}", i)).unwrap(); } for i in 0..500 { let file_path = root_dir.join(format!("file_{}.mp3", i)); let item = create_test_media_item(file_path, &format!("hash_{}", i)); storage.insert_media(&item).await.unwrap(); } let start = std::time::Instant::now(); let report = detect_orphans(&storage).await.unwrap(); let elapsed = start.elapsed(); assert!( elapsed.as_secs() < 5, "Detection took too long: {:?}", elapsed ); assert_eq!(report.untracked_paths.len(), 500); }