use serde_json::{json, Value};
use trusty_mcp::Request;
pub(crate) fn local_answer(req: &Request, default_palace: Option<&str>) -> Option<Value> {
match req.method.as_str() {
"initialize" => Some(trusty_mcp::initialize_response(
"trusty-memory",
env!("CARGO_PKG_VERSION"),
default_palace.map(|p| json!({ "default_palace": p })),
)),
"tools/list" => Some(crate::tools::tool_definitions_with(
default_palace.is_some(),
)),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn req(method: &str) -> Request {
Request {
jsonrpc: Some("2.0".to_string()),
id: Some(json!(1)),
method: method.to_string(),
params: None,
}
}
#[test]
fn initialize_is_answered_without_the_daemon() {
let answer = local_answer(&req("initialize"), None).expect("initialize is answered here");
assert_eq!(answer["protocolVersion"], "2024-11-05");
assert_eq!(answer["serverInfo"]["name"], "trusty-memory");
assert_eq!(answer["serverInfo"]["version"], env!("CARGO_PKG_VERSION"));
let with_palace = local_answer(&req("initialize"), Some("owner-profile"))
.expect("initialize is answered here");
assert_eq!(with_palace["serverInfo"]["default_palace"], "owner-profile");
}
#[test]
fn tools_list_is_the_crate_table() {
for has_default in [false, true] {
let palace = has_default.then_some("owner-profile");
let answer = local_answer(&req("tools/list"), palace).expect("tools/list is answered");
assert_eq!(answer, crate::tools::tool_definitions_with(has_default));
}
assert_ne!(
crate::tools::tool_definitions_with(false),
crate::tools::tool_definitions_with(true),
"the palace default must actually change the schema, or the \
assertions above hold for the wrong reason"
);
}
#[test]
fn everything_else_is_left_to_the_daemon() {
for method in ["tools/call", "memory_recall", "ping", "rpc.discover"] {
assert!(
local_answer(&req(method), Some("owner-profile")).is_none(),
"{method} must reach the daemon"
);
}
}
}