#[cfg(test)]
mod file_search_integration_tests {
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
#[test]
fn test_file_search_config_builder() {
use vtcode_core::tools::file_search_bridge::FileSearchConfig;
let config = FileSearchConfig::new("test".to_string(), PathBuf::from("."))
.exclude("target/**")
.exclude("node_modules/**")
.with_limit(100)
.with_threads(4)
.respect_gitignore(true);
assert_eq!(config.pattern, "test");
assert_eq!(config.max_results, 100);
assert_eq!(config.num_threads, 4);
assert_eq!(config.exclude_patterns.len(), 2);
assert!(config.respect_gitignore);
}
#[test]
fn test_file_search_config_defaults() {
use vtcode_core::tools::file_search_bridge::FileSearchConfig;
let config = FileSearchConfig::new("search_pattern".to_string(), PathBuf::from("src"));
assert_eq!(config.pattern, "search_pattern");
assert_eq!(config.search_dir, PathBuf::from("src"));
assert_eq!(config.max_results, 100);
assert_eq!(config.exclude_patterns.len(), 0);
assert!(config.respect_gitignore);
assert!(!config.compute_indices);
}
#[test]
fn test_file_search_in_current_directory() {
use std::panic;
use vtcode_core::tools::file_search_bridge;
let config = file_search_bridge::FileSearchConfig::new(
"Cargo".to_string(),
std::env::current_dir().expect("Failed to get current dir"),
)
.with_limit(10);
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
file_search_bridge::search_files(config, None)
}));
match result {
Ok(Ok(results)) => {
eprintln!(
"Search completed successfully with {} matches",
results.total_match_count
);
}
Ok(Err(e)) => {
eprintln!("File search error (non-fatal for test): {}", e);
}
Err(_) => {
eprintln!("File search panicked (non-fatal for test)");
}
}
}
#[test]
fn test_file_search_cancellation() {
use vtcode_core::tools::file_search_bridge;
let cancel_flag = Arc::new(AtomicBool::new(false));
let cancel_flag_clone = cancel_flag.clone();
let config = file_search_bridge::FileSearchConfig::new(
"test".to_string(),
std::env::current_dir().expect("Failed to get current dir"),
);
cancel_flag_clone.store(true, std::sync::atomic::Ordering::Relaxed);
match file_search_bridge::search_files(config, Some(cancel_flag)) {
Ok(results) => {
eprintln!(
"Search completed with {} matches despite cancellation",
results.total_match_count
);
}
Err(e) => {
eprintln!("Cancellation triggered search error (expected): {}", e);
}
}
}
#[test]
fn test_filter_by_extension() {
let test_paths = ["src/main.rs", "config.toml", "src/data.json"];
let rust_and_toml: Vec<_> = test_paths
.iter()
.filter(|p| p.ends_with(".rs") || p.ends_with(".toml"))
.collect();
assert_eq!(rust_and_toml.len(), 2);
}
#[test]
fn test_filter_by_pattern() {
let test_paths = ["src/utils.rs", "tests/utils_test.rs", "src/main.rs"];
let src_files: Vec<_> = test_paths
.iter()
.filter(|p| p.starts_with("src/"))
.collect();
assert_eq!(src_files.len(), 2);
}
#[test]
fn test_grep_search_manager_new() {
use vtcode_core::tools::grep_file::GrepSearchManager;
let _manager = GrepSearchManager::new(PathBuf::from("."));
}
#[test]
fn test_grep_search_manager_enumerate_files() {
use std::panic;
use vtcode_core::tools::grep_file::GrepSearchManager;
let manager = GrepSearchManager::new(PathBuf::from("."));
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
manager.enumerate_files_with_pattern("Cargo".to_string(), 10, None)
}));
match result {
Ok(Ok(files)) => {
eprintln!("Found {} files matching 'Cargo'", files.len());
}
Ok(Err(e)) => {
eprintln!("File enumeration error (non-fatal): {}", e);
}
Err(_) => {
eprintln!("File enumeration panicked (non-fatal)");
}
}
}
#[test]
fn test_grep_search_manager_list_all_files() {
use vtcode_core::tools::grep_file::GrepSearchManager;
let manager = GrepSearchManager::new(PathBuf::from("."));
match manager.list_all_files(
20,
vec!["target/**".to_string(), "node_modules/**".to_string()],
) {
Ok(files) => {
eprintln!("Listed {} files", files.len());
}
Err(e) => {
eprintln!("File listing error (may be expected): {}", e);
}
}
}
}