use std::path::PathBuf;
use crate::service::persistence::{
load_index_registry_at, remove_index_registry_entry_at, upsert_index_registry_entry_at,
PersistedIndex,
};
use crate::service::roots_registry::{load_roots_at, remove_root_at, upsert_root_at};
#[test]
fn upsert_does_not_clobber_colocated_flag_of_other_entries() {
let tmp = tempfile::NamedTempFile::new().unwrap();
let path = tmp.path().to_path_buf();
upsert_index_registry_entry_at(
&path,
PersistedIndex {
id: "central-store-idx".into(),
root_path: PathBuf::from("/repos/central"),
colocated: false, ..Default::default()
},
)
.unwrap();
upsert_index_registry_entry_at(
&path,
PersistedIndex {
id: "colocated-idx".into(),
root_path: PathBuf::from("/repos/colocated"),
colocated: true,
..Default::default()
},
)
.unwrap();
upsert_index_registry_entry_at(
&path,
PersistedIndex {
id: "colocated-idx".into(),
root_path: PathBuf::from("/repos/colocated-moved"),
colocated: true,
..Default::default()
},
)
.unwrap();
let entries = load_index_registry_at(&path).unwrap();
assert_eq!(entries.len(), 2, "no entries should be added or removed");
let central = entries
.iter()
.find(|e| e.id == "central-store-idx")
.unwrap();
assert!(
!central.colocated,
"central-store-idx must keep colocated=false after patching a different index \
(issue #1088 / #1089)"
);
assert_eq!(central.root_path, PathBuf::from("/repos/central"));
let col = entries.iter().find(|e| e.id == "colocated-idx").unwrap();
assert!(col.colocated, "colocated-idx must keep colocated=true");
assert_eq!(
col.root_path,
PathBuf::from("/repos/colocated-moved"),
"colocated-idx root_path must be updated"
);
}
#[test]
fn remove_entry_and_root_independent_regression() {
let idx_tmp = tempfile::NamedTempFile::new().unwrap();
let roots_tmp = tempfile::NamedTempFile::new().unwrap();
upsert_index_registry_entry_at(
idx_tmp.path(),
PersistedIndex {
id: "keep-idx".into(),
root_path: PathBuf::from("/repos/keep"),
colocated: true,
..Default::default()
},
)
.unwrap();
upsert_index_registry_entry_at(
idx_tmp.path(),
PersistedIndex {
id: "drop-idx".into(),
root_path: PathBuf::from("/repos/drop"),
colocated: true,
..Default::default()
},
)
.unwrap();
upsert_root_at(roots_tmp.path(), PathBuf::from("/repos/keep")).unwrap();
upsert_root_at(roots_tmp.path(), PathBuf::from("/repos/drop")).unwrap();
assert_eq!(load_index_registry_at(idx_tmp.path()).unwrap().len(), 2);
assert_eq!(load_roots_at(roots_tmp.path()).unwrap().len(), 2);
remove_index_registry_entry_at(idx_tmp.path(), "drop-idx").unwrap();
remove_root_at(roots_tmp.path(), std::path::Path::new("/repos/drop")).unwrap();
let idx_after = load_index_registry_at(idx_tmp.path()).unwrap();
assert_eq!(idx_after.len(), 1, "one index must remain in indexes.toml");
assert_eq!(idx_after[0].id, "keep-idx");
let roots_after = load_roots_at(roots_tmp.path()).unwrap();
assert_eq!(roots_after.len(), 1, "one root must remain in roots.toml");
assert_eq!(roots_after[0].path, PathBuf::from("/repos/keep"));
remove_index_registry_entry_at(idx_tmp.path(), "drop-idx").unwrap();
remove_root_at(roots_tmp.path(), std::path::Path::new("/repos/drop")).unwrap();
assert_eq!(
load_index_registry_at(idx_tmp.path()).unwrap().len(),
1,
"double-delete must be idempotent"
);
}