2024-03-31 00:25:36 +03:00
|
|
|
use std::{path::PathBuf, vec};
|
2024-03-30 21:41:45 +03:00
|
|
|
|
2024-07-17 21:16:48 +03:00
|
|
|
use gitbutler_diff::Hunk;
|
2024-10-08 16:59:27 +03:00
|
|
|
use gitbutler_stack::{reconcile_claims, BranchOwnershipClaims, OwnershipClaim, Stack};
|
2024-03-31 00:27:56 +03:00
|
|
|
|
2024-03-29 12:04:26 +03:00
|
|
|
#[test]
|
|
|
|
fn reconcile_ownership_simple() {
|
2024-10-08 17:05:23 +03:00
|
|
|
let mut branch_a = Stack::new_uninitialized(
|
2024-10-08 13:21:57 +03:00
|
|
|
"a".to_string(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
git2::Oid::zero(),
|
|
|
|
git2::Oid::zero(),
|
|
|
|
usize::default(),
|
|
|
|
None,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
branch_a.ownership = BranchOwnershipClaims {
|
|
|
|
claims: vec![OwnershipClaim {
|
|
|
|
file_path: PathBuf::from("foo"),
|
|
|
|
hunks: vec![
|
|
|
|
Hunk {
|
|
|
|
start: 1,
|
|
|
|
end: 3,
|
|
|
|
hash: Some(Hunk::hash("1,3")),
|
|
|
|
},
|
|
|
|
Hunk {
|
|
|
|
start: 4,
|
|
|
|
end: 6,
|
|
|
|
hash: Some(Hunk::hash("4,6")),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}],
|
2024-03-29 12:04:26 +03:00
|
|
|
};
|
2024-10-08 13:21:57 +03:00
|
|
|
branch_a.created_timestamp_ms = u128::default();
|
|
|
|
branch_a.updated_timestamp_ms = u128::default();
|
|
|
|
|
2024-10-08 17:05:23 +03:00
|
|
|
let mut branch_b = Stack::new_uninitialized(
|
2024-10-08 13:21:57 +03:00
|
|
|
"b".to_string(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
git2::Oid::zero(),
|
|
|
|
git2::Oid::zero(),
|
|
|
|
usize::default(),
|
|
|
|
None,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
branch_b.ownership = BranchOwnershipClaims {
|
|
|
|
claims: vec![OwnershipClaim {
|
|
|
|
file_path: PathBuf::from("foo"),
|
|
|
|
hunks: vec![Hunk {
|
|
|
|
start: 7,
|
|
|
|
end: 9,
|
|
|
|
hash: Some(Hunk::hash("7,9")),
|
2024-03-29 12:04:26 +03:00
|
|
|
}],
|
2024-10-08 13:21:57 +03:00
|
|
|
}],
|
2024-03-29 12:04:26 +03:00
|
|
|
};
|
2024-10-08 13:21:57 +03:00
|
|
|
branch_b.created_timestamp_ms = u128::default();
|
|
|
|
branch_b.updated_timestamp_ms = u128::default();
|
|
|
|
|
2024-10-08 16:59:27 +03:00
|
|
|
let all_branches: Vec<Stack> = vec![branch_a.clone(), branch_b.clone()];
|
2024-03-29 12:04:26 +03:00
|
|
|
let claim: Vec<OwnershipClaim> = vec![OwnershipClaim {
|
|
|
|
file_path: PathBuf::from("foo"),
|
|
|
|
hunks: vec![
|
|
|
|
Hunk {
|
|
|
|
start: 4,
|
|
|
|
end: 6,
|
2024-05-06 21:22:40 +03:00
|
|
|
hash: Some(Hunk::hash("4,6")),
|
2024-03-29 12:04:26 +03:00
|
|
|
},
|
|
|
|
Hunk {
|
|
|
|
start: 7,
|
|
|
|
end: 9,
|
2024-05-06 21:22:40 +03:00
|
|
|
hash: Some(Hunk::hash("9,7")),
|
2024-03-29 12:04:26 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}];
|
|
|
|
let claim_outcomes = reconcile_claims(all_branches.clone(), &branch_b, &claim).unwrap();
|
|
|
|
assert_eq!(claim_outcomes.len(), all_branches.len());
|
|
|
|
assert_eq!(claim_outcomes[0].updated_branch.id, branch_a.id);
|
|
|
|
assert_eq!(claim_outcomes[1].updated_branch.id, branch_b.id);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
claim_outcomes[0].updated_branch.ownership,
|
|
|
|
BranchOwnershipClaims {
|
|
|
|
claims: vec![OwnershipClaim {
|
|
|
|
file_path: PathBuf::from("foo"),
|
|
|
|
hunks: vec![Hunk {
|
|
|
|
start: 1,
|
|
|
|
end: 3,
|
2024-05-06 21:22:40 +03:00
|
|
|
hash: Some(Hunk::hash("1,3")),
|
2024-03-29 12:04:26 +03:00
|
|
|
},],
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
claim_outcomes[1].updated_branch.ownership,
|
|
|
|
BranchOwnershipClaims {
|
|
|
|
claims: vec![OwnershipClaim {
|
|
|
|
file_path: PathBuf::from("foo"),
|
|
|
|
hunks: vec![
|
|
|
|
Hunk {
|
|
|
|
start: 4,
|
|
|
|
end: 6,
|
2024-05-06 21:22:40 +03:00
|
|
|
hash: Some(Hunk::hash("4,6")),
|
2024-03-29 12:04:26 +03:00
|
|
|
},
|
|
|
|
Hunk {
|
|
|
|
start: 7,
|
|
|
|
end: 9,
|
2024-05-06 21:22:40 +03:00
|
|
|
hash: Some(Hunk::hash("9,7")),
|
2024-03-29 12:04:26 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ownership() {
|
|
|
|
let ownership = "src/main.rs:0-100\nsrc/main2.rs:200-300".parse::<BranchOwnershipClaims>();
|
|
|
|
assert!(ownership.is_ok());
|
|
|
|
let ownership = ownership.unwrap();
|
|
|
|
assert_eq!(ownership.claims.len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main.rs:0-100".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[1],
|
|
|
|
"src/main2.rs:200-300".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ownership_2() {
|
|
|
|
let ownership = "src/main.rs:0-100\nsrc/main2.rs:200-300".parse::<BranchOwnershipClaims>();
|
|
|
|
assert!(ownership.is_ok());
|
|
|
|
let ownership = ownership.unwrap();
|
|
|
|
assert_eq!(ownership.claims.len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main.rs:0-100".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[1],
|
|
|
|
"src/main2.rs:200-300".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn put() {
|
|
|
|
let mut ownership = "src/main.rs:0-100"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap();
|
2024-04-24 22:31:03 +03:00
|
|
|
ownership.put("src/main.rs:200-300".parse::<OwnershipClaim>().unwrap());
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(ownership.claims.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main.rs:200-300,0-100"
|
|
|
|
.parse::<OwnershipClaim>()
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn put_2() {
|
|
|
|
let mut ownership = "src/main.rs:0-100"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap();
|
2024-04-24 22:31:03 +03:00
|
|
|
ownership.put("src/main.rs2:200-300".parse::<OwnershipClaim>().unwrap());
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(ownership.claims.len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main.rs2:200-300".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[1],
|
|
|
|
"src/main.rs:0-100".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn put_3() {
|
|
|
|
let mut ownership = "src/main.rs:0-100\nsrc/main2.rs:100-200"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap();
|
2024-04-24 22:31:03 +03:00
|
|
|
ownership.put("src/main2.rs:200-300".parse::<OwnershipClaim>().unwrap());
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(ownership.claims.len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main2.rs:200-300,100-200"
|
|
|
|
.parse::<OwnershipClaim>()
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[1],
|
|
|
|
"src/main.rs:0-100".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn put_4() {
|
|
|
|
let mut ownership = "src/main.rs:0-100\nsrc/main2.rs:100-200"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap();
|
2024-04-24 22:31:03 +03:00
|
|
|
ownership.put("src/main2.rs:100-200".parse::<OwnershipClaim>().unwrap());
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(ownership.claims.len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main2.rs:100-200".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[1],
|
|
|
|
"src/main.rs:0-100".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn put_7() {
|
|
|
|
let mut ownership = "src/main.rs:100-200"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap();
|
2024-04-24 22:31:03 +03:00
|
|
|
ownership.put("src/main.rs:100-200".parse::<OwnershipClaim>().unwrap());
|
2024-03-29 12:04:26 +03:00
|
|
|
assert_eq!(ownership.claims.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main.rs:100-200".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn take_1() {
|
|
|
|
let mut ownership = "src/main.rs:100-200,200-300"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap();
|
|
|
|
let taken = ownership.take(&"src/main.rs:100-200".parse::<OwnershipClaim>().unwrap());
|
|
|
|
assert_eq!(ownership.claims.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
ownership.claims[0],
|
|
|
|
"src/main.rs:200-300".parse::<OwnershipClaim>().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
taken,
|
|
|
|
vec!["src/main.rs:100-200".parse::<OwnershipClaim>().unwrap()]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equal() {
|
|
|
|
for (a, b, expected) in vec![
|
|
|
|
(
|
|
|
|
"src/main.rs:100-200"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
"src/main.rs:100-200"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"src/main.rs:100-200\nsrc/main1.rs:300-400\n"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
"src/main.rs:100-200"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"src/main.rs:100-200\nsrc/main1.rs:300-400\n"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
"src/main.rs:100-200\nsrc/main1.rs:300-400\n"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"src/main.rs:300-400\nsrc/main1.rs:100-200\n"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
"src/main1.rs:100-200\nsrc/main.rs:300-400\n"
|
|
|
|
.parse::<BranchOwnershipClaims>()
|
|
|
|
.unwrap(),
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
] {
|
|
|
|
assert_eq!(a == b, expected, "{:#?} == {:#?}", a, b);
|
|
|
|
}
|
|
|
|
}
|