use super::*;
use crate::agents::manifest::ManifestError;
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()).unwrap();
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());
}
#[test]
fn deploy_blocks_while_the_skill_ledger_lock_is_held() {
use std::sync::mpsc;
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
let (tx, rx) = mpsc::channel();
let src_path = src.path().to_path_buf();
let deploy_target = tgt.path().to_path_buf();
let deployed_marker = tgt.path().join("tm-doctor").join("SKILL.md");
let handle = crate::skills::manifest::with_skill_manifest_lock::<_, ManifestError, _>(
tgt.path(),
|| {
let handle = std::thread::spawn(move || {
tx.send(deploy_skills(&src_path, &deploy_target).unwrap())
.ok();
});
assert!(
rx.recv_timeout(std::time::Duration::from_secs(2)).is_err(),
"deploy completed while the skill ledger lock was held — its \
load-modify-save is not inside the lock"
);
assert!(
!deployed_marker.exists(),
"deploy wrote a skill file while the ledger lock was held"
);
Ok(handle)
},
)
.unwrap();
handle.join().unwrap();
let stats = rx
.recv_timeout(std::time::Duration::from_secs(30))
.expect("deploy must complete once the lock is released");
assert_eq!(stats.deployed.len(), 2);
assert!(
SkillManifest::load(tgt.path())
.unwrap()
.is_managed("tm-doctor"),
"the released deploy must have recorded its entries"
);
}
#[test]
fn a_racing_writer_freezes_nothing_on_either_side() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path());
deploy_skills(src.path(), tgt.path()).unwrap();
let base = SkillManifest::load(tgt.path()).unwrap();
let mut racer = base.clone();
racer.managed.insert(
"racing-writer-skill".into(),
SkillManifestEntry {
checksum: checksum("racer content"),
deployed_at: "2026-08-05T00:00:00Z".into(),
},
);
let racer_dir = tgt.path().join("racing-writer-skill");
fs::create_dir_all(&racer_dir).unwrap();
fs::write(racer_dir.join("SKILL.md"), "racer content").unwrap();
racer.save(tgt.path()).unwrap();
let mut ours = base.clone();
for stem in ["tm-doctor", "example-skill"] {
let content = format!("---\nname: {stem}\n---\n\nv2 content\n");
let dir = tgt.path().join(stem);
fs::write(dir.join("SKILL.md"), &content).unwrap();
fs::write(src.path().join(format!("{stem}.md")), &content).unwrap();
ours.managed.insert(
stem.to_string(),
SkillManifestEntry {
checksum: checksum(&content),
deployed_at: "2026-08-05T00:00:01Z".into(),
},
);
}
assert_eq!(
ours.save_merging(tgt.path(), &base).unwrap(),
SkillManifestSave::Merged
);
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats.skipped.is_empty(),
"a racing writer must freeze nothing: skipped={:?}",
stats.skipped
);
assert_eq!(stats.unchanged.len(), 2, "both skills must stay writable");
let final_manifest = SkillManifest::load(tgt.path()).unwrap();
assert!(
final_manifest.is_managed("racing-writer-skill"),
"the racing writer's entry must survive, or ITS file freezes instead"
);
assert!(final_manifest.checksum_matches("racing-writer-skill", "racer content"));
}
#[test]
fn deploy_records_files_written_before_a_mid_loop_failure() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
fs::write(
src.path().join("aaa-skill.md"),
"---\nname: aaa-skill\n---\n\nfirst\n",
)
.unwrap();
fs::write(src.path().join("zzz-skill.md"), [0xff, 0xfe, 0xff]).unwrap();
let err = deploy_skills(src.path(), tgt.path());
assert!(err.is_err(), "an unreadable source must still fail the run");
let written = tgt.path().join("aaa-skill").join("SKILL.md");
assert!(written.exists(), "aaa-skill was written before the failure");
let manifest = SkillManifest::load(tgt.path()).unwrap();
assert!(
manifest.is_managed("aaa-skill"),
"a file written before the failure must still be recorded"
);
fs::remove_file(src.path().join("zzz-skill.md")).unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats.skipped.is_empty(),
"nothing may be frozen by a mid-loop failure: skipped={:?}",
stats.skipped
);
assert!(stats.unchanged.contains(&"aaa-skill".to_string()));
}
fn write_directory_skill(dir: &Path, stem: &str) {
let skill = dir.join(stem);
fs::create_dir_all(skill.join("references")).unwrap();
fs::write(skill.join("SKILL.md"), format!("# {stem}\n")).unwrap();
fs::write(skill.join("metadata.json"), "{\"v\":1}\n").unwrap();
fs::write(skill.join("references").join("tokens.md"), "# Tokens\n").unwrap();
fs::write(skill.join("references").join("index.md"), "# Index\n").unwrap();
}
#[test]
fn deploy_directory_shaped_skill() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_directory_skill(src.path(), "duetto-design-system");
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats.deployed.contains(&"duetto-design-system".to_string()),
"the entry point must deploy: {stats:?}"
);
let dest = tgt.path().join("duetto-design-system");
assert!(dest.join("SKILL.md").is_file(), "SKILL.md missing");
assert!(
dest.join("metadata.json").is_file(),
"metadata.json missing"
);
assert!(
dest.join("references").join("tokens.md").is_file(),
"references/tokens.md missing"
);
assert!(
dest.join("references").join("index.md").is_file(),
"references/index.md missing"
);
assert!(
stats.skipped.is_empty(),
"nothing may be skipped: {stats:?}"
);
}
#[test]
fn deploy_directory_skill_records_every_file_in_the_manifest() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_directory_skill(src.path(), "duetto-design-system");
deploy_skills(src.path(), tgt.path()).unwrap();
let manifest = SkillManifest::load(tgt.path()).unwrap();
for key in [
"duetto-design-system",
"duetto-design-system/metadata.json",
"duetto-design-system/references/tokens.md",
"duetto-design-system/references/index.md",
] {
assert!(manifest.is_managed(key), "manifest missing key {key}");
}
let again = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(again.deployed.is_empty(), "second deploy wrote: {again:?}");
assert!(again.skipped.is_empty(), "second deploy froze: {again:?}");
assert_eq!(again.unchanged.len(), 4, "{again:?}");
}
#[test]
fn deploy_flat_skill_still_works_alongside_a_directory_skill() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_sources(src.path()); fs::create_dir_all(src.path().join("tm-doctor").join("references")).unwrap();
fs::write(
src.path()
.join("tm-doctor")
.join("references")
.join("cli.md"),
"# CLI\n",
)
.unwrap();
write_directory_skill(src.path(), "duetto-design-system");
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
for stem in ["tm-doctor", "example-skill", "duetto-design-system"] {
assert!(
tgt.path().join(stem).join("SKILL.md").is_file(),
"{stem}/SKILL.md missing: {stats:?}"
);
}
assert!(
tgt.path()
.join("tm-doctor")
.join("references")
.join("cli.md")
.is_file()
);
let manifest = SkillManifest::load(tgt.path()).unwrap();
assert!(manifest.is_managed("tm-doctor/references/cli.md"));
}
#[test]
fn deploy_reports_a_skill_directory_with_no_entry_point() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
fs::create_dir_all(src.path().join("broken-skill")).unwrap();
fs::write(src.path().join("broken-skill").join("notes.md"), "x\n").unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats.skipped.contains(&"broken-skill".to_string()),
"a malformed skill directory must be reported: {stats:?}"
);
assert!(stats.deployed.is_empty(), "{stats:?}");
assert!(
!tgt.path().join("broken-skill").exists(),
"a malformed skill must not be partially written"
);
}
#[test]
fn deploy_directory_skill_respects_the_select_predicate() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_directory_skill(src.path(), "duetto-design-system");
write_directory_skill(src.path(), "cto-kb-ingest");
let stats =
deploy_skills_filtered(src.path(), tgt.path(), |name| name == "cto-kb-ingest").unwrap();
assert!(stats.deployed.contains(&"cto-kb-ingest".to_string()));
assert!(!tgt.path().join("duetto-design-system").exists());
}
#[test]
fn deploy_directory_skill_preserves_a_user_edited_reference() {
let src = TempDir::new().unwrap();
let tgt = TempDir::new().unwrap();
write_directory_skill(src.path(), "duetto-design-system");
deploy_skills(src.path(), tgt.path()).unwrap();
let edited = tgt
.path()
.join("duetto-design-system")
.join("references")
.join("tokens.md");
fs::write(&edited, "# Tokens (hand-edited)\n").unwrap();
fs::write(
src.path()
.join("duetto-design-system")
.join("references")
.join("index.md"),
"# Index v2\n",
)
.unwrap();
let stats = deploy_skills(src.path(), tgt.path()).unwrap();
assert!(
stats
.skipped
.contains(&"duetto-design-system/references/tokens.md".to_string()),
"{stats:?}"
);
assert_eq!(
fs::read_to_string(&edited).unwrap(),
"# Tokens (hand-edited)\n"
);
assert!(
stats
.deployed
.contains(&"duetto-design-system/references/index.md".to_string()),
"{stats:?}"
);
}