use splice::graph::BackendType;
use std::path::{Path, PathBuf};
use std::process::Command;
#[test]
#[cfg(feature = "geometric")]
fn test_component_geometric_detection_workflow() {
use splice::graph::CodeGraph;
let temp_dir = tempfile::TempDir::new().unwrap();
let geo_path = temp_dir.path().join("test_component.geo");
std::fs::File::create(&geo_path).expect("Failed to create .geo file");
let backend =
CodeGraph::detect_backend(&geo_path).expect("Layer 3b: detect_backend should not fail");
assert_eq!(
backend,
BackendType::Geometric,
"Layer 3b: Should detect Geometric backend"
);
assert_eq!(
backend.to_string(),
"geometric",
"Layer 3c: Display should be 'geometric'"
);
}
#[test]
#[cfg(feature = "geometric")]
fn test_component_status_backend_detection() {
use splice::graph::CodeGraph;
use std::path::Path;
let temp_dir = tempfile::TempDir::new().unwrap();
let sqlite_path = temp_dir.path().join("test.sqlite.db");
std::fs::write(&sqlite_path, b"SQLite format 3\0").unwrap();
let backend = CodeGraph::detect_backend(&sqlite_path).unwrap();
assert_eq!(
backend.to_string(),
"sqlite",
"Layer 3a: Should detect SQLite"
);
let geo_path = temp_dir.path().join("test.geo");
std::fs::File::create(&geo_path).unwrap();
let backend = CodeGraph::detect_backend(&geo_path).unwrap();
#[cfg(feature = "geometric")]
assert_eq!(
backend.to_string(),
"geometric",
"Layer 3b: Should detect Geometric"
);
#[cfg(not(feature = "geometric"))]
assert_eq!(
backend.to_string(),
"unknown",
"Layer 3b: Should be Unknown without feature"
);
let nonexistent = temp_dir.path().join("nonexistent.db");
let backend = CodeGraph::detect_backend(&nonexistent).unwrap();
assert_eq!(
backend.to_string(),
"sqlite",
"Layer 3c: Non-existent should default to SQLite"
);
}
#[test]
fn test_component_backend_api_stability() {
use splice::graph::BackendType;
let sqlite = BackendType::SQLite;
let _sqlite_clone = sqlite.clone();
let _sqlite_copy: BackendType = sqlite;
let _ = sqlite;
let sqlite_debug = format!("{:?}", sqlite);
assert!(
sqlite_debug.contains("SQLite") || sqlite_debug.contains("SQLite"),
"Layer 3d: Debug should show variant name"
);
assert_eq!(format!("{}", sqlite), "sqlite");
#[cfg(feature = "geometric")]
{
let geometric = BackendType::Geometric;
assert_eq!(format!("{}", geometric), "geometric");
assert_ne!(geometric, sqlite);
}
}
#[test]
#[cfg(feature = "geometric")]
fn test_component_error_handling() {
use splice::graph::CodeGraph;
let result = CodeGraph::detect_backend(Path::new("/nonexistent/path/file.geo"));
assert!(result.is_ok(), "Layer 3a: Should return Ok, not panic");
assert_eq!(result.unwrap().to_string(), "sqlite");
let paths = vec![
"/nonexistent/path/file.geo",
"",
".",
"..",
"/",
"file.geo",
"file.db",
];
for path_str in paths {
let path = Path::new(path_str);
let _ = CodeGraph::is_geometric_db(path);
}
}
#[test]
#[cfg(feature = "geometric")]
fn test_component_partial_ok_validation() {
use splice::graph::CodeGraph;
let temp_dir = tempfile::TempDir::new().unwrap();
let sqlite_path = temp_dir.path().join("test.db");
std::fs::write(&sqlite_path, b"SQLite format 3\0").unwrap();
let geo_path = temp_dir.path().join("test.geo");
std::fs::File::create(&geo_path).unwrap();
let empty_path = temp_dir.path().join("empty");
std::fs::File::create(&empty_path).unwrap();
let sqlite_backend = CodeGraph::detect_backend(&sqlite_path).unwrap();
let geo_backend = CodeGraph::detect_backend(&geo_path).unwrap();
let _empty_backend = CodeGraph::detect_backend(&empty_path).unwrap();
assert!(
matches!(sqlite_backend, BackendType::SQLite),
"Layer 3b: SQLite detection should return SQLite variant"
);
let is_geo_sqlite = CodeGraph::is_geometric_db(&sqlite_path);
let is_geo_geo = CodeGraph::is_geometric_db(&geo_path);
assert!(!is_geo_sqlite, "Layer 3d: .db should not be geometric");
assert!(is_geo_geo, "Layer 3d: .geo should be geometric");
if is_geo_geo {
let backend_str = geo_backend.to_string();
assert!(
backend_str == "geometric" || backend_str == "unknown",
"Layer 3d: .geo file should be geometric or unknown, got {}",
backend_str
);
}
}