use std::collections::BTreeSet;
use super::*;
use crate::agents::manifest::{AgentManifest, ManifestEntry, Origin, checksum};
use crate::agents::provenance::Provenance;
fn roster(names: &[&str]) -> BTreeSet<String> {
names.iter().map(|s| (*s).to_owned()).collect()
}
fn doc(name: &str) -> String {
format!("---\nname: {name}\nrole: engineer\n---\n\nBody.\n")
}
fn track(dir: &std::path::Path, filename: &str, origin: Origin) {
let mut manifest = AgentManifest::default();
manifest.managed.insert(
filename.to_owned(),
ManifestEntry {
source_chain: vec![],
checksum: checksum("whatever"),
deployed_at: "2026-07-31T00:00:00Z".to_owned(),
origin,
},
);
manifest.save(dir).unwrap();
}
#[test]
fn identity_prefers_the_frontmatter_name() {
assert_eq!(
agent_identity(&doc("rust-engineer"), "helper.md"),
"rust-engineer"
);
}
#[test]
fn identity_falls_back_to_the_stem() {
assert_eq!(agent_identity("just prose\n", "helper.md"), "helper");
assert_eq!(agent_identity("", "qa.md"), "qa");
}
#[test]
fn identity_ignores_a_blank_name() {
assert_eq!(agent_identity("---\nname: \n---\n", "qa.md"), "qa");
}
#[test]
fn identity_strips_only_one_md_suffix() {
assert_eq!(agent_identity("", "weird.md.md"), "weird.md");
}
#[test]
fn classify_bundled_name_shadows() {
let bundled = roster(&["rust-engineer", "qa"]);
assert_eq!(
classify_tier_resident("rust-engineer", TierOwnership::Untracked, &bundled),
TierResidentClass::ShadowsBundled
);
}
#[test]
fn classify_user_owned_survives_a_bundled_name_collision() {
let bundled = roster(&["qa"]);
assert_eq!(
classify_tier_resident("qa", TierOwnership::UserOwned, &bundled),
TierResidentClass::Custom
);
assert!(!TierResidentClass::Custom.is_tm_owned());
}
#[test]
fn classify_manifest_framework_owned_is_stranded() {
let bundled = roster(&["qa"]);
assert_eq!(
classify_tier_resident("legacy-agent", TierOwnership::FrameworkOwned, &bundled),
TierResidentClass::StrandedFrameworkOwned
);
}
#[test]
fn classify_unknown_name_is_custom() {
let bundled = roster(&["qa"]);
assert_eq!(
classify_tier_resident("acme-internal", TierOwnership::Untracked, &bundled),
TierResidentClass::Custom
);
}
#[test]
fn classify_prefers_shadowing_over_stranded() {
let bundled = roster(&["qa"]);
assert_eq!(
classify_tier_resident("qa", TierOwnership::FrameworkOwned, &bundled),
TierResidentClass::ShadowsBundled
);
}
#[test]
fn tm_owned_is_true_for_exactly_the_two_owned_classes() {
assert!(TierResidentClass::ShadowsBundled.is_tm_owned());
assert!(TierResidentClass::StrandedFrameworkOwned.is_tm_owned());
assert!(!TierResidentClass::Custom.is_tm_owned());
}
#[test]
fn ownership_of_reads_the_manifest_entry() {
let mut manifest = AgentManifest::default();
for (file, origin) in [
("bundled.md", Origin::Bundled),
("mine.md", Origin::User),
("pulled.md", Origin::Registry),
] {
manifest.managed.insert(
file.to_owned(),
ManifestEntry {
source_chain: vec![],
checksum: checksum("x"),
deployed_at: "2026-07-31T00:00:00Z".to_owned(),
origin,
},
);
}
assert_eq!(
ownership_of(&manifest, "bundled.md"),
TierOwnership::FrameworkOwned
);
assert_eq!(ownership_of(&manifest, "mine.md"), TierOwnership::UserOwned);
assert_eq!(
ownership_of(&manifest, "pulled.md"),
TierOwnership::UserOwned
);
assert_eq!(
ownership_of(&manifest, "absent.md"),
TierOwnership::Untracked
);
}
#[test]
fn bundled_names_use_the_frontmatter_name() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("BASE-AGENT.md"), doc("base-agent")).unwrap();
std::fs::write(tmp.path().join("qa.md"), doc("qa")).unwrap();
std::fs::write(tmp.path().join("nameless.md"), "prose only").unwrap();
std::fs::write(tmp.path().join("notes.txt"), "x").unwrap();
assert_eq!(
bundled_agent_names(tmp.path()),
roster(&["base-agent", "qa", "nameless"])
);
}
#[test]
fn bundled_names_missing_dir_is_empty() {
let tmp = tempfile::tempdir().unwrap();
assert!(bundled_agent_names(&tmp.path().join("nope")).is_empty());
}
#[test]
fn audit_reports_a_shadowing_stub() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("rust-engineer.md"), doc("rust-engineer")).unwrap();
let found = audit_agent_tier(tmp.path(), &roster(&["rust-engineer"])).unwrap();
assert_eq!(found.len(), 1, "found: {found:?}");
assert_eq!(found[0].name, "rust-engineer");
assert_eq!(found[0].class, TierResidentClass::ShadowsBundled);
assert_eq!(found[0].path, tmp.path().join("rust-engineer.md"));
}
#[test]
fn audit_flags_a_renamed_file_that_declares_a_bundled_name() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("helper.md"), doc("rust-engineer")).unwrap();
let found = audit_agent_tier(tmp.path(), &roster(&["rust-engineer"])).unwrap();
assert_eq!(found.len(), 1, "found: {found:?}");
assert_eq!(found[0].name, "rust-engineer");
assert_eq!(found[0].path, tmp.path().join("helper.md"));
}
#[test]
fn audit_ignores_a_bundled_filename_that_declares_a_custom_name() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("rust-engineer.md"), doc("acme-custom")).unwrap();
assert!(
audit_agent_tier(tmp.path(), &roster(&["rust-engineer"]))
.unwrap()
.is_empty()
);
}
#[test]
fn audit_ignores_a_custom_agent() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("acme-internal.md"), doc("acme-internal")).unwrap();
std::fs::write(tmp.path().join("qa.md"), doc("qa")).unwrap();
let found = audit_agent_tier(tmp.path(), &roster(&["qa"])).unwrap();
assert_eq!(found.len(), 1, "found: {found:?}");
assert_eq!(found[0].name, "qa");
}
#[test]
fn audit_ignores_a_user_owned_entry_on_a_bundled_name() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("qa.md"), doc("qa")).unwrap();
track(tmp.path(), "qa.md", Origin::User);
assert!(
audit_agent_tier(tmp.path(), &roster(&["qa"]))
.unwrap()
.is_empty()
);
}
#[test]
fn audit_reports_a_stranded_framework_entry() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("legacy-agent.md"), doc("legacy-agent")).unwrap();
track(tmp.path(), "legacy-agent.md", Origin::Bundled);
let found = audit_agent_tier(tmp.path(), &roster(&["qa"])).unwrap();
assert_eq!(found.len(), 1, "found: {found:?}");
assert_eq!(found[0].class, TierResidentClass::StrandedFrameworkOwned);
}
#[test]
fn audit_ignores_a_user_owned_manifest_entry() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("mine.md"), doc("mine")).unwrap();
track(tmp.path(), "mine.md", Origin::User);
assert!(
audit_agent_tier(tmp.path(), &roster(&["qa"]))
.unwrap()
.is_empty()
);
}
#[test]
fn audit_missing_dir_is_empty() {
let tmp = tempfile::tempdir().unwrap();
assert!(
audit_agent_tier(&tmp.path().join("nope"), &roster(&["qa"]))
.expect("an absent tier is not an error")
.is_empty()
);
}
#[test]
#[cfg(unix)]
fn audit_unscannable_dir_is_an_error() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
let tier = tmp.path().join("agents");
std::fs::create_dir(&tier).unwrap();
std::fs::write(tier.join("qa.md"), doc("qa")).unwrap();
std::fs::set_permissions(&tier, std::fs::Permissions::from_mode(0o000)).unwrap();
let result = audit_agent_tier(&tier, &roster(&["qa"]));
std::fs::set_permissions(&tier, std::fs::Permissions::from_mode(0o755)).unwrap();
let err = result.expect_err("an unscannable tier must not report an empty scan");
let TierAuditError::Unscannable { path, source } = &err;
assert_eq!(path, &tier);
assert_eq!(source.kind(), std::io::ErrorKind::PermissionDenied);
}
#[test]
fn audit_tolerates_a_corrupt_manifest() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("qa.md"), doc("qa")).unwrap();
std::fs::write(
tmp.path().join(crate::agents::manifest::MANIFEST_FILE),
"{ not json",
)
.unwrap();
let found = audit_agent_tier(tmp.path(), &roster(&["qa"])).unwrap();
assert_eq!(found.len(), 1, "found: {found:?}");
assert_eq!(found[0].class, TierResidentClass::ShadowsBundled);
}
#[test]
fn audit_is_sorted_by_name() {
let tmp = tempfile::tempdir().unwrap();
for name in ["qa", "engineer", "rust-engineer"] {
std::fs::write(tmp.path().join(format!("{name}.md")), doc(name)).unwrap();
}
let found =
audit_agent_tier(tmp.path(), &roster(&["qa", "engineer", "rust-engineer"])).unwrap();
let names: Vec<&str> = found.iter().map(|f| f.name.as_str()).collect();
assert_eq!(names, vec!["engineer", "qa", "rust-engineer"]);
}
fn staged(dir: &std::path::Path, file_name: &str, declared: Option<&str>, origin: Origin) {
let line = declared
.map(|d| format!("provenance: {d}\n"))
.unwrap_or_default();
std::fs::write(
dir.join(file_name),
format!("---\nname: qa\nrole: qa\n{line}---\n\nBODY\n"),
)
.unwrap();
track(dir, file_name, origin);
}
#[test]
fn audit_provenance_reports_a_contradicting_declaration() {
let tmp = tempfile::tempdir().unwrap();
staged(tmp.path(), "qa.md", Some("user-authored"), Origin::Bundled);
let found = audit_provenance(tmp.path()).unwrap();
assert_eq!(found.len(), 1, "one disagreement: {found:?}");
assert_eq!(found[0].path, tmp.path().join("qa.md"));
assert_eq!(found[0].declared, Provenance::UserAuthored);
assert!(
found[0].ledger_framework_owned,
"the ledger's value travels too"
);
assert!(
found[0].detail.contains("qa.md"),
"names the file: {}",
found[0].detail
);
assert!(
found[0].detail.contains("user-authored"),
"names the declaration: {}",
found[0].detail
);
assert!(
found[0].detail.contains("framework-owned"),
"names the ledger's record: {}",
found[0].detail
);
}
#[test]
fn audit_provenance_reports_the_mirrored_contradiction() {
let tmp = tempfile::tempdir().unwrap();
staged(tmp.path(), "mine.md", Some("framework-owned"), Origin::User);
let found = audit_provenance(tmp.path()).unwrap();
assert_eq!(found.len(), 1, "{found:?}");
assert_eq!(found[0].declared, Provenance::FrameworkOwned);
assert!(!found[0].ledger_framework_owned);
}
#[test]
fn audit_provenance_is_silent_when_records_agree() {
let tmp = tempfile::tempdir().unwrap();
staged(
tmp.path(),
"qa.md",
Some("framework-owned"),
Origin::Bundled,
);
assert!(audit_provenance(tmp.path()).unwrap().is_empty());
}
#[test]
fn audit_provenance_ignores_an_untracked_file() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("qa.md"),
"---\nname: qa\nprovenance: framework-owned\n---\n\nBODY\n",
)
.unwrap();
assert!(audit_provenance(tmp.path()).unwrap().is_empty());
}
#[test]
fn audit_provenance_ignores_a_file_declaring_nothing() {
let tmp = tempfile::tempdir().unwrap();
staged(tmp.path(), "qa.md", None, Origin::Bundled);
assert!(audit_provenance(tmp.path()).unwrap().is_empty());
}
#[test]
fn audit_provenance_missing_dir_is_empty() {
let tmp = tempfile::tempdir().unwrap();
let absent = tmp.path().join("nope");
assert!(audit_provenance(&absent).unwrap().is_empty());
}
#[test]
#[cfg(unix)]
fn audit_provenance_unscannable_dir_is_an_error() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
let locked = tmp.path().join("locked");
std::fs::create_dir(&locked).unwrap();
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap();
let result = audit_provenance(&locked);
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(
matches!(result, Err(TierAuditError::Unscannable { .. })),
"{result:?}"
);
}
#[test]
fn audit_verdict_is_unchanged_by_a_contradicting_declaration() {
let tmp = tempfile::tempdir().unwrap();
staged(tmp.path(), "qa.md", Some("user-authored"), Origin::Bundled);
let bundled: BTreeSet<String> = ["qa".to_string()].into_iter().collect();
let found = audit_agent_tier(tmp.path(), &bundled).unwrap();
assert_eq!(found.len(), 1, "still reported: {found:?}");
assert_eq!(found[0].class, TierResidentClass::ShadowsBundled);
}