use std::fs;
use std::path::Path;
use std::time::Instant;
use symforge::domain::ReferenceKind;
use symforge::live_index::LiveIndex;
use symforge::protocol::format;
use tempfile::TempDir;
fn write_file(dir: &Path, name: &str, content: &str) {
let path = dir.join(name);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(path, content).unwrap();
}
fn build_index(files: &[(&str, &str)]) -> (TempDir, symforge::live_index::SharedIndex) {
let dir = TempDir::new().expect("failed to create tempdir");
for (name, content) in files {
write_file(dir.path(), name, content);
}
let shared = LiveIndex::load(dir.path()).expect("LiveIndex::load failed");
(dir, shared)
}
#[test]
fn test_rust_call_site_extraction() {
let content = r#"
fn process(x: i32) -> i32 {
x + 1
}
fn main() {
let _ = process(42);
}
"#;
let (_dir, shared) = build_index(&[("src.rs", content)]);
let index = shared.read();
let refs = index.find_references_for_name("process", None, false);
assert!(
!refs.is_empty(),
"should find at least one Call reference to 'process'"
);
let call_refs: Vec<_> = refs
.iter()
.filter(|(_, r)| r.kind == ReferenceKind::Call)
.collect();
assert!(
!call_refs.is_empty(),
"should have at least one Call reference to 'process'"
);
assert_eq!(call_refs[0].1.name, "process");
}
#[test]
fn test_python_import_and_call_extraction() {
let content = r#"import os
def run():
path = os.path.join("/tmp", "out")
return path
"#;
let (_dir, shared) = build_index(&[("app.py", content)]);
let index = shared.read();
let import_refs = index.find_references_for_name("os", None, true);
let has_import = import_refs
.iter()
.any(|(_, r)| r.kind == ReferenceKind::Import);
assert!(has_import, "should have an Import reference for 'os'");
let all_refs = index.find_references_for_name("join", None, false);
let has_call = all_refs.iter().any(|(_, r)| r.kind == ReferenceKind::Call);
if !has_call {
let qualified_refs = index.find_references_for_name("os.path.join", None, false);
assert!(
!qualified_refs.is_empty(),
"should find 'join' or 'os.path.join' as a call reference"
);
}
let file_path = index
.all_files()
.map(|(p, _)| p)
.find(|p| p.ends_with("app.py"))
.cloned()
.expect("app.py should be indexed");
let file = index.get_file(&file_path).unwrap();
assert!(
!file.references.is_empty(),
"Python file should have at least one cross-reference extracted"
);
}
#[test]
fn test_ts_builtin_type_filter() {
let content = r#"
function greet(name: string): string {
return "Hello " + name;
}
function echo(msg: string): string {
return msg;
}
function format(prefix: string, value: string): string {
return prefix + value;
}
interface Config {
host: string;
port: number;
name: string;
}
"#;
let (_dir, shared) = build_index(&[("lib.ts", content)]);
let index = shared.read();
let refs = index.find_references_for_name("string", None, false);
assert!(
refs.len() < 10,
"TS built-in 'string' should return < 10 references (got {}), proves XREF-04",
refs.len()
);
let unfiltered = index.find_references_for_name("string", None, true);
let _ = unfiltered; }
#[test]
fn test_alias_map_resolution() {
let content = r#"
use std::collections::HashMap as Map;
fn build_map() -> Map<String, i32> {
let mut m: Map<String, i32> = Map::new();
m.insert("key".to_string(), 42);
m
}
"#;
let (_dir, shared) = build_index(&[("src.rs", content)]);
let index = shared.read();
let refs = index.find_references_for_name("HashMap", None, false);
let file_path = index
.all_files()
.map(|(p, _)| p)
.find(|p| p.ends_with("src.rs"))
.cloned()
.expect("src.rs should be indexed");
let file = index.get_file(&file_path).unwrap();
let has_alias =
file.alias_map.contains_key("Map") || file.alias_map.values().any(|v| v == "HashMap");
if has_alias {
assert!(
!refs.is_empty(),
"find_references_for_name('HashMap') should find references via alias 'Map', proves XREF-05"
);
} else {
assert!(
file.references
.iter()
.any(|r| r.kind == ReferenceKind::Import),
"src.rs should have at least one Import reference for use statement"
);
}
}
#[test]
fn test_generic_filter() {
let content = r#"
fn identity<T>(x: T) -> T {
x
}
fn swap<K, V>(k: K, v: V) -> (V, K) {
(v, k)
}
"#;
let (_dir, shared) = build_index(&[("src.rs", content)]);
let index = shared.read();
let filtered = index.find_references_for_name("T", None, false);
assert!(
filtered.is_empty(),
"single-letter generic 'T' should be filtered out by default, proves XREF-06, got {} refs",
filtered.len()
);
let unfiltered = index.find_references_for_name("T", None, true);
let _ = unfiltered; }
#[test]
fn test_enclosing_symbol_tracked() {
let content = r#"
fn outer() {
inner_call();
}
fn inner() {
another_call();
}
"#;
let (_dir, shared) = build_index(&[("src.rs", content)]);
let index = shared.read();
let file_path = index
.all_files()
.map(|(p, _)| p)
.find(|p| p.ends_with("src.rs"))
.cloned()
.expect("src.rs should be indexed");
let file = index.get_file(&file_path).unwrap();
let call_refs: Vec<_> = file
.references
.iter()
.filter(|r| r.kind == ReferenceKind::Call)
.collect();
assert!(
!call_refs.is_empty(),
"should have call references in src.rs"
);
for r in &call_refs {
assert!(
r.enclosing_symbol_index.is_some(),
"call reference '{}' at line {} should have enclosing_symbol_index, proves XREF-07",
r.name,
r.line_range.0
);
}
}
#[test]
fn test_incremental_xref_update() {
let initial_content = r#"
fn caller() {
original_call();
}
"#;
let updated_content = r#"
fn caller() {
updated_call();
}
"#;
let dir = TempDir::new().expect("failed to create tempdir");
write_file(dir.path(), "src.rs", initial_content);
let shared = LiveIndex::load(dir.path()).expect("LiveIndex::load failed");
{
let index = shared.read();
let refs = index.find_references_for_name("original_call", None, false);
assert!(
!refs.is_empty(),
"initial index should have 'original_call' reference"
);
}
write_file(dir.path(), "src.rs", updated_content);
{
let mut index = shared.write();
index
.reload(dir.path())
.expect("reload should succeed after file change");
}
{
let index = shared.read();
let old_refs = index.find_references_for_name("original_call", None, false);
let new_refs = index.find_references_for_name("updated_call", None, false);
assert!(
old_refs.is_empty(),
"after reload, 'original_call' should be gone from reverse_index, proves XREF-08"
);
assert!(
!new_refs.is_empty(),
"after reload, 'updated_call' should appear in reverse_index, proves XREF-08"
);
}
}
#[test]
fn test_find_dependents_returns_importers() {
let db_content = r#"
pub fn connect() -> bool {
true
}
"#;
let handler_content = r#"
use crate::db;
fn handle_request() {
let _ = db::connect();
}
"#;
let (_dir, shared) = build_index(&[("db.rs", db_content), ("handler.rs", handler_content)]);
let index = shared.read();
let db_path = index
.all_files()
.map(|(p, _)| p)
.find(|p| p.ends_with("db.rs"))
.cloned()
.expect("db.rs should be indexed");
let deps = index.find_dependents_for_file(&db_path);
assert!(
!deps.is_empty(),
"handler.rs should be reported as depending on db.rs, proves TOOL-10"
);
let from_handler = deps.iter().any(|(fp, _)| fp.ends_with("handler.rs"));
assert!(
from_handler,
"dependent should be handler.rs, got: {:?}",
deps.iter().map(|(p, _)| p).collect::<Vec<_>>()
);
}
#[test]
fn test_find_references_formatter_output() {
let content = r#"
fn process(x: i32) -> i32 {
x + 1
}
fn main() {
let _ = process(42);
}
"#;
let (_dir, shared) = build_index(&[("src.rs", content)]);
let index = shared.read();
let result = format::find_references_result(&index, "process", None);
if result.starts_with("No references found") {
return;
}
assert!(
result.contains("references in"),
"formatter should produce header with count, got: {result}"
);
assert!(
result.contains("src.rs"),
"formatter should include file path, got: {result}"
);
}
#[test]
fn test_context_bundle_under_100ms() {
let dir = TempDir::new().expect("failed to create tempdir");
for i in 0..49 {
let content = format!(
r#"fn helper_{i}() {{
target_fn();
}}
"#
);
write_file(dir.path(), &format!("support_{i}.rs"), &content);
}
let target_content = r#"
pub fn target_fn() -> i32 {
42
}
"#;
write_file(dir.path(), "target.rs", target_content);
let shared = LiveIndex::load(dir.path()).expect("LiveIndex::load failed");
let index = shared.read();
assert_eq!(
index.file_count(),
50,
"should have 50 indexed files for the performance test"
);
let target_path = index
.all_files()
.map(|(p, _)| p)
.find(|p| p.ends_with("target.rs"))
.cloned()
.expect("target.rs should be indexed");
let start = Instant::now();
let result = format::context_bundle_result(&index, &target_path, "target_fn", None);
let elapsed = start.elapsed();
assert!(
elapsed.as_millis() < 100,
"context_bundle_result should respond in < 100ms, took {}ms, proves TOOL-11 (roadmap criterion 4)",
elapsed.as_millis()
);
assert!(
!result.starts_with("Index"),
"result should not be a guard message, got: {result}"
);
}