use std::path::Path;
use tempfile::NamedTempFile;
#[test]
fn test_sqlite_backend_open_and_store() {
let temp_file = NamedTempFile::new().unwrap();
let mut graph = splice::graph::CodeGraph::open(temp_file.path()).unwrap();
assert!(matches!(
graph.backend_type(),
splice::graph::BackendType::SQLite
));
assert!(!graph.is_geometric());
let node_id = graph
.store_symbol_with_file_and_language(
Path::new("src/test.rs"),
"test_function",
"function",
splice::symbol::Language::Rust,
100,
200,
10,
20,
4,
0,
)
.unwrap();
let found = graph.find_symbol_in_file("src/test.rs", "test_function");
assert_eq!(found, Some(node_id));
let (start, end) = graph.get_span(node_id).unwrap();
assert_eq!(start, 100);
assert_eq!(end, 200);
}
#[test]
fn test_router_dispatches_to_sqlite_for_db() {
let temp_file = NamedTempFile::new().unwrap();
let graph = splice::graph::CodeGraph::open(temp_file.path()).unwrap();
assert!(matches!(
graph.backend_type(),
splice::graph::BackendType::SQLite
));
assert!(graph.inner().is_ok());
assert!(graph.as_sqlite().is_some());
assert!(!graph.is_geometric());
}
#[test]
#[cfg(feature = "geometric")]
fn test_router_dispatches_to_geo_for_geo() {
if !Path::new("./code.geo").exists() {
eprintln!("SKIP: No code.geo file available for test");
return;
}
let graph = splice::graph::CodeGraph::open(Path::new("./code.geo")).unwrap();
assert!(matches!(
graph.backend_type(),
splice::graph::BackendType::Geometric
));
assert!(graph.is_geometric());
assert!(graph.geometric().is_ok());
assert!(graph.as_geo().is_some());
assert!(graph.inner().is_err());
assert!(graph.as_sqlite().is_none());
}
#[test]
fn test_both_backends_find_symbols_by_name() {
let temp_file = NamedTempFile::new().unwrap();
let mut graph = splice::graph::CodeGraph::open(temp_file.path()).unwrap();
graph
.store_symbol_with_file_and_language(
Path::new("src/a.rs"),
"common",
"function",
splice::symbol::Language::Rust,
0,
10,
1,
1,
0,
0,
)
.unwrap();
graph
.store_symbol_with_file_and_language(
Path::new("src/b.rs"),
"common",
"function",
splice::symbol::Language::Rust,
0,
10,
1,
1,
0,
0,
)
.unwrap();
let matches = graph.find_symbols_by_name("common");
assert_eq!(matches.len(), 2);
}
#[test]
fn test_both_backends_all_symbol_names() {
let temp_file = NamedTempFile::new().unwrap();
let mut graph = splice::graph::CodeGraph::open(temp_file.path()).unwrap();
graph
.store_symbol_with_file_and_language(
Path::new("src/test.rs"),
"func_a",
"function",
splice::symbol::Language::Rust,
0,
10,
1,
1,
0,
0,
)
.unwrap();
graph
.store_symbol_with_file_and_language(
Path::new("src/test.rs"),
"func_b",
"function",
splice::symbol::Language::Rust,
20,
30,
2,
2,
0,
0,
)
.unwrap();
let names = graph.all_symbol_names();
assert!(names.contains(&"func_a".to_string()));
assert!(names.contains(&"func_b".to_string()));
}
#[test]
fn test_backend_detection() {
let sqlite_file = NamedTempFile::new().unwrap();
let detected = splice::graph::CodeGraph::detect_backend(sqlite_file.path()).unwrap();
assert!(matches!(detected, splice::graph::BackendType::SQLite));
let detected = splice::graph::CodeGraph::detect_backend(Path::new("test.geo")).unwrap();
assert!(matches!(detected, splice::graph::BackendType::Geometric));
}
#[test]
fn test_is_geometric_db_helper() {
assert!(splice::graph::CodeGraph::is_geometric_db(Path::new(
"code.geo"
)));
assert!(!splice::graph::CodeGraph::is_geometric_db(Path::new(
"code.db"
)));
assert!(!splice::graph::CodeGraph::is_geometric_db(Path::new(
".magellan/splice.db"
)));
}
#[test]
#[cfg(feature = "geometric")]
fn test_geo_backend_real_workflow() {
if !Path::new("./code.geo").exists() {
eprintln!("SKIP: No code.geo file available for test");
return;
}
let graph = splice::graph::CodeGraph::open(Path::new("./code.geo")).unwrap();
assert!(graph.is_geometric());
let file_path = "/home/feanor/Projects/splice/src/graph/mod.rs";
let _symbols = graph.find_symbols_by_name("open");
let names = graph.all_symbol_names();
assert!(!names.is_empty(), "Geometric backend should have symbols");
}