use chrono::Utc;
use contextgraph_core::commit::{Author, Commit, Delta, Metadata};
use contextgraph_core::mem::{InMemoryCommitStore, InMemoryRefStore};
use contextgraph_core::store::{CommitStore, RefStore};
use contextgraph_core::CommitId;
use proptest::prelude::*;
fn rt() -> tokio::runtime::Runtime {
tokio::runtime::Runtime::new().unwrap()
}
fn make_commit(parents: Vec<CommitId>, text: &str) -> Commit {
Commit::new(
parents,
Author::User,
Delta::Message {
content: text.to_string(),
},
Metadata::new(Utc::now()),
)
}
#[derive(Debug, Clone)]
enum Op {
NewRoot(String),
NewChild(usize, String),
Duplicate(usize),
}
fn op_strategy(max_index: usize) -> impl Strategy<Value = Op> {
if max_index == 0 {
"[a-z]{1,8}".prop_map(Op::NewRoot).boxed()
} else {
prop_oneof![
"[a-z]{1,8}".prop_map(Op::NewRoot),
(0..max_index, "[a-z]{1,8}").prop_map(|(i, s)| Op::NewChild(i, s)),
(0..max_index).prop_map(Op::Duplicate),
]
.boxed()
}
}
fn ops_strategy() -> impl Strategy<Value = Vec<Op>> {
(1usize..15).prop_flat_map(|len| (0..len).map(op_strategy).collect::<Vec<_>>())
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(200))]
#[test]
fn arbitrary_commit_sequences_preserve_content_addressing_and_acyclicity(ops in ops_strategy()) {
let runtime = rt();
runtime.block_on(async {
let store = InMemoryCommitStore::new();
let mut created: Vec<CommitId> = Vec::new();
let mut distinct_contents: std::collections::HashSet<CommitId> = std::collections::HashSet::new();
for op in ops {
match op {
Op::NewRoot(text) => {
let c = make_commit(vec![], &format!("root:{text}"));
let id = store.put(c).await.unwrap();
distinct_contents.insert(id);
created.push(id);
}
Op::NewChild(idx, text) => {
let parent = created[idx % created.len().max(1)];
let c = make_commit(vec![parent], &text);
let id = store.put(c).await.unwrap();
distinct_contents.insert(id);
created.push(id);
prop_assert!(store.contains(&parent).await.unwrap());
}
Op::Duplicate(idx) => {
if created.is_empty() { continue; }
let target = created[idx % created.len()];
let original = store.get(&target).await.unwrap().unwrap();
let before_len = store.len().await.unwrap();
let id_again = store.put(original).await.unwrap();
let after_len = store.len().await.unwrap();
prop_assert_eq!(id_again, target);
prop_assert_eq!(before_len, after_len);
created.push(id_again);
}
}
}
prop_assert_eq!(store.len().await.unwrap(), distinct_contents.len());
for id in store.all_ids().await.unwrap() {
let c = store.get(&id).await.unwrap().unwrap();
for p in &c.parent_ids {
prop_assert!(store.contains(p).await.unwrap());
prop_assert_ne!(*p, id, "a commit can never be its own parent");
}
}
Ok(())
})?;
}
#[test]
fn commits_are_never_mutated_by_branch_pointer_moves(text in "[a-z]{1,8}", other_text in "[a-z]{1,8}") {
let runtime = rt();
runtime.block_on(async {
let store = InMemoryCommitStore::new();
let refs = InMemoryRefStore::new();
let root = make_commit(vec![], &text);
let root_id = store.put(root.clone()).await.unwrap();
let child = make_commit(vec![root_id], &other_text);
let child_id = store.put(child).await.unwrap();
refs.set_branch("main", root_id).await.unwrap();
refs.set_branch("main", child_id).await.unwrap();
refs.set_branch("main", root_id).await.unwrap();
let fetched = store.get(&root_id).await.unwrap().unwrap();
prop_assert_eq!(fetched, root);
Ok(())
})?;
}
}