#![cfg(feature = "v2_experimental")]
use serde_json::json;
use sqlitegraph::backend::native::{
EdgeRecord, NativeNodeId, NodeRecord, edge_store::EdgeStore, graph_file::GraphFile,
graph_ops::native_bfs, node_store::NodeStore,
};
use tempfile::{NamedTempFile, TempDir};
#[test]
fn test_clean_graph_file_header_per_test() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("isolation_test_1.db");
let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");
let header = graph_file.header();
assert_eq!(header.node_count, 0, "New graph file should have 0 nodes");
assert_eq!(header.edge_count, 0, "New graph file should have 0 edges");
assert_eq!(header.version, 2, "New graph file should use V2 format");
{
let mut node_store = NodeStore::new(&mut graph_file);
let node = NodeRecord::new(1, "Test".to_string(), "node1".to_string(), json!({}));
node_store.write_node(&node).expect("Failed to write node");
}
assert_eq!(
graph_file.header().node_count,
1,
"Node count should be 1 in this test"
);
}
#[test]
fn test_unique_tempfile_per_test() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("unique_test_2.db");
assert!(
!db_path.exists(),
"Database file should not exist before creation"
);
let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");
{
let mut node_store = NodeStore::new(&mut graph_file);
let node = NodeRecord::new(
2,
"Unique".to_string(),
"unique_node".to_string(),
json!({}),
);
node_store.write_node(&node).expect("Failed to write node");
}
let mut node_store = NodeStore::new(&mut graph_file);
let read_node = node_store.read_node(2).expect("Failed to read node");
assert_eq!(read_node.name, "unique_node");
assert_eq!(read_node.kind, "Unique");
}
#[test]
fn test_graph_file_proper_closure() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("closure_test_3.db");
{
let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");
{
let mut node_store = NodeStore::new(&mut graph_file);
let node = NodeRecord::new(
3,
"Closure".to_string(),
"closure_node".to_string(),
json!({}),
);
node_store.write_node(&node).expect("Failed to write node");
}
graph_file.flush().expect("Failed to flush graph file");
graph_file.sync().expect("Failed to sync graph file");
}
let mut graph_file = GraphFile::create(&db_path).expect("Failed to reopen graph file");
{
let mut node_store = NodeStore::new(&mut graph_file);
let read_node = node_store
.read_node(3)
.expect("Failed to read node after reopen");
assert_eq!(read_node.name, "closure_node");
}
}
#[test]
fn test_read_buffer_isolation() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("buffer_test_4.db");
let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");
{
let mut node_store = NodeStore::new(&mut graph_file);
let node = NodeRecord::new(
4,
"Buffer".to_string(),
"buffer_node".to_string(),
json!({"test": "buffer_isolation"}),
);
node_store.write_node(&node).expect("Failed to write node");
}
graph_file.invalidate_read_buffer();
{
let mut node_store = NodeStore::new(&mut graph_file);
let read_node = node_store.read_node(4).expect("Failed to read node");
assert_eq!(read_node.data, json!({"test": "buffer_isolation"}));
assert_eq!(read_node.name, "buffer_node");
}
}
#[test]
fn test_v2_format_is_default_under_native_backend() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("v1_test_5.db");
let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");
let node_slot_offset = graph_file.header().node_data_offset;
let mut version_buf = [0u8; 1];
graph_file
.read_bytes(node_slot_offset, &mut version_buf)
.expect("Failed to read version");
assert_eq!(
version_buf[0], 2,
"Native backend should default to V2 format"
);
}
#[test]
fn test_edge_operations_with_v2_isolation() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let db_path = temp_dir.path().join("edge_test_6.db");
let mut graph_file = GraphFile::create(&db_path).expect("Failed to create graph file");
{
let mut node_store = NodeStore::new(&mut graph_file);
let source = NodeRecord::new(1, "EdgeTest".to_string(), "source".to_string(), json!({}));
let target = NodeRecord::new(2, "EdgeTest".to_string(), "target".to_string(), json!({}));
node_store
.write_node(&source)
.expect("Failed to write source node");
node_store
.write_node(&target)
.expect("Failed to write target node");
graph_file
.flush_write_buffer()
.expect("Failed to flush write buffer");
graph_file.invalidate_read_buffer();
}
{
let mut edge_store = EdgeStore::new(&mut graph_file);
let edge = EdgeRecord::new(1, 1, 2, "LINK".to_string(), json!({"weight": 1}));
edge_store.write_edge(&edge).expect("Failed to write edge");
}
let result = native_bfs(&mut graph_file, 1, 1);
assert!(
result.is_ok(),
"BFS should work correctly with isolated V2 nodes and edges"
);
}