#[cfg(test)]
mod tests {
use crate::tools::glob::ContentCategory;
use crate::tools::GlobTool;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;
fn create_test_workspace() -> TempDir {
let temp_dir = TempDir::new().unwrap();
let root = temp_dir.path();
let git_init = std::process::Command::new("git")
.arg("init")
.current_dir(root)
.output()
.is_ok();
fs::create_dir(root.join("src")).unwrap();
fs::write(root.join("src").join("main.rs"), "fn main() {}").unwrap();
fs::write(root.join("src").join("lib.rs"), "pub mod lib {}").unwrap();
fs::create_dir(root.join("tests")).unwrap();
fs::write(
root.join("tests").join("integration_test.rs"),
"#[test] fn test() {}",
)
.unwrap();
fs::write(root.join("src").join("main_test.rs"), "mod tests {}").unwrap();
fs::write(root.join("Cargo.toml"), "[package]\nname = \"test\"").unwrap();
fs::write(root.join("config.json"), "{}").unwrap();
fs::write(root.join("README.md"), "# Test Project").unwrap();
fs::write(root.join(".gitignore"), "target/\n*.tmp\n").unwrap();
if !git_init {
fs::write(root.join(".ignore"), "target/\n*.tmp\n").unwrap();
}
fs::create_dir(root.join("target")).unwrap();
fs::write(root.join("target").join("debug.txt"), "debug info").unwrap();
fs::write(root.join("ignored.tmp"), "temporary").unwrap();
temp_dir
}
#[test]
fn test_basic_glob_functionality() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf());
let result = glob_tool.glob("**/*.rs").unwrap();
assert!(!result.result.is_empty());
assert!(result.summary.contains("Found"));
assert!(result.metadata.operation == "file_discovery");
assert!(result.performance.execution_time.as_millis() >= 0);
}
#[test]
fn test_find_type_method() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf());
let result = glob_tool.find_type("rs").unwrap();
assert!(result.result.len() >= 3); assert!(
result
.result
.iter()
.all(|f| f.extension.as_ref().unwrap() == "rs")
);
}
#[test]
fn test_file_type_classification() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf());
let root_files = glob_tool.glob("*").unwrap();
let sub_files = glob_tool.glob("**/*").unwrap();
let mut all_files = root_files.result;
all_files.extend(sub_files.result);
let has_source = all_files
.iter()
.any(|f| f.content_category == ContentCategory::Source);
let has_config = all_files
.iter()
.any(|f| f.content_category == ContentCategory::Config);
let has_docs = all_files
.iter()
.any(|f| f.content_category == ContentCategory::Documentation);
let has_test = all_files
.iter()
.any(|f| f.content_category == ContentCategory::Test);
assert!(has_source, "Should classify some files as source");
assert!(has_config, "Should classify some files as config");
assert!(has_docs, "Should classify some files as documentation");
assert!(has_test, "Should classify some files as test");
}
#[test]
fn test_gitignore_support() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf()).with_respect_ignore(true);
let result = glob_tool.glob("**/*").unwrap();
assert!(
!result
.result
.iter()
.any(|f| f.relative_path.to_string_lossy().contains("target")),
"Should not find files in target directory"
);
assert!(
!result
.result
.iter()
.any(|f| f.path.file_name().unwrap() == "ignored.tmp"),
"Should not find ignored.tmp file"
);
let tmp_result = glob_tool.glob("*.tmp").unwrap();
assert_eq!(
tmp_result.result.len(),
0,
"Should not find any .tmp files due to .gitignore"
);
}
#[test]
fn test_find_in_directory() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf());
let src_dir = workspace.path().join("src");
let result = glob_tool.find_in_directory(&src_dir, "*.rs").unwrap();
assert!(!result.result.is_empty());
assert!(result.result.iter().all(|f| f.path.starts_with(&src_dir)));
}
#[test]
fn test_parallel_vs_sequential() {
let workspace = create_test_workspace();
let parallel_tool = GlobTool::new(workspace.path().to_path_buf()).with_parallelism(4);
let sequential_tool = GlobTool::new(workspace.path().to_path_buf()).with_parallelism(1);
let parallel_result = parallel_tool.glob("*").unwrap();
let sequential_result = sequential_tool.glob("*").unwrap();
assert_eq!(parallel_result.result.len(), sequential_result.result.len());
assert!(parallel_result.performance.execution_time.as_millis() >= 0);
assert!(sequential_result.performance.execution_time.as_millis() >= 0);
}
#[test]
fn test_max_results_limit() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf()).with_max_results(Some(2));
let result = glob_tool.glob("*").unwrap();
assert!(result.result.len() <= 2);
}
#[test]
fn test_error_handling() {
let non_existent = PathBuf::from("/non/existent/path");
let glob_tool = GlobTool::new(non_existent);
let result = glob_tool.glob("*");
assert!(result.is_err());
}
#[test]
fn test_tool_output_structure() {
let workspace = create_test_workspace();
let glob_tool = GlobTool::new(workspace.path().to_path_buf());
let result = glob_tool.glob("**/*.rs").unwrap();
assert!(!result.result.is_empty());
assert!(result.metadata.tool == "glob");
assert!(!result.summary.is_empty());
assert!(result.performance.memory_usage.peak_bytes > 0);
assert!(result.metadata.completed_at >= result.metadata.started_at);
assert_eq!(result.metadata.operation, "file_discovery");
assert!(result.performance.io_stats.read_ops > 0);
assert!(result.performance.execution_time.as_millis() >= 0);
}
}