use sqlitegraph::{EdgeSpec, GraphConfig, NodeSpec, SnapshotId, open_graph};
#[test]
fn test_phase64_node_count_durability_regression() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Phase 64 Node Count Durability Regression Test ===");
let temp_dir = tempfile::tempdir()?;
let db_path = temp_dir.path().join("phase64_node_count_test.db");
println!("STEP 1: Creating database and inserting nodes...");
let config = GraphConfig::native();
let graph = open_graph(&db_path, &config)?;
let mut node_ids = Vec::new();
for i in 1..=3 {
let node_id = graph.insert_node(NodeSpec {
kind: "TestNode".to_string(),
name: format!("node_{}", i),
file_path: None,
data: serde_json::json!({"index": i}),
})?;
node_ids.push(node_id);
println!("✅ Created node {} with ID {}", i, node_id);
}
println!("STEP 2: Closing database (no edges inserted)...");
drop(graph);
println!("✅ Database closed");
println!("STEP 3: Reopening database...");
let mut reopen_config = GraphConfig::native();
reopen_config.native.create_if_missing = false;
let graph_reopened = open_graph(&db_path, &reopen_config)?;
println!("✅ Database reopened successfully");
println!("STEP 4: Verifying node accessibility after reopen...");
for (i, &expected_id) in node_ids.iter().enumerate() {
let neighbors =
graph_reopened.neighbors(SnapshotId::current(), expected_id, Default::default())?;
println!(
"✅ Node {} (ID {}) accessible - has {} neighbors",
i + 1,
expected_id,
neighbors.len()
);
}
println!(
"✅ All {} nodes accessible after reopen - node_count persistence confirmed",
node_ids.len()
);
println!("=== PHASE 64 NODE COUNT DURABILITY TEST PASSED ===");
Ok(())
}
#[test]
fn test_phase64_node_count_durability_with_edges() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Phase 64 Node Count Durability Test (with edges) ===");
let temp_dir = tempfile::tempdir()?;
let db_path = temp_dir.path().join("phase64_node_count_edges_test.db");
let config = GraphConfig::native();
let graph = open_graph(&db_path, &config)?;
let node1 = graph.insert_node(NodeSpec {
kind: "SourceNode".to_string(),
name: "source".to_string(),
file_path: None,
data: serde_json::json!({"type": "source"}),
})?;
let node2 = graph.insert_node(NodeSpec {
kind: "TargetNode".to_string(),
name: "target".to_string(),
file_path: None,
data: serde_json::json!({"type": "target"}),
})?;
println!("✅ Created nodes: {} and {}", node1, node2);
drop(graph);
println!("✅ Closed database before edge insertion");
let mut reopen_config = GraphConfig::native();
reopen_config.native.create_if_missing = false;
let graph_reopened = open_graph(&db_path, &reopen_config)?;
println!("✅ Reopened database");
let neighbors1 = graph_reopened.neighbors(SnapshotId::current(), node1, Default::default())?;
let neighbors2 = graph_reopened.neighbors(SnapshotId::current(), node2, Default::default())?;
println!(
"✅ Nodes accessible after reopen - node1: {} neighbors, node2: {} neighbors",
neighbors1.len(),
neighbors2.len()
);
let edge_id = graph_reopened.insert_edge(EdgeSpec {
from: node1,
to: node2,
edge_type: "test_edge".to_string(),
data: serde_json::json!({"test": true}),
})?;
println!("✅ Inserted edge with ID {}", edge_id);
println!("=== PHASE 64 NODE COUNT DURABILITY WITH EDGES TEST PASSED ===");
Ok(())
}