use super::*;
use tempfile::TempDir;
fn sample_entry() -> SkillManifestEntry {
SkillManifestEntry {
checksum: checksum("hello world"),
deployed_at: "2026-05-19T00:00:00Z".into(),
}
}
#[test]
fn skill_manifest_load_missing_returns_empty() {
let tmp = TempDir::new().unwrap();
let manifest = SkillManifest::load(tmp.path()).expect("an absent ledger is not an error");
assert_eq!(manifest.version, SKILL_MANIFEST_VERSION);
assert!(manifest.managed.is_empty());
}
#[test]
fn skill_manifest_load_malformed_is_an_error() {
let tmp = TempDir::new().unwrap();
std::fs::write(tmp.path().join(SKILL_MANIFEST_FILE), b"not valid json{{{").unwrap();
let err = SkillManifest::load(tmp.path()).expect_err("a malformed ledger must be an error");
assert!(
matches!(err, ManifestError::Json(_)),
"expected a Json error, got {err:?}"
);
}
#[test]
fn skill_manifest_load_truncated_is_an_error() {
let tmp = TempDir::new().unwrap();
std::fs::write(
tmp.path().join(SKILL_MANIFEST_FILE),
b"{\"version\":1,\"managed\":{",
)
.unwrap();
assert!(
SkillManifest::load(tmp.path()).is_err(),
"a truncated ledger must be an error"
);
}
#[test]
#[cfg(unix)]
fn skill_manifest_load_unreadable_is_an_error() {
use std::os::unix::fs::PermissionsExt;
let tmp = TempDir::new().unwrap();
let mut manifest = SkillManifest::default();
manifest.managed.insert("tm-doctor".into(), sample_entry());
manifest.save(tmp.path()).unwrap();
let path = tmp.path().join(SKILL_MANIFEST_FILE);
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();
let loaded = SkillManifest::load(tmp.path());
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
let err = loaded.expect_err("an unreadable ledger must be an error, not an empty one");
assert!(
matches!(&err, ManifestError::Io(e) if e.kind() == std::io::ErrorKind::PermissionDenied),
"expected a PermissionDenied Io error, got {err:?}"
);
assert!(
err.to_string().contains(SKILL_MANIFEST_FILE),
"the error must name the ledger it could not read: {err}"
);
}
#[test]
fn skill_manifest_deploy_refuses_a_ledger_it_could_not_read() {
let src = TempDir::new().unwrap();
let dest = TempDir::new().unwrap();
std::fs::write(src.path().join("tm-doctor.md"), "v1").unwrap();
crate::skills::deployer::deploy_skills(src.path(), dest.path()).unwrap();
let ledger = dest.path().join(SKILL_MANIFEST_FILE);
assert!(
SkillManifest::load(dest.path())
.unwrap()
.is_managed("tm-doctor"),
"the first deploy must record the skill"
);
let corrupt = "{\"version\":1,\"managed\":{\"tm-doctor\":";
std::fs::write(&ledger, corrupt).unwrap();
std::fs::write(src.path().join("tm-doctor.md"), "v2").unwrap();
let result = crate::skills::deployer::deploy_skills(src.path(), dest.path());
assert!(
result.is_err(),
"the deploy must refuse rather than act as if nothing is owned"
);
assert_eq!(
std::fs::read_to_string(&ledger).unwrap(),
corrupt,
"a refused deploy must leave the ledger exactly as it found it"
);
assert_eq!(
std::fs::read_to_string(dest.path().join("tm-doctor").join("SKILL.md")).unwrap(),
"v1",
"a refused deploy writes no skill content either"
);
}
#[test]
fn skill_manifest_round_trip() {
let tmp = TempDir::new().unwrap();
let mut manifest = SkillManifest::default();
manifest
.managed
.insert("tm-doctor.md".into(), sample_entry());
manifest.save(tmp.path()).unwrap();
let loaded = SkillManifest::load(tmp.path()).unwrap();
assert_eq!(loaded, manifest);
assert!(tmp.path().join(SKILL_MANIFEST_FILE).exists());
}
#[test]
fn skill_manifest_checksum_matches() {
let mut manifest = SkillManifest::default();
manifest
.managed
.insert("tm-doctor.md".into(), sample_entry());
assert!(manifest.checksum_matches("tm-doctor.md", "hello world"));
assert!(!manifest.checksum_matches("tm-doctor.md", "hello world!"));
assert!(!manifest.checksum_matches("other.md", "hello world"));
}
#[test]
fn skill_manifest_is_managed() {
let mut manifest = SkillManifest::default();
manifest
.managed
.insert("tm-doctor.md".into(), sample_entry());
assert!(manifest.is_managed("tm-doctor.md"));
assert!(!manifest.is_managed("user-skill.md"));
}
#[test]
fn skill_manifest_file_name_differs_from_agent_manifest() {
assert_ne!(SKILL_MANIFEST_FILE, crate::agents::manifest::MANIFEST_FILE);
}
#[test]
fn skill_manifest_lock_path_is_a_sidecar() {
let dir = Path::new("/some/skills");
assert_eq!(
skill_manifest_lock_path(dir),
dir.join(".trusty-mpm-skills-manifest.json.lock")
);
assert_ne!(
skill_manifest_lock_path(dir),
crate::agents::manifest::manifest_lock_path(dir)
);
}
#[test]
fn skill_manifest_lock_serialises_concurrent_writers() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path().to_path_buf();
SkillManifest::default().save(&dir).unwrap();
let handles: Vec<_> = (0..8)
.map(|i| {
let dir = dir.clone();
std::thread::spawn(move || {
with_skill_manifest_lock::<(), ManifestError, _>(&dir, || {
let mut m = SkillManifest::load(&dir).unwrap();
m.managed.insert(format!("skill-{i}"), sample_entry());
std::thread::sleep(std::time::Duration::from_millis(5));
m.save(&dir)
})
.unwrap();
})
})
.collect();
for h in handles {
h.join().unwrap();
}
let final_manifest = SkillManifest::load(&dir).unwrap();
assert_eq!(
final_manifest.managed.len(),
8,
"every writer's entry must survive: {:?}",
final_manifest.managed.keys().collect::<Vec<_>>()
);
}
#[test]
fn skill_manifest_lock_releases_so_a_second_acquisition_succeeds() {
let tmp = TempDir::new().unwrap();
for _ in 0..3 {
with_skill_manifest_lock::<(), ManifestError, _>(tmp.path(), || Ok(())).unwrap();
}
}
#[test]
fn skill_manifest_save_merging_folds_in_a_concurrent_writer() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path();
let mut base = SkillManifest::default();
base.managed.insert("shared".into(), sample_entry());
base.save(dir).unwrap();
let mut newer = base.clone();
newer
.managed
.insert("from-the-other-writer".into(), sample_entry());
newer.save(dir).unwrap();
let mut ours = base.clone();
ours.managed.insert("ours".into(), sample_entry());
assert_eq!(
ours.save_merging(dir, &base).unwrap(),
SkillManifestSave::Merged
);
let on_disk = SkillManifest::load(dir).unwrap();
assert!(on_disk.is_managed("ours"), "our own entry must be recorded");
assert!(
on_disk.is_managed("from-the-other-writer"),
"the racing writer's entry must survive"
);
assert!(on_disk.is_managed("shared"));
}
#[test]
fn skill_manifest_save_merging_applies_this_runs_removals() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path();
let mut base = SkillManifest::default();
base.managed.insert("keep".into(), sample_entry());
base.managed.insert("prune-me".into(), sample_entry());
base.save(dir).unwrap();
let mut newer = base.clone();
newer.managed.insert("arrived-later".into(), sample_entry());
newer.save(dir).unwrap();
let mut ours = base.clone();
ours.managed.remove("prune-me");
assert_eq!(
ours.save_merging(dir, &base).unwrap(),
SkillManifestSave::Merged
);
let on_disk = SkillManifest::load(dir).unwrap();
assert!(!on_disk.is_managed("prune-me"), "our removal must apply");
assert!(on_disk.is_managed("keep"));
assert!(
on_disk.is_managed("arrived-later"),
"the racing writer's insert must survive a prune merge"
);
}
#[test]
fn skill_manifest_save_merging_writes_when_unchanged() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path();
let base = SkillManifest::default();
let mut first = base.clone();
first.managed.insert("tm-doctor".into(), sample_entry());
assert_eq!(
first.save_merging(dir, &base).unwrap(),
SkillManifestSave::Written
);
assert!(SkillManifest::load(dir).unwrap().is_managed("tm-doctor"));
let base = SkillManifest::load(dir).unwrap();
let mut second = base.clone();
second.managed.insert("tm-workflow".into(), sample_entry());
assert_eq!(
second.save_merging(dir, &base).unwrap(),
SkillManifestSave::Written
);
let on_disk = SkillManifest::load(dir).unwrap();
assert!(on_disk.is_managed("tm-doctor"));
assert!(on_disk.is_managed("tm-workflow"));
}
#[test]
fn skill_manifest_save_merging_over_a_corrupt_ledger_keeps_the_base() {
let tmp = TempDir::new().unwrap();
let dir = tmp.path();
let mut base = SkillManifest::default();
for i in 0..9 {
base.managed.insert(format!("s{i}"), sample_entry());
}
base.save(dir).unwrap();
std::fs::write(dir.join(SKILL_MANIFEST_FILE), "{ not json").unwrap();
let mut ours = base.clone();
ours.managed.insert("ours".into(), sample_entry());
assert_eq!(
ours.save_merging(dir, &base).unwrap(),
SkillManifestSave::OverwroteUnreadable
);
let on_disk = SkillManifest::load(dir).unwrap();
assert_eq!(
on_disk.managed.len(),
10,
"the base's 9 entries plus ours must survive, not just ours: {:?}",
on_disk.managed.keys().collect::<Vec<_>>()
);
for i in 0..9 {
assert!(on_disk.is_managed(&format!("s{i}")));
}
assert!(on_disk.is_managed("ours"));
}