use tempfile::tempdir;
use super::support::{global_file, one_stdio_server};
use crate::mcp::{McpToolSet, register_configured_tools};
use crate::tools::registry::ToolRegistry;
#[tokio::test]
async fn an_unconfigured_run_registers_nothing_and_reports_nothing() {
let home = tempdir().expect("tempdir");
let project = tempdir().expect("tempdir");
let set = McpToolSet::load_at(&home.path().join("servers.toml"), project.path()).await;
let mut registry = ToolRegistry::new();
let diagnostics = register_configured_tools(&mut registry, &set);
assert!(set.is_empty());
assert!(diagnostics.statuses.is_empty());
assert!(diagnostics.issues.is_empty());
assert_eq!(diagnostics.usable_count(), 0);
}
#[tokio::test]
async fn a_failed_server_does_not_stop_the_others() {
let home = tempdir().expect("tempdir");
let project = tempdir().expect("tempdir");
let mut text = one_stdio_server("ghost-a", "/nonexistent/mcp/a-5428");
text.push_str(&one_stdio_server("ghost-b", "/nonexistent/mcp/b-5428"));
let global = global_file(home.path(), &text);
let set = McpToolSet::load_at(&global, project.path()).await;
let diagnostics = set.diagnostics();
assert_eq!(diagnostics.statuses.len(), 2, "both are reported");
assert!(diagnostics.statuses.iter().all(|s| !s.usable));
assert!(
diagnostics.statuses[1]
.detail
.as_deref()
.is_some_and(|d| d.contains("b-5428")),
"the second server was still attempted: {:?}",
diagnostics.statuses[1],
);
assert!(set.tool_names().is_empty());
}
#[tokio::test]
async fn a_malformed_global_file_reaches_the_diagnostics() {
let home = tempdir().expect("tempdir");
let project = tempdir().expect("tempdir");
let global = global_file(home.path(), "not = = toml [[[");
let set = McpToolSet::load_at(&global, project.path()).await;
let mut registry = ToolRegistry::new();
let diagnostics = register_configured_tools(&mut registry, &set);
assert!(diagnostics.tools.is_empty());
assert!(
!diagnostics.issues.is_empty(),
"a broken file MUST be reported, never read as an empty catalog",
);
diagnostics.log();
}
#[tokio::test]
async fn the_live_path_loads_nothing_under_a_test_harness() {
let project = tempdir().expect("tempdir");
let set = McpToolSet::load(project.path()).await;
assert!(set.is_empty());
assert!(set.diagnostics().statuses.is_empty());
}
#[cfg(unix)]
#[tokio::test]
async fn an_unwanted_server_is_never_spawned() {
let home = tempdir().expect("tempdir");
let project = tempdir().expect("tempdir");
let script = |name: &str| {
let marker = home.path().join(format!("{name}.started"));
let path = home.path().join(format!("{name}.sh"));
trusty_common::exec_stub::write_executable(
&path,
format!("#!/bin/sh\ntouch '{}'\n", marker.display()),
)
.expect("write script");
(path, marker)
};
let (wanted_cmd, wanted_marker) = script("wanted");
let (other_cmd, other_marker) = script("other");
let mut text = one_stdio_server("wanted", &wanted_cmd.display().to_string());
text.push_str(&one_stdio_server("other", &other_cmd.display().to_string()));
let global = global_file(home.path(), &text);
let set =
McpToolSet::load_at_only(&global, project.path(), &|name: &str| name == "wanted").await;
assert!(wanted_marker.exists(), "the wanted server was started");
assert!(
!other_marker.exists(),
"the unwanted server must never start"
);
let names: Vec<&str> = set
.diagnostics()
.statuses
.iter()
.map(|s| s.name.as_str())
.collect();
assert_eq!(names, ["wanted"], "only the wanted server is reported");
}