refactor: Implement unified search abstraction, MemoryState refactoring, error handling, and unit test expansion

This commit is contained in:
Riz Ashraf committed 2026-10-01 08:37:52 +01:00
1 parent a34554b7ff
commit 462f65f66d
21 files changed
+425 -535

No files matched your search

+51
View File
@@ -196,3 +196,54 @@ impl McpTool for ReplaceAstNodeHandler {
Ok(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use tempfile::tempdir;
#[tokio::test]
async fn test_read_file_skeleton() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let file_path = dir.path().join("test_skeleton.rs");
let code = "fn my_func() {\n let x = 1;\n}\n\nstruct MyStruct {\n val: i32\n}";
std::fs::write(&file_path, code).unwrap();
let handler = ReadFileSkeletonHandler;
let args = json!({
"file_path": file_path.to_str().unwrap()
});
let res = handler.execute(args, state.clone()).await.unwrap();
assert!(res.contains("fn my_func()"));
assert!(res.contains("struct MyStruct"));
}
#[tokio::test]
async fn test_replace_ast_node() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let file_path = dir.path().join("test_replace.rs");
let code = "fn my_func() {\n let x = 1;\n}\n\nstruct MyStruct {\n val: i32\n}";
std::fs::write(&file_path, code).unwrap();
let handler = ReplaceAstNodeHandler;
let args = json!({
"file_path": file_path.to_str().unwrap(),
"node_type": "function_item",
"node_name": "my_func",
"new_content": "fn my_func() {\n let x = 2;\n}"
});
let res = handler.execute(args, state.clone()).await.unwrap();
assert!(res.contains("Successfully replaced node"));
let new_code = std::fs::read_to_string(&file_path).unwrap();
assert!(new_code.contains("let x = 2;"));
assert!(!new_code.contains("let x = 1;"));
}
}