use super::*;
use crate::paths::TRUSTY_CODE_DIRNAME;
fn write_claude(root: &Path, relative: &str, body: &str) -> PathBuf {
let path = root.join(CLAUDE_COMPAT_DIRNAME).join(relative);
std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
std::fs::write(&path, body).expect("write");
path
}
fn native_tree(root: &Path) -> Vec<PathBuf> {
fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let p = entry.path();
if p.is_dir() {
walk(&p, out);
} else {
out.push(p);
}
}
}
let mut out = Vec::new();
walk(&root.join(TRUSTY_CODE_DIRNAME), &mut out);
out.sort();
out
}
#[test]
fn plan_is_sorted_and_deterministic() {
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(tmp.path(), "agents/pm.md", "# pm");
write_claude(tmp.path(), "agents/engineer.md", "# engineer");
write_claude(tmp.path(), "skills/demo/SKILL.md", "# demo");
write_claude(tmp.path(), "skills/demo/references/deep.md", "# deep");
write_claude(
tmp.path(),
"settings.json",
r#"{"code_harness":{"mode":"parity"}}"#,
);
let plan = plan_import(tmp.path());
let again = plan_import(tmp.path());
assert_eq!(plan, again, "the plan must be a function of the tree alone");
assert_eq!(plan.entries.len(), 5);
assert!(
plan.entries.windows(2).all(|w| w[0].to <= w[1].to),
"entries must be sorted by target path"
);
assert_eq!(plan.to_copy().count(), 5);
assert!(
plan.entries
.iter()
.all(|e| e.to.starts_with(tmp.path().join(TRUSTY_CODE_DIRNAME))),
"every target must land beneath .trusty-code/"
);
}
#[test]
fn missing_claude_dir_yields_empty_plan() {
let tmp = tempfile::tempdir().expect("tempdir");
assert_eq!(plan_import(tmp.path()), ImportPlan::default());
}
#[test]
fn apply_creates_only_the_planned_files() {
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(tmp.path(), "agents/pm.md", "# pm");
write_claude(tmp.path(), "skills/demo/SKILL.md", "# demo");
let settings = r#"{"code_harness":{"mode":"parity"}}"#;
write_claude(tmp.path(), crate::paths::SETTINGS_FILENAME, settings);
let plan = plan_import(tmp.path());
let report = apply_import(tmp.path(), &plan);
assert!(report.refused.is_empty(), "refused: {:?}", report.refused);
assert_eq!(report.created, native_tree(tmp.path()));
assert_eq!(report.created.len(), 3);
assert_eq!(
std::fs::read_to_string(tmp.path().join(TRUSTY_CODE_DIRNAME).join("agents/pm.md"))
.expect("read"),
"# pm"
);
assert_eq!(
std::fs::read_to_string(
tmp.path()
.join(TRUSTY_CODE_DIRNAME)
.join(crate::paths::SETTINGS_FILENAME)
)
.expect("read the imported settings"),
settings
);
}
#[test]
fn apply_is_idempotent() {
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(tmp.path(), "agents/pm.md", "# pm");
apply_import(tmp.path(), &plan_import(tmp.path()));
let target = tmp.path().join(TRUSTY_CODE_DIRNAME).join("agents/pm.md");
std::fs::write(&target, "# edited by the project owner").expect("write");
let second = plan_import(tmp.path());
let report = apply_import(tmp.path(), &second);
assert_eq!(second.to_copy().count(), 0);
assert!(report.created.is_empty());
assert_eq!(report.refused.len(), 1);
assert!(report.refused[0].1.contains("already exists"));
assert_eq!(
std::fs::read_to_string(&target).expect("read"),
"# edited by the project owner"
);
}
#[test]
fn existing_target_is_never_overwritten() {
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(tmp.path(), "agents/pm.md", "# from claude");
let target = tmp.path().join(TRUSTY_CODE_DIRNAME).join("agents/pm.md");
std::fs::create_dir_all(target.parent().expect("parent")).expect("mkdir");
std::fs::write(&target, "# authored natively").expect("write");
let plan = plan_import(tmp.path());
assert_eq!(plan.entries.len(), 1);
assert!(matches!(plan.entries[0].action, ImportAction::Refuse(_)));
assert_eq!(
std::fs::read_to_string(&target).expect("read"),
"# authored natively"
);
}
#[cfg(unix)]
#[test]
fn executable_source_is_refused() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().expect("tempdir");
let src = write_claude(tmp.path(), "agents/hook.md", "#!/bin/sh\nrm -rf /\n");
std::fs::set_permissions(&src, std::fs::Permissions::from_mode(0o755)).expect("chmod");
let plan = plan_import(tmp.path());
let report = apply_import(tmp.path(), &plan);
assert_eq!(plan.to_copy().count(), 0);
assert!(report.created.is_empty());
assert_eq!(report.refused.len(), 1);
assert!(
report.refused[0].1.contains("executable"),
"reason was: {}",
report.refused[0].1
);
}
#[cfg(unix)]
#[test]
fn symlink_escaping_claude_is_refused() {
let tmp = tempfile::tempdir().expect("tempdir");
let outside = tempfile::tempdir().expect("tempdir");
let secret = outside.path().join("id_rsa");
std::fs::write(&secret, "PRIVATE KEY").expect("write");
let agents = tmp.path().join(CLAUDE_COMPAT_DIRNAME).join("agents");
std::fs::create_dir_all(&agents).expect("mkdir");
std::os::unix::fs::symlink(&secret, agents.join("leak.md")).expect("symlink");
let plan = plan_import(tmp.path());
let report = apply_import(tmp.path(), &plan);
assert_eq!(plan.entries.len(), 1);
assert_eq!(plan.to_copy().count(), 0);
assert!(report.created.is_empty());
assert!(
report.refused[0].1.contains("symlink"),
"reason was: {}",
report.refused[0].1
);
assert!(
!tmp.path()
.join(TRUSTY_CODE_DIRNAME)
.join("agents/leak.md")
.exists()
);
}
#[test]
fn secret_bearing_settings_is_refused() {
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(
tmp.path(),
"settings.json",
r#"{"env":{"OPENROUTER_API_KEY":"sk-live-xyz"}}"#,
);
let plan = plan_import(tmp.path());
let report = apply_import(tmp.path(), &plan);
assert!(report.created.is_empty());
assert_eq!(report.refused.len(), 1);
assert!(
report.refused[0].1.contains("OPENROUTER_API_KEY"),
"reason was: {}",
report.refused[0].1
);
assert!(
!tmp.path()
.join(TRUSTY_CODE_DIRNAME)
.join("settings.json")
.exists()
);
}
#[test]
fn unparseable_settings_is_refused() {
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(tmp.path(), "settings.json", "{ not json");
let plan = plan_import(tmp.path());
assert_eq!(plan.to_copy().count(), 0);
assert!(matches!(plan.entries[0].action, ImportAction::Refuse(_)));
}
#[cfg(unix)]
#[test]
fn unreadable_subtree_is_skipped_with_a_warning() {
use std::os::unix::fs::PermissionsExt;
if unsafe { libc::geteuid() } == 0 {
return;
}
let tmp = tempfile::tempdir().expect("tempdir");
write_claude(tmp.path(), "agents/pm.md", "# pm");
let skills = write_claude(tmp.path(), "skills/demo/SKILL.md", "# demo")
.parent()
.expect("parent")
.parent()
.expect("skills")
.to_path_buf();
std::fs::set_permissions(&skills, std::fs::Permissions::from_mode(0o000)).expect("chmod");
let plan = plan_import(tmp.path());
std::fs::set_permissions(&skills, std::fs::Permissions::from_mode(0o755)).expect("restore");
assert_eq!(plan.to_copy().count(), 1, "the readable agent must survive");
assert!(plan.entries[0].to.ends_with("agents/pm.md"));
}
#[cfg(unix)]
#[test]
fn swapped_target_dir_never_reaches_the_victim() {
let tmp = tempfile::tempdir().expect("tempdir");
let victim = tempfile::tempdir().expect("victim");
write_claude(tmp.path(), "agents/pm.md", "AGENT BODY");
let plan = plan_import(tmp.path());
assert_eq!(plan.to_copy().count(), 1, "the agent must be planned");
let agents = tmp.path().join(TRUSTY_CODE_DIRNAME).join("agents");
std::fs::create_dir_all(&agents).expect("mkdir");
std::fs::remove_dir_all(&agents).expect("remove");
std::os::unix::fs::symlink(victim.path(), &agents).expect("symlink");
let report = apply_import(tmp.path(), &plan);
assert!(
report.created.is_empty(),
"nothing may be reported created: {:?}",
report.created
);
assert_eq!(report.refused.len(), 1, "the swap must be reported");
let leaked: Vec<_> = std::fs::read_dir(victim.path())
.expect("read the victim")
.flatten()
.map(|e| e.file_name())
.collect();
assert!(
leaked.is_empty(),
"the victim must receive nothing, found {leaked:?}"
);
}