use super::*;
struct TestEnv {
_lock: std::sync::MutexGuard<'static, ()>,
_tmp: tempfile::TempDir,
store: SkillStore,
skills_dir: std::path::PathBuf,
}
impl Drop for TestEnv {
fn drop(&mut self) {
central_repo::set_test_base_dir_override(None);
}
}
fn test_env() -> TestEnv {
git_credentials::use_mock_keyring();
let lock = central_repo::test_base_dir_lock();
let tmp = tempfile::tempdir().unwrap();
let base = tmp.path().join("base");
central_repo::set_test_base_dir_override(Some(base.clone()));
let skills_dir = central_repo::skills_dir();
std::fs::create_dir_all(&skills_dir).unwrap();
let store = SkillStore::new(&base.join("test.db")).unwrap();
TestEnv {
_lock: lock,
_tmp: tmp,
store,
skills_dir,
}
}
fn git(dir: &Path, args: &[&str]) {
let out = std::process::Command::new("git")
.arg("-C")
.arg(dir)
.args(args)
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
}
fn origin_url(dir: &Path) -> String {
let out = std::process::Command::new("git")
.arg("-C")
.arg(dir)
.args(["remote", "get-url", "origin"])
.output()
.unwrap();
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
#[test]
fn restored_metadata_keeps_local_only_skills_even_when_remote_snapshot_is_empty() {
let env = test_env();
sync_metadata::write_all_from_db_unlocked(&env.store).unwrap();
let local = env.skills_dir.join("local-only");
std::fs::create_dir_all(&local).unwrap();
std::fs::write(local.join("SKILL.md"), "# Local only").unwrap();
reconcile_skills_index_unlocked(&env.store).unwrap();
let skills = env.store.get_all_skills().unwrap();
assert_eq!(skills.len(), 1);
let id = skills[0].id.clone();
let additional = env.skills_dir.join("another-local");
std::fs::create_dir_all(&additional).unwrap();
std::fs::write(additional.join("SKILL.md"), "# Another local").unwrap();
reconcile_skills_index_unlocked(&env.store).unwrap();
let skills = env.store.get_all_skills().unwrap();
assert_eq!(skills.len(), 2);
assert!(skills.iter().any(|skill| skill.id == id));
sync_metadata::reindex_from_metadata_unlocked(&env.store).unwrap();
assert_eq!(env.store.get_all_skills().unwrap().len(), 2);
assert!(local.join("SKILL.md").is_file());
}
#[test]
fn device_name_default_persists_and_rename_updates_repo_config() {
let env = test_env();
let name = effective_device_name(&env.store);
assert!(!name.is_empty());
assert_eq!(
env.store
.get_setting("backup_device_name")
.unwrap()
.as_deref(),
Some(name.as_str())
);
git(&env.skills_dir, &["init", "-b", "main"]);
env.store
.set_setting("backup_device_name", "Work Laptop")
.unwrap();
apply_device_identity(&env.store, &env.skills_dir);
let user_name = std::process::Command::new("git")
.arg("-C")
.arg(&env.skills_dir)
.args(["config", "--local", "--get", "user.name"])
.output()
.unwrap();
assert_eq!(
String::from_utf8_lossy(&user_name.stdout).trim(),
"Work Laptop"
);
}
#[test]
fn sync_transaction_commits_merges_and_pushes_in_one_call() {
let env = test_env();
let remote = env._tmp.path().join("remote.git");
assert!(std::process::Command::new("git")
.args(["init", "--bare", "--initial-branch=main"])
.arg(&remote)
.output()
.unwrap()
.status
.success());
crate::core::git_backup::init_repo_unlocked(&env.skills_dir, "Device A").unwrap();
crate::core::git_backup::set_remote_unlocked(&env.skills_dir, remote.to_str().unwrap())
.unwrap();
crate::core::git_backup::push_unlocked(&env.skills_dir).unwrap();
let other = env._tmp.path().join("other");
assert!(std::process::Command::new("git")
.arg("clone")
.arg(&remote)
.arg(&other)
.output()
.unwrap()
.status
.success());
crate::core::git_backup::configure_device_identity(&other, "Device B").unwrap();
std::fs::write(other.join("from-b.md"), "b").unwrap();
crate::core::git_backup::commit_all_unlocked(&other, "from B").unwrap();
git(&other, &["push", "origin", "main"]);
std::fs::write(env.skills_dir.join("local.md"), "a").unwrap();
let outcome = run_sync_blocking(&env.store, &env.skills_dir, "backup").unwrap();
assert!(outcome.committed);
assert!(outcome.merge.is_some(), "a merge must have run");
assert!(outcome.pushed);
assert!(outcome.snapshot_tag.is_some());
let remote_head = {
let out = std::process::Command::new("git")
.arg("-C")
.arg(&remote)
.args(["rev-parse", "main"])
.output()
.unwrap();
String::from_utf8_lossy(&out.stdout).trim().to_string()
};
let local_head = {
let out = std::process::Command::new("git")
.arg("-C")
.arg(&env.skills_dir)
.args(["rev-parse", "HEAD"])
.output()
.unwrap();
String::from_utf8_lossy(&out.stdout).trim().to_string()
};
assert_eq!(remote_head, local_head);
assert!(env.skills_dir.join("from-b.md").exists());
assert!(env.skills_dir.join("local.md").exists());
let quiet = run_sync_blocking(&env.store, &env.skills_dir, "backup").unwrap();
assert!(!quiet.committed);
assert!(quiet.merge.is_none());
assert!(!quiet.pushed);
assert!(quiet.snapshot_tag.is_none());
}
#[test]
fn migrate_is_noop_without_embedded_credentials() {
let env = test_env();
git(&env.skills_dir, &["init", "-b", "main"]);
git(
&env.skills_dir,
&[
"remote",
"add",
"origin",
"https://github.com/acme/repo.git",
],
);
env.store
.set_setting("git_backup_remote_url", "https://github.com/acme/repo.git")
.unwrap();
let result = migrate_embedded_credentials_unlocked(&env.store, &env.skills_dir).unwrap();
assert_eq!(result, None);
assert_eq!(
origin_url(&env.skills_dir),
"https://github.com/acme/repo.git"
);
}
#[test]
fn disconnect_clears_origin_and_setting_and_is_idempotent() {
let env = test_env();
git(&env.skills_dir, &["init", "-b", "main"]);
git(
&env.skills_dir,
&[
"remote",
"add",
"origin",
"https://github.com/acme/repo.git",
],
);
env.store
.set_setting("git_backup_remote_url", "https://github.com/acme/repo.git")
.unwrap();
disconnect_local(&env.store, &env.skills_dir).unwrap();
assert_eq!(origin_url(&env.skills_dir), "");
assert_eq!(
env.store
.get_setting("git_backup_remote_url")
.unwrap()
.as_deref(),
Some("")
);
disconnect_local(&env.store, &env.skills_dir).unwrap();
}
#[test]
fn migrate_sanitizes_setting_when_no_repo_exists() {
let env = test_env();
env.store
.set_setting(
"git_backup_remote_url",
"https://user:tok@github.com/acme/repo.git",
)
.unwrap();
let result = migrate_embedded_credentials_unlocked(&env.store, &env.skills_dir).unwrap();
assert_eq!(result.as_deref(), Some("https://github.com/acme/repo.git"));
assert_eq!(
env.store
.get_setting("git_backup_remote_url")
.unwrap()
.as_deref(),
Some("https://github.com/acme/repo.git")
);
}
#[test]
fn migrate_rolls_back_config_and_setting_on_verify_failure() {
let env = test_env();
let token_url = "https://user:sometok@127.0.0.1:1/acme/repo.git";
git(&env.skills_dir, &["init", "-b", "main"]);
git(&env.skills_dir, &["remote", "add", "origin", token_url]);
env.store
.set_setting("git_backup_remote_url", token_url)
.unwrap();
let err = migrate_embedded_credentials_unlocked(&env.store, &env.skills_dir).unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains("verification failed"),
"unexpected error: {msg}"
);
assert!(
!msg.contains("sometok"),
"token must not leak into the error: {msg}"
);
assert_eq!(origin_url(&env.skills_dir), token_url);
assert_eq!(
env.store
.get_setting("git_backup_remote_url")
.unwrap()
.as_deref(),
Some(token_url)
);
}
#[test]
fn backup_status_reports_the_configuration_reason_instead_of_only_prepare_context() {
let env = test_env();
let root = crate::core::user_config::ensure_root().unwrap();
std::fs::write(
root.join("agent-launch.json"),
r#"{"schema_version":99,"default_agent":"codex","schemes":{}}"#,
)
.unwrap();
let error = backup_status_blocking(&env.store).unwrap_err();
assert!(error.message.contains("Cannot prepare backup repository"));
assert!(
error
.message
.contains("Unsupported Agent launch configuration version"),
"{}",
error.message
);
}