use crate::server::MemoryMCPServer;
use anyhow::Result;
use serde_json::json;
use tracing::debug;
impl MemoryMCPServer {
pub async fn execute_test_agentfs_connection(
&self,
input: crate::mcp::tools::external_signals::TestAgentFsConnectionInput,
) -> Result<serde_json::Value> {
self.track_tool_usage("test_agentfs_connection").await;
debug!(
"Testing AgentFS connection for db_path: {:?}",
input.db_path
);
let start_time = std::time::Instant::now();
let db_path = input
.db_path
.clone()
.or_else(|| std::env::var("AGENTFS_DB_PATH").ok());
let Some(db_path) = db_path else {
let result = crate::mcp::tools::external_signals::TestAgentFsConnectionOutput {
success: false,
provider: "agentfs".to_string(),
db_path: String::new(),
connection_time_ms: start_time.elapsed().as_millis() as u64,
readable: false,
writable: false,
toolcall_count: None,
version: Some("0.6.4".to_string()),
message: "AgentFS database path not configured".to_string(),
error: Some(
"Set AGENTFS_DB_PATH environment variable or provide db_path in request"
.to_string(),
),
};
return Ok(json!(result));
};
let (success, message, error, toolcall_count) =
match agentfs_sdk::ToolCalls::new(&db_path).await {
Ok(tc) => {
let count = tc.stats().await.ok().map(|s| s.len());
(
true,
"Successfully connected to AgentFS database".to_string(),
None,
count,
)
}
Err(e) => (
false,
"Failed to connect to AgentFS database".to_string(),
Some(format!("Connection error: {e}")),
None,
),
};
let test_duration_ms = start_time.elapsed().as_millis() as u64;
let result = crate::mcp::tools::external_signals::TestAgentFsConnectionOutput {
success,
provider: "agentfs".to_string(),
db_path,
connection_time_ms: test_duration_ms,
readable: success,
writable: false, toolcall_count,
version: Some("0.6.4".to_string()),
message,
error,
};
Ok(json!(result))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[expect(clippy::manual_async_fn)]
fn test_test_agentfs_connection_signature_compile() {
use crate::mcp::tools::external_signals::TestAgentFsConnectionInput;
fn method_signature(
_server: &MemoryMCPServer,
_input: TestAgentFsConnectionInput,
) -> impl std::future::Future<Output = Result<serde_json::Value>> {
async { Ok(json!({})) }
}
let _ = method_signature; }
#[test]
fn test_stub_result_indicates_sdk_unavailable() {
let result = crate::mcp::tools::external_signals::TestAgentFsConnectionOutput {
success: false,
provider: "agentfs".to_string(),
db_path: "/tmp/test.db".to_string(),
connection_time_ms: 0,
readable: false,
writable: false,
toolcall_count: None,
version: None,
message: "SDK not integrated".to_string(),
error: Some("SDK unavailable".to_string()),
};
assert!(!result.success, "Stub should report unsuccessful test");
assert!(result.error.is_some(), "Should have error message");
assert!(!result.readable, "Should report not readable");
assert!(
result.toolcall_count.is_none(),
"Should have no toolcall count"
);
}
}
#[cfg(test)]
mod functional_tests {
use super::*;
use do_memory_core::SelfLearningMemory;
use std::sync::Arc;
async fn create_test_server() -> MemoryMCPServer {
let memory = Arc::new(SelfLearningMemory::new());
MemoryMCPServer::new(Default::default(), memory)
.await
.unwrap()
}
#[tokio::test]
async fn test_execute_test_agentfs_connection_invalid_path() {
let server = create_test_server().await;
let input = crate::mcp::tools::external_signals::TestAgentFsConnectionInput {
db_path: Some("/nonexistent/path".to_string()),
};
let result = server.execute_test_agentfs_connection(input).await.unwrap();
let output: crate::mcp::tools::external_signals::TestAgentFsConnectionOutput =
serde_json::from_value(result).unwrap();
assert!(!output.success);
assert!(output.error.is_some());
}
}