//! Integration tests for queue_index //! //! Tests the priority queue with real filesystem operations. use tempfile::TempDir; use queue_index::{QueueIndex, Task}; fn create_test_task(id: &str, _priority: i64) -> Task { Task::new(id, "test-job") } #[test] fn test_queue_index_creation() { let temp = TempDir::new().unwrap(); let index = QueueIndex::open(temp.path()); assert!(index.is_ok()); } #[test] fn test_add_and_retrieve_task() { let temp = TempDir::new().unwrap(); let mut index = QueueIndex::open(temp.path()).unwrap(); let task = create_test_task("task-1", 100); let added = index.add_tasks(&[task]).unwrap(); assert_eq!(added, 1); let batch = index.get_next_batch(10).unwrap(); assert_eq!(batch.len(), 1); assert_eq!(batch[0].id, "task-1"); } #[test] fn test_priority_ordering() { let temp = TempDir::new().unwrap(); let mut index = QueueIndex::open(temp.path()).unwrap(); let mut low_priority = create_test_task("low", 10); low_priority.priority = 10; let mut high_priority = create_test_task("high", 100); high_priority.priority = 100; index.add_tasks(&[low_priority, high_priority]).unwrap(); let batch = index.get_next_batch(10).unwrap(); assert_eq!(batch.len(), 2); // Higher priority should come first assert_eq!(batch[0].id, "high"); assert_eq!(batch[1].id, "low"); } #[test] fn test_batch_operations() { let temp = TempDir::new().unwrap(); let mut index = QueueIndex::open(temp.path()).unwrap(); let tasks: Vec = (0..100) .map(|i| Task::new(&format!("task-{}", i), "batch-job")) .collect(); let added = index.add_tasks(&tasks).unwrap(); assert_eq!(added, 100); // Get in batches of 10 let mut total_retrieved = 0; for _ in 0..10 { let batch = index.get_next_batch(10).unwrap(); total_retrieved += batch.len(); } assert_eq!(total_retrieved, 100); } #[test] fn test_task_count() { let temp = TempDir::new().unwrap(); let mut index = QueueIndex::open(temp.path()).unwrap(); let count_before = index.get_task_count("queued"); assert_eq!(count_before, 0); let task = create_test_task("task-1", 50); index.add_tasks(&[task]).unwrap(); let count_after = index.get_task_count("queued"); assert_eq!(count_after, 1); } #[test] #[ignore = "Persistence not fully implemented - load_tasks is a stub"] fn test_persistence() { let temp = TempDir::new().unwrap(); let path = temp.path().to_path_buf(); // Create index and add task { let mut index = QueueIndex::open(&path).unwrap(); let task = create_test_task("persistent-task", 50); index.add_tasks(&[task]).unwrap(); // Index dropped here, should persist } // Reopen and verify { let mut index = QueueIndex::open(&path).unwrap(); let batch = index.get_next_batch(10).unwrap(); assert_eq!(batch.len(), 1); assert_eq!(batch[0].id, "persistent-task"); } } #[test] fn test_empty_batch() { let temp = TempDir::new().unwrap(); let mut index = QueueIndex::open(temp.path()).unwrap(); let batch = index.get_next_batch(10).unwrap(); assert!(batch.is_empty()); } #[test] fn test_task_status() { let temp = TempDir::new().unwrap(); let mut index = QueueIndex::open(temp.path()).unwrap(); let mut task = create_test_task("status-test", 50); task.status = "queued".to_string(); index.add_tasks(&[task]).unwrap(); let batch = index.get_next_batch(10).unwrap(); assert_eq!(batch[0].status, "queued"); }