use std::path::Path;
use super::suggest::{from_command, from_url, slug, suggest, unique};
use super::*;
use crate::env::Os;
use crate::fs::RealFs;
fn argv(text: &str) -> Vec<String> {
split_command(text).unwrap()
}
#[test]
fn slugs_follow_the_server_id_rule() {
assert_eq!(slug("n8n MCP for PES"), "n8n-mcp-for-pes");
assert_eq!(slug(" --Weather MCP!! "), "weather-mcp");
assert_eq!(slug("@@"), "server");
assert_eq!(slug("Ünïcode"), "n-code");
let long = slug(&"a-".repeat(80));
assert!(long.len() <= 64 && !long.ends_with('-'), "{long}");
for text in ["x", "A.B_C", "-a-", "..."] {
let id = slug(text);
assert!(
crate::model::ids::is_valid(crate::model::ids::IdKind::Server, &id),
"{text:?} -> {id:?}"
);
}
}
#[test]
fn unique_numbers_taken_ids_and_keeps_the_length_rule() {
let taken = ["github", "github-2"];
assert_eq!(unique("github", |id| taken.contains(&id)), "github-3");
assert_eq!(unique("fresh", |id| taken.contains(&id)), "fresh");
let long = "a".repeat(64);
let id = unique(&long, |id| id == long);
assert!(id.len() <= 64 && id.ends_with("-2"), "{id}");
}
#[test]
fn addresses_suggest_the_service_name() {
for (url, id) in [
(
"https://n8n.pesengineers.dev/mcp-server/http",
"n8n-pesengineers",
),
("https://mcp.sentry.dev/mcp", "sentry"),
("https://api.githubcopilot.com/mcp/", "githubcopilot"),
("https://mcp.atlassian.com/v1/sse", "atlassian"),
(
"https://server.smithery.ai/@owner/weather/mcp",
"smithery-weather",
),
("http://localhost:8080/mcp", "localhost-8080"),
("http://127.0.0.1:3000/sse", "localhost-3000"),
("https://example.com/", "example"),
] {
assert_eq!(from_url(url).as_deref(), Some(id), "{url}");
}
let id = from_url("https://actions.zapier.com/mcp/sk-ak-1a2b3c4d5e6f7a8b9c0d/sse").unwrap();
assert_eq!(id, "actions-zapier");
assert_eq!(from_url("not a url"), None);
assert_eq!(from_url("ftp://example.com"), None);
}
#[test]
fn commands_suggest_the_package_or_program() {
for (line, id) in [
(
"npx -y @modelcontextprotocol/server-filesystem C:\\data",
"filesystem",
),
("npx -y @upstash/context7-mcp@latest", "context7"),
("bunx some-mcp-server", "some"),
("uvx mcp-server-fetch", "fetch"),
(
"uvx --from git+https://github.com/acme/weather-mcp.git weather",
"weather",
),
("pipx run mcp-server-time", "time"),
("python -m mcp_server_git --repository .", "git"),
("python3 C:\\tools\\weather\\server.py", "weather"),
("node C:\\tools\\notes\\build\\index.js", "notes"),
(
"node /opt/mcp/github-mcp-server/dist/index.js --stdio",
"github",
),
(
"docker run -i --rm -e GITHUB_TOKEN ghcr.io/github/github-mcp-server",
"github",
),
(
"docker run -i --rm mcp/postgres:latest postgresql://localhost",
"postgres",
),
("cmd /c npx -y @acme/mcp", "acme"),
("\"C:\\Program Files\\Acme\\acme-mcp.exe\" --stdio", "acme"),
("/usr/local/bin/github-mcp-server stdio", "github"),
("pnpm dlx @scope/server-linear", "linear"),
("uv run --with mcp weather.py", "weather"),
] {
assert_eq!(from_command(&argv(line)).as_deref(), Some(id), "{line}");
}
}
#[test]
fn suggest_reads_names_addresses_and_commands() {
assert_eq!(suggest("n8n MCP for PES"), "n8n-mcp-for-pes");
assert_eq!(suggest("https://mcp.notion.com/mcp"), "notion");
assert_eq!(suggest("npx -y @acme/server-weather"), "weather");
assert_eq!(suggest("github tools"), "github-tools");
}
#[test]
fn commands_split_with_quotes_and_windows_paths() {
assert_eq!(
argv(r#"node "C:\Program Files\x\index.js" --flag 'a b'"#),
["node", r"C:\Program Files\x\index.js", "--flag", "a b"]
);
assert_eq!(argv("$ npx -y pkg"), ["npx", "-y", "pkg"]);
assert_eq!(split_command("npx \"unclosed"), None);
assert_eq!(split_command(" "), None);
}
fn env() -> Env {
Env::builder(Os::Windows, "C:\\Users\\me").build()
}
fn analyze_text(text: &str) -> Analysis {
analyze(text, &env(), &RealFs)
}
#[test]
fn analysis_recognizes_each_kind() {
let url = analyze_text(" https://mcp.sentry.dev/mcp ");
assert_eq!(url.kind, InputKind::Url);
assert_eq!(url.url.as_deref(), Some("https://mcp.sentry.dev/mcp"));
assert_eq!(url.transport, Some(Transport::Http));
assert_eq!(url.suggested_id.as_deref(), Some("sentry"));
assert_eq!(url.message, "Remote server (Streamable HTTP)");
let gitlab = analyze_text("https://gitlab.com/api/v4/mcp");
assert_eq!(gitlab.kind, InputKind::Url);
assert_eq!(gitlab.suggested_id.as_deref(), Some("gitlab"));
let sse = analyze_text("https://mcp.example.com/v1/sse/");
assert_eq!(sse.transport, Some(Transport::Sse));
assert_eq!(sse.message, "Remote server (SSE)");
let command = analyze_text("npx -y @modelcontextprotocol/server-github");
assert_eq!(command.kind, InputKind::Command);
assert_eq!(command.command.as_deref(), Some("npx"));
assert_eq!(command.args, ["-y", "@modelcontextprotocol/server-github"]);
assert_eq!(command.transport, Some(Transport::Stdio));
assert_eq!(command.suggested_id.as_deref(), Some("github"));
for repo in [
"https://github.com/acme/weather-mcp",
"https://github.com/acme/weather-mcp/tree/main/server",
"https://git.example.com/acme/weather.git",
"git@github.com:acme/weather.git",
] {
let analysis = analyze_text(repo);
assert_eq!(analysis.kind, InputKind::Repository, "{repo}");
assert_eq!(analysis.repository.as_deref(), Some(repo));
}
let snippet = analyze_text(
r#"{"mcpServers": {"a": {"command": "npx", "args": ["a"]}, "b": {"url": "https://b.example/mcp"}}}"#,
);
assert_eq!(snippet.kind, InputKind::Snippet);
assert_eq!(snippet.snippet_entries, 2);
let toml = analyze_text("[mcp_servers.docs]\ncommand = \"uvx\"\nargs = [\"docs\"]\n");
assert_eq!(toml.kind, InputKind::Snippet);
for unknown in ["", "hello there", "github tools", "{ not json"] {
assert_eq!(
analyze_text(unknown).kind,
InputKind::Unknown,
"{unknown:?}"
);
}
}
#[test]
fn an_unquoted_program_path_with_spaces_keeps_its_arguments() {
let temp = surjection_testkit::TempHome::new();
let dir = temp.root().join("Program Files").join("nodejs");
std::fs::create_dir_all(&dir).unwrap();
let program = dir.join("npx.cmd");
std::fs::write(&program, "").unwrap();
let text = format!("{} -y @acme/server-github", program.display());
let analysis = analyze_text(&text);
assert_eq!(analysis.kind, InputKind::Command);
assert_eq!(
analysis.command.as_deref(),
Some(program.to_string_lossy().as_ref())
);
assert_eq!(analysis.args, ["-y", "@acme/server-github"]);
assert_eq!(analysis.suggested_id.as_deref(), Some("github"));
}
#[test]
fn an_existing_folder_is_a_folder_and_a_missing_one_is_not() {
let temp = surjection_testkit::TempHome::new();
let path = temp.root().to_string_lossy().into_owned();
let analysis = analyze_text(&path);
assert_eq!(analysis.kind, InputKind::Folder);
assert_eq!(analysis.folder.as_deref(), Some(temp.root()));
let missing = Path::new(&path)
.join("missing")
.to_string_lossy()
.into_owned();
assert_ne!(analyze_text(&missing).kind, InputKind::Folder);
}
#[test]
fn launch_keys_match_shims_and_cmd_wrappers() {
let args = |list: &[&str]| list.iter().map(|s| (*s).to_owned()).collect::<Vec<_>>();
let plain = args(&["-y", "@acme/weather"]);
let wrapped = args(&["/c", "npx", "-y", "@acme/weather"]);
assert_eq!(
launch_key(Launch::Command("npx", &plain)),
launch_key(Launch::Command("cmd", &wrapped))
);
assert_eq!(
launch_key(Launch::Command(r"C:\nodejs\npx.cmd", &plain)),
launch_key(Launch::Command("npx", &plain))
);
assert_eq!(
launch_key(Launch::Url("HTTPS://Mcp.Example.com/mcp/")),
launch_key(Launch::Url("https://mcp.example.com/mcp"))
);
assert_ne!(
launch_key(Launch::Url("https://mcp.example.com/a")),
launch_key(Launch::Url("https://mcp.example.com/b"))
);
}