use serde_json::json;
use vtcode_config::OptimizationConfig;
use vtcode_config::constants::tools;
use vtcode_core::tools::ToolRegistry;
#[tokio::test]
async fn test_execute_tool_ref_uses_optimizations() {
let temp_dir = tempfile::tempdir().unwrap();
let workspace_root = temp_dir.path().to_path_buf();
let test_file = workspace_root.join("test.txt");
std::fs::write(&test_file, "Hello, World!").unwrap();
let mut registry = ToolRegistry::new(workspace_root.clone()).await;
let mut config = OptimizationConfig::default();
config.tool_registry.use_optimized_registry = true;
config.tool_registry.hot_cache_size = 8;
config.memory_pool.enabled = true;
registry.configure_optimizations(config);
assert!(registry.has_optimizations_enabled());
let (initial_cache_size, cache_capacity) = registry.hot_cache_stats();
assert_eq!(cache_capacity, 8);
assert_eq!(initial_cache_size, 0);
let args = json!({
"path": test_file.to_string_lossy()
});
let result1 = registry.execute_tool_ref("read_file", &args).await;
assert!(result1.is_ok());
let (cache_size_after_first, _) = registry.hot_cache_stats();
let result2 = registry.execute_tool_ref("read_file", &args).await;
assert!(result2.is_ok());
let result3 = registry.execute_tool_ref("read_file", &args).await;
assert!(result3.is_ok());
assert_eq!(result1.unwrap(), result2.unwrap());
println!("v execute_tool_ref optimization test passed");
println!(" Cache size after first call: {}", cache_size_after_first);
println!(" Cache capacity: {}", cache_capacity);
}
#[tokio::test]
async fn test_execute_tool_ref_memory_pool_integration() {
let temp_dir = tempfile::tempdir().unwrap();
let workspace_root = temp_dir.path().to_path_buf();
let mut registry = ToolRegistry::new(workspace_root.clone()).await;
let mut config = OptimizationConfig::default();
config.memory_pool.enabled = true;
config.memory_pool.max_string_pool_size = 32;
registry.configure_optimizations(config);
let _memory_pool = registry.memory_pool();
let args = json!({
"path": "."
});
let result = registry.execute_tool_ref(tools::LIST_FILES, &args).await;
assert!(result.is_ok());
println!("v execute_tool_ref memory pool integration test passed");
}
#[tokio::test]
async fn test_execute_tool_ref_without_optimizations() {
let temp_dir = tempfile::tempdir().unwrap();
let workspace_root = temp_dir.path().to_path_buf();
let mut registry = ToolRegistry::new(workspace_root.clone()).await;
let mut config = OptimizationConfig::default();
config.tool_registry.use_optimized_registry = false;
config.memory_pool.enabled = false; registry.configure_optimizations(config);
assert!(!registry.has_optimizations_enabled());
let (cache_size, cache_capacity) = registry.hot_cache_stats();
assert_eq!(cache_size, 0);
assert_eq!(cache_capacity, 16);
let args = json!({
"path": "."
});
let result = registry.execute_tool_ref(tools::LIST_FILES, &args).await;
assert!(result.is_ok());
println!("v execute_tool_ref without optimizations test passed");
}
#[tokio::test]
async fn test_execute_tool_ref_hot_cache_effectiveness() {
let temp_dir = tempfile::tempdir().unwrap();
let workspace_root = temp_dir.path().to_path_buf();
let mut registry = ToolRegistry::new(workspace_root.clone()).await;
let mut config = OptimizationConfig::default();
config.tool_registry.use_optimized_registry = true;
config.tool_registry.hot_cache_size = 2; registry.configure_optimizations(config);
let args = json!({"path": "."});
let _result1 = registry.execute_tool_ref(tools::LIST_FILES, &args).await;
let (cache_size_1, _) = registry.hot_cache_stats();
let _result2 = registry.execute_tool_ref(tools::LIST_FILES, &args).await; let (cache_size_2, _) = registry.hot_cache_stats();
assert!(cache_size_1 <= 2);
assert!(cache_size_2 <= 2);
println!("v execute_tool_ref hot cache effectiveness test passed");
println!(" Cache size after first tool: {}", cache_size_1);
println!(" Cache size after second tool: {}", cache_size_2);
}
#[tokio::test]
async fn test_execute_tool_ref_performance_comparison() {
let temp_dir = tempfile::tempdir().unwrap();
let workspace_root = temp_dir.path().to_path_buf();
let registry_unoptimized = ToolRegistry::new(workspace_root.clone()).await;
let mut registry_optimized = ToolRegistry::new(workspace_root.clone()).await;
let config = OptimizationConfig::production(); registry_optimized.configure_optimizations(config);
let args = json!({"path": "."});
let _ = registry_unoptimized
.execute_tool_ref(tools::LIST_FILES, &args)
.await;
let _ = registry_optimized
.execute_tool_ref(tools::LIST_FILES, &args)
.await;
let start = std::time::Instant::now();
for _ in 0..5 {
let _ = registry_unoptimized
.execute_tool_ref(tools::LIST_FILES, &args)
.await;
}
let unoptimized_duration = start.elapsed();
let start = std::time::Instant::now();
for _ in 0..5 {
let _ = registry_optimized
.execute_tool_ref(tools::LIST_FILES, &args)
.await;
}
let optimized_duration = start.elapsed();
println!("v execute_tool_ref performance comparison test completed");
println!(" Unoptimized: {:?}", unoptimized_duration);
println!(" Optimized: {:?}", optimized_duration);
println!(
" Optimized registry has {} optimizations enabled",
registry_optimized.has_optimizations_enabled()
);
assert!(unoptimized_duration.as_millis() > 0);
assert!(optimized_duration.as_millis() > 0);
}