#[cfg(test)]
mod tests {
use sqlitegraph::{BackendDirection, GraphConfig, NeighborQuery, open_graph};
#[test]
#[cfg(feature = "v2_experimental")]
fn test_v2_cluster_allocation_invariant_violation() {
println!("Starting V2 cluster allocation invariant violation test...");
let temp_dir = tempfile::tempdir().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("v2_cluster_allocation_test.db");
let config = GraphConfig::native();
let graph = open_graph(&db_path, &config).expect("Failed to open V2 graph");
println!("✅ Graph backend opened successfully");
let node_count = 10_000;
println!("Inserting {} nodes...", node_count);
let mut node_ids = Vec::with_capacity(node_count);
for i in 0..node_count {
let node_id = graph
.insert_node(sqlitegraph::NodeSpec {
kind: "TestNode".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({
"index": i,
"test_type": "cluster_allocation_invariant",
}),
})
.expect("Failed to insert node");
node_ids.push(node_id);
}
println!("✅ {} nodes inserted successfully", node_count);
println!("Inserting edges until invariant violation occurs...");
let seed = 0xCAFE_BABE_u32;
let mut rng_state = seed;
let mut edges_inserted = 0;
for edge_index in 0..50 {
rng_state = rng_state.wrapping_mul(1103515245).wrapping_add(12345);
let from_idx = rng_state as usize % node_count;
rng_state = rng_state.wrapping_mul(1103515245).wrapping_add(12345);
let mut to_idx = rng_state as usize % node_count;
if to_idx == from_idx {
to_idx = (to_idx + 1) % node_count;
}
let _edge_id = graph
.insert_edge(sqlitegraph::EdgeSpec {
from: node_ids[from_idx],
to: node_ids[to_idx],
edge_type: "test_edge".to_string(),
data: serde_json::json!({
"edge_index": edge_index,
"from_idx": from_idx,
"to_idx": to_idx,
"test_batch": edge_index / 1000,
}),
})
.expect("Edge insertion should succeed with the fix");
edges_inserted += 1;
if edge_index > 0 && edge_index % 10 == 0 {
println!(" Inserted {} edges so far...", edges_inserted);
}
}
println!(
"✅ SUCCESS: Inserted {} edges without invariant violation",
edges_inserted
);
println!("✅ CONFIRMED: The cluster allocation invariant violation has been fixed");
}
}