mod common;
use common::sample_triple;
use nusy_arrow_core::{Namespace, YLayer};
use nusy_arrow_git::{
CommitsTable, GitObjectStore, RefsTable, checkout, cherry_pick, create_commit, revert,
};
#[test]
fn test_full_lifecycle() {
let tmp = tempfile::tempdir().unwrap();
let mut obj = GitObjectStore::with_snapshot_dir(tmp.path());
let mut commits = CommitsTable::new();
let mut refs = RefsTable::new();
obj.store
.add_triple(
&sample_triple("initial", "Base"),
Namespace::World,
YLayer::Semantic,
)
.unwrap();
let c0 = create_commit(&obj, &mut commits, vec![], "initial commit", "DGX").unwrap();
refs.init_main(&c0.commit_id);
assert_eq!(obj.store.len(), 1);
refs.create_branch("feature", &c0.commit_id).unwrap();
refs.switch_head("feature").unwrap();
assert_eq!(refs.head().unwrap().ref_name, "feature");
obj.store
.add_triple(
&sample_triple("feat1", "F1"),
Namespace::World,
YLayer::Semantic,
)
.unwrap();
let f1 = create_commit(
&obj,
&mut commits,
vec![c0.commit_id.clone()],
"feature commit 1",
"DGX",
)
.unwrap();
refs.update_ref("feature", &f1.commit_id).unwrap();
obj.store
.add_triple(
&sample_triple("feat2", "F2"),
Namespace::World,
YLayer::Semantic,
)
.unwrap();
let f2 = create_commit(
&obj,
&mut commits,
vec![f1.commit_id.clone()],
"feature commit 2",
"DGX",
)
.unwrap();
refs.update_ref("feature", &f2.commit_id).unwrap();
assert_eq!(obj.store.len(), 3);
refs.create_tag("v1.0", &f2.commit_id).unwrap();
assert_eq!(refs.tags().len(), 1);
assert_eq!(refs.resolve("v1.0"), Some(f2.commit_id.as_str()));
refs.switch_head("main").unwrap();
checkout(&mut obj, &commits, &c0.commit_id).unwrap();
assert_eq!(obj.store.len(), 1);
let cp_id = cherry_pick(&mut obj, &mut commits, &f1.commit_id, &c0.commit_id, "DGX").unwrap();
refs.update_ref("main", &cp_id).unwrap();
assert_eq!(obj.store.len(), 2);
let revert_id = revert(&mut obj, &mut commits, &cp_id, &cp_id, "DGX").unwrap();
refs.update_ref("main", &revert_id).unwrap();
assert_eq!(obj.store.len(), 1);
refs.delete_branch("feature").unwrap();
assert_eq!(refs.branches().len(), 1); assert!(refs.get("feature").is_none());
assert_eq!(refs.resolve("v1.0"), Some(f2.commit_id.as_str()));
assert_eq!(refs.tags().len(), 1);
assert!(commits.len() >= 5);
}
#[test]
fn test_tag_and_branch_namespace_isolation() {
let mut refs = RefsTable::new();
refs.init_main("c1");
refs.create_branch("dev", "c1").unwrap();
refs.create_tag("v1.0", "c1").unwrap();
refs.create_tag("v2.0", "c2").unwrap();
assert_eq!(refs.branches().len(), 2);
assert_eq!(refs.tags().len(), 2);
assert!(refs.get("dev").is_some());
assert!(refs.get("v1.0").is_some());
}
#[test]
fn test_delete_branch_leaves_commits_accessible() {
let tmp = tempfile::tempdir().unwrap();
let mut obj = GitObjectStore::with_snapshot_dir(tmp.path());
let mut commits = CommitsTable::new();
let mut refs = RefsTable::new();
obj.store
.add_triple(
&sample_triple("s1", "A"),
Namespace::World,
YLayer::Semantic,
)
.unwrap();
let c1 = create_commit(&obj, &mut commits, vec![], "c1", "DGX").unwrap();
refs.init_main(&c1.commit_id);
refs.create_branch("ephemeral", &c1.commit_id).unwrap();
obj.store
.add_triple(
&sample_triple("s2", "B"),
Namespace::World,
YLayer::Semantic,
)
.unwrap();
let c2 = create_commit(
&obj,
&mut commits,
vec![c1.commit_id.clone()],
"ephemeral work",
"DGX",
)
.unwrap();
refs.update_ref("ephemeral", &c2.commit_id).unwrap();
refs.delete_branch("ephemeral").unwrap();
assert!(commits.get(&c2.commit_id).is_some());
checkout(&mut obj, &commits, &c2.commit_id).unwrap();
assert_eq!(obj.store.len(), 2);
}