use super::*;
fn write_plugin_agent(plugins_dir: &Path, plugin: &str, agent_name: &str, content: &str) {
let dir = plugins_dir.join(plugin).join("agents");
std::fs::create_dir_all(&dir).expect("mkdir agents dir");
std::fs::write(dir.join(format!("{agent_name}.md")), content).expect("write agent");
}
#[test]
fn discover_plugin_agents_namespaces_entries() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"reviewer",
"---\nname: reviewer\ndescription: Reviews code\nmodel: sonnet\n---\n\nYou review code.\n",
);
let agents = discover_plugin_agents(tmp.path());
assert_eq!(agents.len(), 1);
assert_eq!(agents[0].agent.name, "my-plugin:reviewer");
assert_eq!(agents[0].agent.description.as_deref(), Some("Reviews code"));
assert_eq!(agents[0].agent.model.as_deref(), Some("sonnet"));
assert_eq!(agents[0].system_prompt.content, "You review code.");
}
#[test]
fn discover_plugin_agents_excludes_base_named_agent() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"base-engineer",
"---\nname: base-engineer\n---\n\nTemplate only.\n",
);
write_plugin_agent(
&plugins_dir,
"my-plugin",
"real-agent",
"---\nname: real-agent\n---\n\nBody.\n",
);
let agents = discover_plugin_agents(tmp.path());
let names: Vec<&str> = agents.iter().map(|a| a.agent.name.as_str()).collect();
assert_eq!(names, vec!["my-plugin:real-agent"]);
}
#[test]
fn discover_plugin_agents_skips_unparseable_agent() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
std::fs::create_dir_all(
plugins_dir
.join("my-plugin")
.join("agents")
.join("broken.md"),
)
.expect("mkdir");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"good-agent",
"---\nname: good-agent\n---\n\nBody.\n",
);
let agents = discover_plugin_agents(tmp.path());
let names: Vec<&str> = agents.iter().map(|a| a.agent.name.as_str()).collect();
assert_eq!(names, vec!["my-plugin:good-agent"]);
}
#[test]
fn load_plugin_agent_projects_fields() {
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("solo.md");
std::fs::write(
&path,
"---\nname: solo\nrole: engineer\ndescription: A lone agent\nmodel: sonnet\nmax_tokens: 4096\ntools: [read_file]\n---\n\nBody text.\n",
)
.expect("write");
let cfg = load_plugin_agent("plug", "solo", &path, tmp.path()).expect("load");
assert_eq!(cfg.agent.name, "plug:solo");
assert_eq!(cfg.agent.role.as_deref(), Some("engineer"));
assert_eq!(cfg.agent.description.as_deref(), Some("A lone agent"));
assert_eq!(cfg.agent.model.as_deref(), Some("sonnet"));
assert_eq!(cfg.llm.max_tokens, Some(4096));
assert_eq!(cfg.system_prompt.content, "Body text.");
assert_eq!(
cfg.tools.and_then(|t| t.allowed),
Some(vec!["read_file".to_string()])
);
}
#[test]
fn load_plugin_agent_warns_and_drops_unsupported_fields() {
crate::test_support::begin_capture();
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("fancy.md");
std::fs::write(
&path,
"---\nname: fancy\neffort: high\nmaxTurns: 10\nmemory: enabled\nisolation: worktree\ndisallowedTools: [Bash]\n---\n\nBody.\n",
)
.expect("write");
let cfg =
load_plugin_agent("plug", "fancy", &path, tmp.path()).expect("load must still succeed");
assert_eq!(cfg.agent.name, "plug:fancy");
let captured = crate::test_support::captured_at_least(tracing::Level::WARN);
let warning = captured
.iter()
.find(|m| m.contains("unsupported field"))
.unwrap_or_else(|| panic!("expected an unsupported-field warning, got: {captured:?}"));
assert!(warning.contains("plug:fancy"), "got: {warning}");
assert!(warning.contains("effort"), "got: {warning}");
assert!(warning.contains("maxTurns"), "got: {warning}");
assert!(warning.contains("memory"), "got: {warning}");
assert!(warning.contains("isolation"), "got: {warning}");
assert!(warning.contains("disallowedTools"), "got: {warning}");
}
#[test]
fn load_plugin_agent_warns_on_extends_and_treats_as_leaf() {
crate::test_support::begin_capture();
let tmp = tempfile::tempdir().expect("tempdir");
let path = tmp.path().join("child.md");
std::fs::write(
&path,
"---\nname: child\nextends: some-base\n---\n\nOnly my own body.\n",
)
.expect("write");
let cfg =
load_plugin_agent("plug", "child", &path, tmp.path()).expect("load must still succeed");
assert_eq!(cfg.system_prompt.content, "Only my own body.");
let captured = crate::test_support::captured_at_least(tracing::Level::WARN);
let warning = captured
.iter()
.find(|m| m.contains("extends"))
.unwrap_or_else(|| panic!("expected an extends warning, got: {captured:?}"));
assert!(warning.contains("plug:child"), "got: {warning}");
assert!(warning.contains("some-base"), "got: {warning}");
}
#[test]
fn load_plugin_agent_missing_file_errors() {
let result = load_plugin_agent(
"plug",
"ghost",
Path::new("/nonexistent/ghost.md"),
Path::new("/nonexistent"),
);
assert!(result.is_err());
}
#[test]
#[cfg(unix)]
fn load_plugin_agent_rejects_symlinked_file() {
let tmp = tempfile::tempdir().expect("tempdir");
let agents_dir = tmp.path().join("agents");
std::fs::create_dir_all(&agents_dir).expect("mkdir");
let secret_dir = tmp.path().join("outside");
std::fs::create_dir_all(&secret_dir).expect("mkdir");
let secret_path = secret_dir.join("id_rsa");
std::fs::write(
&secret_path,
"-----BEGIN OPENSSH PRIVATE KEY-----\nSECRET\n",
)
.expect("write secret");
let leak_path = agents_dir.join("leak.md");
std::os::unix::fs::symlink(&secret_path, &leak_path).expect("create symlink");
let result = load_plugin_agent("plug", "leak", &leak_path, &agents_dir);
let err = result.expect_err("a symlinked leaf file must be rejected");
let msg = format!("{err:#}");
assert!(
!msg.contains("SECRET"),
"the secret content must never appear anywhere, even in an error message, got: {msg}"
);
}
#[test]
fn find_plugin_agent_config_resolves_known_agent() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"reviewer",
"---\nname: reviewer\n---\n\nBody.\n",
);
let result = find_plugin_agent_config(tmp.path(), "my-plugin", "reviewer");
let cfg = result.expect("must find").expect("must load");
assert_eq!(cfg.agent.name, "my-plugin:reviewer");
}
#[test]
fn find_plugin_agent_config_unknown_plugin_is_none() {
let tmp = tempfile::tempdir().expect("tempdir");
assert!(find_plugin_agent_config(tmp.path(), "no-such-plugin", "reviewer").is_none());
}
#[test]
fn find_plugin_agent_config_unknown_agent_is_none() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"reviewer",
"---\nname: reviewer\n---\n\nBody.\n",
);
assert!(find_plugin_agent_config(tmp.path(), "my-plugin", "ghost").is_none());
}
#[test]
fn find_plugin_agent_config_rejects_traversal_name() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"reviewer",
"---\nname: reviewer\n---\n\nBody.\n",
);
std::fs::write(
tmp.path().join("secret.md"),
"---\nname: secret\n---\n\nSHOULD NEVER BE READ.\n",
)
.expect("write secret");
assert!(
find_plugin_agent_config(tmp.path(), "my-plugin", "../../secret").is_none(),
"a traversal payload in agent_name must be rejected, not resolved"
);
assert!(
find_plugin_agent_config(tmp.path(), "my-plugin", "/etc/passwd").is_none(),
"an absolute-path-shaped agent_name must be rejected"
);
}
#[test]
#[cfg(unix)]
fn discover_plugin_agents_excludes_symlinked_leak_file() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
write_plugin_agent(
&plugins_dir,
"my-plugin",
"reviewer",
"---\nname: reviewer\n---\n\nBody.\n",
);
let secret_dir = tmp.path().join("outside");
std::fs::create_dir_all(&secret_dir).expect("mkdir");
let secret_path = secret_dir.join("id_rsa");
std::fs::write(&secret_path, "SECRET_KEY_MATERIAL").expect("write secret");
let leak_path = plugins_dir.join("my-plugin").join("agents").join("leak.md");
std::os::unix::fs::symlink(&secret_path, &leak_path).expect("create symlink");
let agents = discover_plugin_agents(tmp.path());
let names: Vec<&str> = agents.iter().map(|a| a.agent.name.as_str()).collect();
assert_eq!(
names,
vec!["my-plugin:reviewer"],
"the symlinked entry must be excluded; the real agent must still be listed"
);
assert!(
agents
.iter()
.all(|a| !a.system_prompt.content.contains("SECRET_KEY_MATERIAL")),
"the secret content must never appear in any listed agent's body"
);
}
#[test]
#[cfg(unix)]
fn find_plugin_agent_config_rejects_symlinked_file() {
let tmp = tempfile::tempdir().expect("tempdir");
let plugins_dir = tmp.path().join(".claude").join("plugins");
std::fs::create_dir_all(plugins_dir.join("my-plugin").join("agents")).expect("mkdir");
let secret_dir = tmp.path().join("outside");
std::fs::create_dir_all(&secret_dir).expect("mkdir");
let secret_path = secret_dir.join("id_rsa");
std::fs::write(&secret_path, "SECRET_KEY_MATERIAL").expect("write secret");
let leak_path = plugins_dir.join("my-plugin").join("agents").join("leak.md");
std::os::unix::fs::symlink(&secret_path, &leak_path).expect("create symlink");
match find_plugin_agent_config(tmp.path(), "my-plugin", "leak") {
None => {}
Some(Ok(cfg)) => panic!(
"a symlinked agent must never resolve successfully, got: {:?}",
cfg.system_prompt.content
),
Some(Err(e)) => {
let msg = format!("{e:#}");
assert!(
!msg.contains("SECRET_KEY_MATERIAL"),
"the secret content must never leak into the error message, got: {msg}"
);
}
}
}