use super::*;
#[test]
fn redact_url_with_credentials() {
assert_eq!(
redact_url("https://user:token@github.com/acme/repo.git"),
"https://***@github.com/acme/repo.git"
);
}
#[test]
fn redact_url_with_token_only() {
assert_eq!(
redact_url("https://ghp_abc123@github.com/acme/repo.git"),
"https://***@github.com/acme/repo.git"
);
}
#[test]
fn redact_url_no_credentials_unchanged() {
assert_eq!(
redact_url("https://github.com/acme/repo.git"),
"https://github.com/acme/repo.git"
);
}
#[test]
fn redact_url_not_a_url_unchanged() {
assert_eq!(redact_url("just-a-string"), "just-a-string");
}
#[test]
fn redact_url_ssh_no_scheme_unchanged() {
assert_eq!(
redact_url("git@github.com:acme/repo.git"),
"git@github.com:acme/repo.git"
);
}
#[test]
fn auto_msg_single_skill() {
let porcelain = " M skills/my-tool/SKILL.md\n";
assert_eq!(build_auto_commit_message(porcelain), "auto backup: my-tool");
}
#[test]
fn auto_msg_multiple_skills_sorted() {
let porcelain = " M skills/zeta/SKILL.md\n M skills/alpha/notes.md\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: alpha, zeta"
);
}
#[test]
fn auto_msg_skill_plus_metadata() {
let porcelain = " M skills/demo/SKILL.md\n M .agent-hub/state.json\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: demo, metadata"
);
}
#[test]
fn auto_msg_automation_uses_task_name_instead_of_metadata() {
let porcelain = " M automations/daily-review/config/task.json\n M automations/daily-review/scripts/run.sh\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: automation/daily-review"
);
}
#[test]
fn auto_msg_automation_plus_metadata() {
let porcelain = " M automations/daily-review/config/task.json\n M .agent-hub/state.json\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: automation/daily-review, metadata"
);
}
#[test]
fn auto_msg_overflow_truncates() {
let porcelain = (1..=7)
.map(|i| format!(" M skills/s{i}/SKILL.md"))
.collect::<Vec<_>>()
.join("\n");
assert_eq!(
build_auto_commit_message(&porcelain),
"auto backup: s1, s2, s3, s4, s5 (+2 more)"
);
}
#[test]
fn auto_msg_metadata_only() {
let porcelain = " M .agent-hub/state.json\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: metadata"
);
}
#[test]
fn auto_msg_empty_porcelain_fallback() {
assert_eq!(build_auto_commit_message(""), "auto backup");
}
#[test]
fn auto_msg_added_and_deleted() {
let porcelain = "?? skills/new-one/SKILL.md\n D skills/old/SKILL.md\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: new-one, old"
);
}
#[test]
fn auto_msg_rename_arrow() {
let porcelain = "R skills/old-name/SKILL.md -> skills/new-name/SKILL.md\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: new-name"
);
}
#[test]
fn auto_msg_dot_prefix_skill_excluded() {
let porcelain = " M skills/.hidden/data.json\n M skills/visible/SKILL.md\n";
assert_eq!(
build_auto_commit_message(porcelain),
"auto backup: visible, metadata"
);
}
#[test]
fn redact_urls_in_text_mixed_content() {
let input = "failed to push to https://user:pass@github.com/repo.git (error)";
let result = redact_urls_in_text(input);
assert!(result.contains("***@github.com/repo.git"));
assert!(!result.contains("user:pass"));
}
#[test]
fn redact_urls_in_text_no_urls() {
assert_eq!(redact_urls_in_text("plain text here"), "plain text here");
}
#[test]
fn sanitize_device_name_strips_ident_breakers_and_collapses_whitespace() {
assert_eq!(sanitize_device_name(" MacBook Pro "), "MacBook Pro");
assert_eq!(sanitize_device_name("evil<\n>name"), "evilname");
assert_eq!(sanitize_device_name("公司 Windows"), "公司 Windows");
assert_eq!(sanitize_device_name("<>"), "");
assert_eq!(
sanitize_device_name("a".repeat(100).as_str())
.chars()
.count(),
64
);
}
#[test]
fn device_email_slugs_name_with_fallback() {
assert_eq!(device_email("MacBook Pro"), "macbook-pro@agent-hub.local");
assert_eq!(device_email("公司 Windows"), "windows@agent-hub.local");
assert_eq!(device_email("公司"), "device@agent-hub.local");
}
#[test]
fn default_device_name_is_never_empty() {
assert!(!default_device_name().is_empty());
}
#[test]
fn ensure_gitignore_appends_missing_entries_and_leaves_user_lines_alone() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let gitignore = dir.join(".gitignore");
std::fs::write(&gitignore, "# my own notes\nsecret-notes/\n*.tmp\n").unwrap();
ensure_gitignore(dir).unwrap();
let lines: Vec<String> = std::fs::read_to_string(&gitignore)
.unwrap()
.lines()
.map(ToOwned::to_owned)
.collect();
assert_eq!(&lines[..3], &["# my own notes", "secret-notes/", "*.tmp"]);
assert_eq!(lines.iter().filter(|l| *l == "*.tmp").count(), 1);
for required in [
".DS_Store",
"Thumbs.db",
".agent-hub.lock",
"__pycache__/",
"*.pyc",
] {
assert!(lines.iter().any(|l| l == required), "missing {required}");
}
let before = std::fs::read_to_string(&gitignore).unwrap();
ensure_gitignore(dir).unwrap();
assert_eq!(std::fs::read_to_string(&gitignore).unwrap(), before);
}
#[test]
fn compiled_python_artifacts_already_committed_are_untracked() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
init_repo_unlocked(dir, "Test Device").unwrap();
let cache = dir.join("docx/scripts/__pycache__");
std::fs::create_dir_all(&cache).unwrap();
std::fs::write(cache.join("helpers.cpython-311.pyc"), b"\x00bytecode").unwrap();
std::fs::write(dir.join("docx/scripts/helpers.py"), "print('hi')").unwrap();
std::fs::write(dir.join("docx/scripts/_native.pyd"), b"\x00ext").unwrap();
run_git_checked(dir, &["add", "-A", "--force"]).unwrap();
run_git_checked(dir, &["commit", "-m", "before the ignore rule"]).unwrap();
assert!(is_tracked(
dir,
"docx/scripts/__pycache__/helpers.cpython-311.pyc"
));
commit_all_unlocked(dir, "backup").unwrap();
assert!(
!is_tracked(dir, "docx/scripts/__pycache__/helpers.cpython-311.pyc"),
"compiled artifacts must stop being tracked"
);
assert!(cache.join("helpers.cpython-311.pyc").is_file());
assert!(is_tracked(dir, "docx/scripts/helpers.py"));
assert!(is_tracked(dir, "docx/scripts/_native.pyd"));
std::fs::write(dir.join("note.md"), "x").unwrap();
commit_all_unlocked(dir, "backup again").unwrap();
assert!(is_tracked(dir, "docx/scripts/helpers.py"));
assert!(is_tracked(dir, "docx/scripts/_native.pyd"));
}
#[test]
fn init_repo_commits_as_device_and_identity_follows_rename() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
init_repo_unlocked(dir, "MacBook Pro").unwrap();
assert_eq!(
run_git(dir, &["log", "-1", "--format=%an"]).unwrap(),
"MacBook Pro"
);
assert_eq!(
run_git(dir, &["config", "--local", "--get", "user.email"]).unwrap(),
"macbook-pro@agent-hub.local"
);
configure_device_identity(dir, "公司 Windows").unwrap();
std::fs::write(dir.join("note.md"), "x").unwrap();
commit_all_unlocked(dir, "backup").unwrap();
assert_eq!(
run_git(dir, &["log", "-1", "--format=%an"]).unwrap(),
"公司 Windows"
);
let tag = create_snapshot_tag_unlocked(dir).unwrap();
let versions = list_snapshot_versions(dir, None).unwrap();
let entry = versions.iter().find(|v| v.tag == tag).unwrap();
assert_eq!(entry.author, "公司 Windows");
assert_eq!(entry.message, "backup");
assert!(!entry.committed_at.is_empty());
}
#[test]
fn configure_device_identity_noop_without_repo_or_name() {
let tmp = tempfile::tempdir().unwrap();
configure_device_identity(tmp.path(), "MacBook Pro").unwrap();
assert!(!tmp.path().join(".git").exists());
init_repo_unlocked(tmp.path(), "Device A").unwrap();
configure_device_identity(tmp.path(), " ").unwrap();
assert_eq!(
run_git(tmp.path(), &["config", "--local", "--get", "user.name"]).unwrap(),
"Device A"
);
}
#[test]
fn oversized_untracked_skill_is_excluded_but_tracked_one_is_not() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
init_repo_unlocked(dir, "Device A").unwrap();
std::fs::create_dir_all(dir.join("grown")).unwrap();
std::fs::write(dir.join("grown/SKILL.md"), "small at first").unwrap();
commit_all_unlocked(dir, "seed").unwrap();
std::fs::write(dir.join("grown/data.bin"), vec![0u8; 64]).unwrap();
std::fs::create_dir_all(dir.join("huge")).unwrap();
std::fs::write(dir.join("huge/SKILL.md"), vec![b'x'; 64]).unwrap();
let meta_dir = dir.join(".agent-hub/skills");
std::fs::create_dir_all(&meta_dir).unwrap();
std::fs::write(
meta_dir.join("skill-huge.json"),
br#"{"schema_version":1,"skill_id":"skill-huge","path":"huge","path_key":"huge","enabled":true,"tags":[],"source":{"type":"import","ref":null,"subpath":null,"branch":null}}"#,
)
.unwrap();
let excluded = apply_oversized_exclusions(dir, 32).unwrap();
assert_eq!(excluded, vec!["huge"]);
let gitignore = std::fs::read_to_string(dir.join(".gitignore")).unwrap();
assert!(gitignore.contains("/huge/"), "{gitignore}");
assert!(
gitignore.contains("/.agent-hub/skills/skill-huge.json"),
"{gitignore}"
);
assert!(
!gitignore.contains("/grown/"),
"tracked skill must not be excluded: {gitignore}"
);
std::fs::write(dir.join("note.md"), "trigger commit").unwrap();
apply_oversized_exclusions(dir, 32).unwrap();
run_git_checked(dir, &["add", "-A"]).unwrap();
run_git_checked(dir, &["commit", "-m", "test"]).unwrap();
assert!(run_git(dir, &["cat-file", "-e", "HEAD:huge/SKILL.md"]).is_err());
assert!(run_git(
dir,
&["cat-file", "-e", "HEAD:.agent-hub/skills/skill-huge.json"]
)
.is_err());
run_git(dir, &["cat-file", "-e", "HEAD:grown/data.bin"]).unwrap();
assert!(dir.join("huge/SKILL.md").exists());
apply_oversized_exclusions(dir, 32).unwrap();
let same = std::fs::read_to_string(dir.join(".gitignore")).unwrap();
assert_eq!(gitignore, same);
std::fs::write(dir.join("huge/SKILL.md"), "tiny").unwrap();
apply_oversized_exclusions(dir, 32).unwrap();
let after = std::fs::read_to_string(dir.join(".gitignore")).unwrap();
assert!(
!after.contains("/huge/"),
"shrunk skill re-enters backup: {after}"
);
assert!(
!after.contains("oversized"),
"empty section is removed: {after}"
);
}
#[test]
fn size_report_marks_untracked_oversized_as_excluded() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
init_repo_unlocked(dir, "Device A").unwrap();
std::fs::create_dir_all(dir.join("big")).unwrap();
std::fs::write(dir.join("big/SKILL.md"), vec![b'x'; 200]).unwrap();
assert!(!is_tracked(dir, "big"));
run_git_checked(dir, &["add", "-A"]).unwrap();
run_git_checked(dir, &["commit", "-m", "track big"]).unwrap();
assert!(is_tracked(dir, "big"));
}
#[test]
fn app_commits_carry_protocol_marker_and_trailer() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
init_repo_unlocked(dir, "Device A").unwrap();
let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
assert!(protocol::has_protocol_trailer(&body), "init: {body}");
run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();
let snapshot_tag = create_snapshot_tag_unlocked(dir).unwrap();
std::fs::write(dir.join("note.md"), "x").unwrap();
commit_all_unlocked(dir, "backup").unwrap();
let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
assert!(protocol::has_protocol_trailer(&body), "commit_all: {body}");
let safety = restore_snapshot_version_unlocked(dir, &snapshot_tag).unwrap();
assert!(safety.starts_with("sm-v-"));
let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
assert!(protocol::has_protocol_trailer(&body), "restore: {body}");
run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();
}
#[test]
fn restore_of_pre_protocol_snapshot_self_heals_marker() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let git = |args: &[&str]| {
let out = Command::new("git")
.arg("-C")
.arg(dir)
.args(["-c", "user.email=t@e.c", "-c", "user.name=T"])
.args(args)
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
};
git(&["init", "-b", "main"]);
git(&["config", "user.email", "t@e.c"]);
git(&["config", "user.name", "T"]);
std::fs::create_dir_all(dir.join("skill-a")).unwrap();
std::fs::write(dir.join("skill-a/SKILL.md"), "v1").unwrap();
git(&["add", "-A"]);
git(&["commit", "-m", "pre-protocol"]);
let old_tag = create_snapshot_tag_unlocked(dir).unwrap();
std::fs::write(dir.join("skill-a/SKILL.md"), "v2").unwrap();
commit_all_unlocked(dir, "backup").unwrap();
run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();
restore_snapshot_version_unlocked(dir, &old_tag).unwrap();
assert_eq!(
std::fs::read_to_string(dir.join("skill-a/SKILL.md")).unwrap(),
"v1"
);
run_git(dir, &["cat-file", "-e", "HEAD:.agent-hub/protocol.json"]).unwrap();
let body = run_git(dir, &["log", "-1", "--format=%B"]).unwrap();
assert!(protocol::has_protocol_trailer(&body), "restore: {body}");
}
#[test]
fn parse_restored_tag_valid() {
let msg = "restore: switch skills library to sm-v-20260318-153012-abc1234";
assert_eq!(
parse_restored_from_tag_message(msg).as_deref(),
Some("sm-v-20260318-153012-abc1234")
);
}
#[test]
fn parse_restored_tag_invalid_prefix() {
assert_eq!(
parse_restored_from_tag_message("some other commit message"),
None
);
}
#[test]
fn parse_restored_tag_non_snapshot_tag() {
let msg = "restore: switch skills library to v1.0.0";
assert_eq!(parse_restored_from_tag_message(msg), None);
}
#[test]
fn clone_into_unlocked_failure_includes_git_stderr() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("clone-target");
let bogus_src = tmp.path().join("does-not-exist.git");
let url = format!("file://{}", bogus_src.display());
let err = clone_into_unlocked(&target, &url).unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains("git clone failed"),
"expected git stderr to be surfaced, got: {msg}"
);
assert!(
!msg.eq("Failed to clone repository"),
"error must not be the old generic placeholder"
);
}
#[test]
fn clone_into_unlocked_failure_redacts_credentials_in_url() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("clone-target");
let url = "https://ghp_supersecrettoken123@127.0.0.1:1/does-not-exist.git";
let err = clone_into_unlocked(&target, url).unwrap_err();
let msg = format!("{err:#}");
assert!(
!msg.contains("ghp_supersecrettoken123"),
"credential must not leak into error message: {msg}"
);
}
#[test]
fn parse_restored_tag_with_trailing_whitespace() {
let msg = "restore: switch skills library to sm-v-20260318-153012-abc1234 ";
assert_eq!(
parse_restored_from_tag_message(msg).as_deref(),
Some("sm-v-20260318-153012-abc1234")
);
}
#[test]
fn status_reports_in_sync_after_backup_cycle_and_restart() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote.git");
let work = tmp.path().join("work");
std::fs::create_dir_all(&work).unwrap();
assert!(Command::new("git")
.args(["init", "--bare", "--initial-branch=main"])
.arg(&remote)
.output()
.unwrap()
.status
.success());
let git = |args: &[&str]| {
let out = Command::new("git")
.arg("-C")
.arg(&work)
.args(args)
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
};
git(&["init", "-b", "main"]);
git(&["config", "user.email", "test@example.com"]);
git(&["config", "user.name", "Test"]);
git(&["config", "push.autoSetupRemote", "false"]);
std::fs::create_dir_all(work.join("skill-a")).unwrap();
std::fs::write(work.join("skill-a/SKILL.md"), "content").unwrap();
commit_all_unlocked(&work, "backup").unwrap();
set_remote_unlocked(&work, remote.to_str().unwrap()).unwrap();
let tag = create_snapshot_tag_unlocked(&work).unwrap();
push_unlocked(&work).unwrap();
fetch_remote(&work).unwrap();
let status = get_status(&work).unwrap();
assert!(status.is_repo);
assert_eq!(status.upstream_health, "healthy");
assert!(!status.has_changes, "no pending changes after clean sync");
assert_eq!(status.changed_skill_count, 0);
assert_eq!((status.ahead, status.behind), (0, 0));
assert_eq!(status.current_snapshot_tag.as_deref(), Some(tag.as_str()));
}
#[test]
fn a_reftable_library_is_refused_with_a_migration_hint() {
let tmp = tempfile::tempdir().unwrap();
let out = Command::new("git")
.args(["init", "--ref-format=reftable"])
.arg(tmp.path())
.output()
.unwrap();
if !out.status.success() {
eprintln!("skipping: this git cannot create reftable repositories");
return;
}
let err = ensure_repo(tmp.path()).unwrap_err().to_string();
assert!(err.contains("refs migrate --ref-format=files"), "{err}");
assert!(ensure_no_interrupted_git_operation(tmp.path()).is_err());
}
#[test]
#[ignore = "mutates GIT_DEFAULT_REF_FORMAT; run alone"]
fn init_creates_a_files_repo_even_when_git_defaults_to_reftable() {
let tmp = tempfile::tempdir().unwrap();
std::env::set_var("GIT_DEFAULT_REF_FORMAT", "reftable");
let result = init_repo_unlocked(tmp.path(), "test-device");
std::env::remove_var("GIT_DEFAULT_REF_FORMAT");
result.unwrap();
assert!(!tmp.path().join(".git/reftable").exists());
ensure_repo(tmp.path()).unwrap();
}
#[test]
fn restore_creates_safety_point_capturing_dirty_tree() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
let git = |args: &[&str]| {
let out = Command::new("git")
.arg("-C")
.arg(dir)
.args(args)
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
};
git(&["init", "-b", "main"]);
git(&["config", "user.email", "test@example.com"]);
git(&["config", "user.name", "Test"]);
std::fs::create_dir_all(dir.join("skill-a")).unwrap();
std::fs::write(dir.join("skill-a/SKILL.md"), "v1").unwrap();
git(&["add", "-A"]);
git(&["commit", "-m", "v1"]);
let old_tag = create_snapshot_tag_unlocked(dir).unwrap();
std::fs::write(dir.join("skill-a/SKILL.md"), "v2").unwrap();
git(&["add", "-A"]);
git(&["commit", "-m", "v2"]);
std::fs::write(dir.join("skill-a/SKILL.md"), "v3-dirty").unwrap();
let safety_tag = restore_snapshot_version_unlocked(dir, &old_tag).unwrap();
assert_eq!(
std::fs::read_to_string(dir.join("skill-a/SKILL.md")).unwrap(),
"v1"
);
assert!(safety_tag.starts_with("sm-v-"));
let captured = run_git(dir, &["show", &format!("{safety_tag}:skill-a/SKILL.md")]).unwrap();
assert_eq!(captured, "v3-dirty");
let versions = list_snapshot_versions(dir, None).unwrap();
assert!(versions.iter().any(|v| v.tag == safety_tag));
}
#[test]
fn ensure_clean_clone_target_allows_nonexistent_path() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("not-yet");
ensure_clean_clone_target(&target).unwrap();
}
#[test]
fn ensure_clean_clone_target_allows_empty_directory() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("empty");
std::fs::create_dir_all(&target).unwrap();
ensure_clean_clone_target(&target).unwrap();
}
#[test]
fn ensure_clean_clone_target_allows_existing_git_repo() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("existing-repo");
std::fs::create_dir_all(target.join(".git")).unwrap();
std::fs::write(target.join("README"), b"x").unwrap();
ensure_clean_clone_target(&target).unwrap();
}
#[test]
fn ensure_clean_clone_target_refuses_non_empty_non_git() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("populated");
std::fs::create_dir_all(&target).unwrap();
std::fs::write(target.join("user-file.txt"), b"important").unwrap();
let err = ensure_clean_clone_target(&target).unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains("non-empty") && msg.contains("not a git repo"),
"unexpected message: {msg}"
);
assert!(target.join("user-file.txt").exists());
}
#[test]
fn remove_remote_deletes_origin_and_is_idempotent() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path();
assert!(Command::new("git")
.arg("-C")
.arg(dir)
.arg("init")
.output()
.unwrap()
.status
.success());
run_git_checked(
dir,
&[
"remote",
"add",
"origin",
"https://github.com/acme/repo.git",
],
)
.unwrap();
remove_remote_unlocked(dir).unwrap();
assert!(run_git(dir, &["remote", "get-url", "origin"]).is_err());
remove_remote_unlocked(dir).unwrap();
}
#[test]
fn remove_remote_on_non_repo_is_ok() {
let tmp = tempfile::tempdir().unwrap();
remove_remote_unlocked(tmp.path()).unwrap();
}
#[test]
fn push_first_time_to_empty_remote_with_no_upstream() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote.git");
let work = tmp.path().join("work");
std::fs::create_dir_all(&work).unwrap();
assert!(Command::new("git")
.args(["init", "--bare"])
.arg(&remote)
.output()
.unwrap()
.status
.success());
let git = |args: &[&str]| {
Command::new("git")
.arg("-C")
.arg(&work)
.args(["-c", "user.email=test@example.com", "-c", "user.name=Test"])
.args(args)
.output()
.unwrap()
};
assert!(git(&["init"]).status.success());
assert!(git(&["checkout", "-b", "main"]).status.success());
assert!(git(&["config", "push.autoSetupRemote", "false"])
.status
.success());
std::fs::write(work.join("a.txt"), "hello").unwrap();
assert!(git(&["add", "-A"]).status.success());
assert!(git(&["commit", "-m", "initial"]).status.success());
set_remote_unlocked(&work, remote.to_str().unwrap()).unwrap();
assert_eq!(detect_upstream_health(&work, true), "no_upstream");
assert_eq!(get_ahead_behind(&work).unwrap_or((0, 0)), (0, 0));
push_unlocked(&work).unwrap();
let remote_main = Command::new("git")
.arg("-C")
.arg(&remote)
.args(["rev-parse", "main"])
.output()
.unwrap();
assert!(
remote_main.status.success(),
"remote should have branch main after first push"
);
assert_eq!(detect_upstream_health(&work, true), "healthy");
}
#[test]
fn prune_hidden_refs_removes_remote_copies_and_keeps_local_refs() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote.git");
let work = tmp.path().join("work");
std::fs::create_dir_all(&work).unwrap();
assert!(Command::new("git")
.args(["init", "--bare", "--initial-branch=main"])
.arg(&remote)
.output()
.unwrap()
.status
.success());
init_repo_unlocked(&work, "Device A").unwrap();
set_remote_unlocked(&work, remote.to_str().unwrap()).unwrap();
push_unlocked(&work).unwrap();
run_git_checked(
&work,
&["update-ref", "refs/agent-hub/conflict/skill-x", "HEAD"],
)
.unwrap();
run_git_checked(&work, &["update-ref", "refs/agent-hub/pre-merge", "HEAD"]).unwrap();
run_git_checked(
&work,
&[
"push",
"origin",
"refs/agent-hub/conflict/skill-x",
"refs/agent-hub/pre-merge",
],
)
.unwrap();
let leaked = run_git(&work, &["ls-remote", "origin", "refs/agent-hub/*"]).unwrap();
assert_eq!(
leaked.lines().count(),
2,
"setup: refs must be on the remote"
);
let removed = prune_hidden_refs_on_remote(&work).unwrap();
assert_eq!(removed, 2);
let after = run_git(&work, &["ls-remote", "origin", "refs/agent-hub/*"]).unwrap();
assert!(
after.trim().is_empty(),
"remote hidden refs must be gone: {after}"
);
run_git(&work, &["rev-parse", "refs/agent-hub/conflict/skill-x"]).unwrap();
run_git(&work, &["rev-parse", "refs/agent-hub/pre-merge"]).unwrap();
let heads = run_git(&work, &["ls-remote", "--heads", "origin"]).unwrap();
assert!(heads.contains("refs/heads/main"));
assert_eq!(prune_hidden_refs_on_remote(&work).unwrap(), 0);
}
#[test]
fn pull_conflict_aborts_merge_and_reports_sync_conflict() {
let tmp = tempfile::tempdir().unwrap();
let remote = tmp.path().join("remote.git");
let a = tmp.path().join("a");
let b = tmp.path().join("b");
std::fs::create_dir_all(&a).unwrap();
assert!(Command::new("git")
.args(["init", "--bare", "--initial-branch=main"])
.arg(&remote)
.output()
.unwrap()
.status
.success());
let git = |dir: &Path, args: &[&str]| {
Command::new("git")
.arg("-C")
.arg(dir)
.args(["-c", "user.email=test@example.com", "-c", "user.name=Test"])
.args(args)
.output()
.unwrap()
};
assert!(git(&a, &["init"]).status.success());
assert!(git(&a, &["checkout", "-b", "main"]).status.success());
std::fs::write(a.join("skill.md"), "base\n").unwrap();
assert!(git(&a, &["add", "-A"]).status.success());
assert!(git(&a, &["commit", "-m", "base"]).status.success());
assert!(
git(&a, &["remote", "add", "origin", remote.to_str().unwrap()])
.status
.success()
);
assert!(git(&a, &["push", "-u", "origin", "main"]).status.success());
assert!(Command::new("git")
.args(["clone", remote.to_str().unwrap()])
.arg(&b)
.output()
.unwrap()
.status
.success());
assert!(git(&b, &["config", "user.email", "test@example.com"])
.status
.success());
assert!(git(&b, &["config", "user.name", "Test"]).status.success());
std::fs::write(a.join("skill.md"), "edited on A\n").unwrap();
assert!(git(&a, &["commit", "-am", "edit A"]).status.success());
assert!(git(&a, &["push", "origin", "main"]).status.success());
std::fs::write(b.join("skill.md"), "edited on B\n").unwrap();
assert!(git(&b, &["commit", "-am", "edit B"]).status.success());
let err = pull_unlocked(&b).unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains("SYNC_CONFLICT"),
"expected a recognizable conflict error, got: {msg}"
);
assert!(
!b.join(".git/MERGE_HEAD").exists(),
"MERGE_HEAD should be gone after abort"
);
let porcelain = run_git(&b, &["status", "--porcelain"]).unwrap();
assert!(
porcelain.is_empty(),
"working tree should be clean after abort, got: {porcelain:?}"
);
ensure_no_interrupted_git_operation(&b).unwrap();
}
#[test]
fn ensure_clean_clone_target_refuses_file() {
let tmp = tempfile::tempdir().unwrap();
let target = tmp.path().join("a-file");
std::fs::write(&target, b"x").unwrap();
let err = ensure_clean_clone_target(&target).unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains("file, not a directory"),
"unexpected message: {msg}"
);
}
#[test]
fn auto_msg_user_configuration_is_distinct_from_skills_and_metadata() {
assert_eq!(
build_auto_commit_message(" M user-config/agent-launch.json\n"),
"auto backup: user-config/agent-launch"
);
assert_eq!(
build_auto_commit_message(
" M skills/demo/SKILL.md\n M user-config/agent-launch.json\n M .agent-hub/state.json\n"
),
"auto backup: demo, user-config/agent-launch, metadata"
);
}