use super::*;
use std::fs;
use tempfile::TempDir;
fn write_sources(dir: &Path) {
fs::write(
dir.join("tm-doctor.md"),
"---\nname: tm-doctor\n---\n\n# Doctor\n\nDiagnostic skill.\n",
)
.unwrap();
fs::write(
dir.join("example-skill.md"),
"---\nname: example-skill\n---\n\n# Example\n\nExample skill.\n",
)
.unwrap();
}
#[test]
fn deploy_new_skill() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert_eq!(stats.deployed.len(), 2);
assert!(stats.deployed.contains(&"tm-doctor".to_string()));
assert!(stats.skipped.is_empty());
assert!(stats.unchanged.is_empty());
let doctor = fs::read_to_string(tgt.path().join("tm-doctor").join("SKILL.md")).unwrap();
assert!(doctor.contains("Diagnostic skill."));
let manifest = SkillManifest::load(tgt.path());
assert!(manifest.is_managed("tm-doctor"));
}
#[test]
fn deploy_filtered_respects_predicate() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
let stats =
deploy_skills_filtered(src.path(), tgt.path(), |name| name == "example-skill").unwrap();
assert!(stats.deployed.contains(&"example-skill".to_string()));
assert!(!stats.deployed.contains(&"tm-doctor".to_string()));
assert!(tgt.path().join("example-skill").join("SKILL.md").exists());
assert!(!tgt.path().join("tm-doctor").exists());
}
#[test]
fn deploy_skips_user_modified() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
deploy_skills(src.path(), tgt.path()).unwrap();
fs::write(
tgt.path().join("tm-doctor").join("SKILL.md"),
"---\nname: tm-doctor\n---\n\nUSER HAND-EDIT\n",
)
.unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(stats.skipped.contains(&"tm-doctor".to_string()));
assert!(!stats.deployed.contains(&"tm-doctor".to_string()));
let still = fs::read_to_string(tgt.path().join("tm-doctor").join("SKILL.md")).unwrap();
assert!(still.contains("USER HAND-EDIT"));
}
#[test]
fn deploy_unchanged_no_write() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
deploy_skills(src.path(), tgt.path()).unwrap();
let skill_md = tgt.path().join("tm-doctor").join("SKILL.md");
let before = fs::metadata(&skill_md).unwrap().modified().unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(stats.unchanged.contains(&"tm-doctor".to_string()));
assert!(stats.deployed.is_empty());
let after = fs::metadata(&skill_md).unwrap().modified().unwrap();
assert_eq!(before, after, "unchanged file must not be rewritten");
}
#[test]
fn deploy_user_owned_skipped() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
let user_dir = tgt.path().join("tm-doctor");
fs::create_dir_all(&user_dir).unwrap();
fs::write(user_dir.join("SKILL.md"), "USER OWNED — not trusty-mpm's\n").unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(stats.skipped.contains(&"tm-doctor".to_string()));
let content = fs::read_to_string(user_dir.join("SKILL.md")).unwrap();
assert_eq!(content, "USER OWNED — not trusty-mpm's\n");
assert!(stats.deployed.contains(&"example-skill".to_string()));
}
#[test]
fn deploy_refreshes_stale_managed_skill() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
deploy_skills(src.path(), tgt.path()).unwrap();
fs::write(
src.path().join("tm-doctor.md"),
"---\nname: tm-doctor\n---\n\n# Doctor v2\n",
)
.unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(stats.deployed.contains(&"tm-doctor".to_string()));
let refreshed = fs::read_to_string(tgt.path().join("tm-doctor").join("SKILL.md")).unwrap();
assert!(refreshed.contains("Doctor v2"));
}
#[test]
fn deploy_skips_mcp_named_skill() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path()); fs::write(
src.path().join("toolchains-ai-protocols-mcp.md"),
"---\nname: toolchains-ai-protocols-mcp\n---\n\n# AI Protocols\n\nMCP guidance.\n",
)
.unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats
.skipped
.contains(&"toolchains-ai-protocols-mcp".to_string())
);
assert!(
!stats
.deployed
.contains(&"toolchains-ai-protocols-mcp".to_string())
);
assert!(
!tgt.path()
.join("toolchains-ai-protocols-mcp")
.join("SKILL.md")
.exists(),
"an mcp-named skill must never be written to disk"
);
assert!(stats.deployed.contains(&"tm-doctor".to_string()));
assert!(stats.deployed.contains(&"example-skill".to_string()));
}
#[test]
fn deploy_skips_mcp_named_skill_case_insensitive() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path()); fs::write(
src.path().join("Foo-MCP.md"),
"---\nname: Foo-MCP\n---\n\n# Foo MCP\n\nSomething.\n",
)
.unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(stats.skipped.contains(&"Foo-MCP".to_string()));
assert!(!stats.deployed.contains(&"Foo-MCP".to_string()));
assert!(!tgt.path().join("Foo-MCP").exists());
}
#[test]
fn deploy_missing_source_dir_is_empty_result() {
let tgt = TempDir::new().unwrap();
let stats = deploy_skills(Path::new("/nonexistent/trusty-mpm/skills"), tgt.path()).unwrap();
assert_eq!(stats, DeployStats::default());
}
fn write_multi_file_skill(dir: &Path, stem: &str, refs: &[(&str, &str)]) {
fs::write(
dir.join(format!("{stem}.md")),
format!("---\nname: {stem}\n---\n\nEntry point.\n"),
)
.unwrap();
let refs_dir = dir.join(stem).join("references");
fs::create_dir_all(&refs_dir).unwrap();
for (name, body) in refs {
fs::write(refs_dir.join(name), body).unwrap();
}
}
#[test]
fn deploy_reference_files_land_alongside_skill() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_multi_file_skill(
src.path(),
"systematic-debugging",
&[("workflow.md", "WORKFLOW"), ("examples.md", "EXAMPLES")],
);
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(stats.deployed.contains(&"systematic-debugging".to_string()));
assert!(
stats
.deployed
.contains(&"systematic-debugging/references/workflow.md".to_string())
);
assert!(
stats
.deployed
.contains(&"systematic-debugging/references/examples.md".to_string())
);
let workflow = fs::read_to_string(
tgt.path()
.join("systematic-debugging")
.join("references")
.join("workflow.md"),
)
.unwrap();
assert_eq!(workflow, "WORKFLOW");
let examples = fs::read_to_string(
tgt.path()
.join("systematic-debugging")
.join("references")
.join("examples.md"),
)
.unwrap();
assert_eq!(examples, "EXAMPLES");
}
#[test]
fn deploy_reference_files_skip_user_modified() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_multi_file_skill(src.path(), "systematic-debugging", &[("workflow.md", "V1")]);
deploy_skills(src.path(), tgt.path()).unwrap();
let ref_path = tgt
.path()
.join("systematic-debugging")
.join("references")
.join("workflow.md");
fs::write(&ref_path, "USER HAND-EDIT").unwrap();
fs::write(
src.path()
.join("systematic-debugging")
.join("references")
.join("workflow.md"),
"V2",
)
.unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats
.skipped
.contains(&"systematic-debugging/references/workflow.md".to_string())
);
assert_eq!(fs::read_to_string(&ref_path).unwrap(), "USER HAND-EDIT");
}
#[test]
fn deploy_reference_files_sync_even_when_entry_unchanged() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_multi_file_skill(src.path(), "systematic-debugging", &[]);
deploy_skills(src.path(), tgt.path()).unwrap();
let refs_dir = src.path().join("systematic-debugging").join("references");
fs::write(refs_dir.join("new-ref.md"), "NEW REF").unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats
.unchanged
.contains(&"systematic-debugging".to_string()),
"entry point content is unchanged: {stats:?}"
);
assert!(
stats
.deployed
.contains(&"systematic-debugging/references/new-ref.md".to_string())
);
assert!(
tgt.path()
.join("systematic-debugging")
.join("references")
.join("new-ref.md")
.is_file()
);
}
#[test]
fn deploy_single_file_skill_has_no_references_dir() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
deploy_skills(src.path(), tgt.path()).unwrap();
assert!(!tgt.path().join("tm-doctor").join("references").exists());
}