#[cfg(test)]
mod tests {
#[test]
fn test_hash_computation() {
let graph = crate::CodeGraph::open(":memory:").unwrap();
let source = b"fn test() {}";
let hash = graph.files.compute_hash(source);
assert_eq!(hash.len(), 16);
let hash2 = graph.files.compute_hash(source);
assert_eq!(hash, hash2);
let hash3 = graph.files.compute_hash(b"different content");
assert_ne!(hash, hash3);
}
#[test]
fn test_cross_file_references() {
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("test.db");
let mut graph = crate::CodeGraph::open(&db_path).unwrap();
let file1_path = "/test/file1.rs";
let file1_source = b"pub fn defined_in_file1() -> i32 { 42 }";
graph.index_file(file1_path, file1_source).unwrap();
let file2_path = "/test/file2.rs";
let file2_source = b"pub fn caller_in_file2() -> i32 { defined_in_file1() }";
graph.index_file(file2_path, file2_source).unwrap();
graph.index_references(file1_path, file1_source).unwrap();
graph.index_references(file2_path, file2_source).unwrap();
let symbols1 = graph.symbols_in_file(file1_path).unwrap();
assert_eq!(symbols1.len(), 1);
assert_eq!(symbols1[0].name, Some("defined_in_file1".to_string()));
let symbols2 = graph.symbols_in_file(file2_path).unwrap();
assert_eq!(symbols2.len(), 1);
assert_eq!(symbols2[0].name, Some("caller_in_file2".to_string()));
let symbol_id = graph
.symbol_id_by_name(file1_path, "defined_in_file1")
.unwrap();
assert!(symbol_id.is_some(), "Symbol defined_in_file1 should exist");
let symbol_id = symbol_id.unwrap();
let references = graph.references_to_symbol(symbol_id).unwrap();
assert!(
!references.is_empty(),
"Cross-file references should be created. Found: {} references. \
This demonstrates the bug: only same-file references are indexed.",
references.len()
);
}
}
mod pragma_tests {
use tempfile::TempDir;
#[test]
fn test_pragma_journal_mode_wal() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let journal_mode: String = conn
.query_row("PRAGMA journal_mode", [], |row| row.get(0))
.unwrap();
assert_eq!(
journal_mode, "wal",
"journal_mode should be 'wal' for better concurrency"
);
}
#[test]
fn test_pragma_synchronous_normal() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let synchronous: i32 = conn
.query_row("PRAGMA synchronous", [], |row| row.get(0))
.unwrap();
assert!(
synchronous == 1 || synchronous == 2,
"synchronous should be NORMAL (1) or FULL (2), got {}",
synchronous
);
}
#[test]
fn test_pragma_cache_size_configured() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let cache_size: i32 = conn
.query_row("PRAGMA cache_size", [], |row| row.get(0))
.unwrap();
assert!(cache_size < 0, "cache_size should be negative (KB)");
}
#[test]
fn test_pragma_temp_store_memory() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let temp_store: i32 = conn
.query_row("PRAGMA temp_store", [], |row| row.get(0))
.unwrap();
assert!(
temp_store >= 0 && temp_store <= 2,
"temp_store should be 0-2, got {}",
temp_store
);
}
#[test]
fn test_pragma_all_settings_applied() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let journal_mode: String = conn
.query_row("PRAGMA journal_mode", [], |row| row.get(0))
.unwrap();
assert_eq!(journal_mode, "wal", "WAL mode should be enabled");
let cache_size: i32 = conn
.query_row("PRAGMA cache_size", [], |row| row.get(0))
.unwrap();
assert!(cache_size < 0, "cache_size should be negative (KB)");
}
#[test]
fn test_pragma_persistence_across_reopens() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("test.db");
{
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let journal_mode: String = conn
.query_row("PRAGMA journal_mode", [], |row| row.get(0))
.unwrap();
assert_eq!(journal_mode, "wal", "WAL mode should be set on first open");
}
{
let _graph = crate::CodeGraph::open(&db_path).unwrap();
let conn = rusqlite::Connection::open(&db_path).unwrap();
let journal_mode: String = conn
.query_row("PRAGMA journal_mode", [], |row| row.get(0))
.unwrap();
assert_eq!(
journal_mode, "wal",
"WAL mode should persist across reopens"
);
}
}
}