use anyhow::{Context, Result};
use neo4rs::query;
use super::GraphDb;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MatchTier {
ExactFqn,
FqnSuffix,
ExactName,
SignaturePrefix,
Fuzzy,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RootCandidate {
pub uuid: String,
pub name: String,
pub fqn: Option<String>,
pub kind: Option<String>,
pub signature: Option<String>,
pub docstring: Option<String>,
pub file_path: Option<String>,
pub start_line: Option<i64>,
}
impl MatchTier {
pub fn as_str(&self) -> &'static str {
match self {
Self::ExactFqn => "exact_fqn",
Self::FqnSuffix => "fqn_suffix",
Self::ExactName => "exact_name",
Self::SignaturePrefix => "signature_prefix",
Self::Fuzzy => "fuzzy",
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TargetRow {
pub uuid: String,
pub name: String,
pub fqn: String,
pub kind: String,
pub file_path: String,
pub start_line: i64,
}
pub const MIN_FUZZY_LEN: usize = 4;
pub const MAX_TARGETS: usize = 25;
pub fn target_resolution_tiers(name: &str) -> Vec<(MatchTier, &'static str)> {
let mut tiers = Vec::new();
if name.contains('.') || name.contains("::") {
tiers.push((MatchTier::ExactFqn, "target.fqn = $name"));
tiers.push((
MatchTier::FqnSuffix,
"target.fqn ENDS WITH '.' + $name OR target.fqn ENDS WITH '::' + $name",
));
}
tiers.push((MatchTier::ExactName, "target.name = $name"));
if name.contains('(') {
tiers.push((
MatchTier::SignaturePrefix,
"(target.name + COALESCE(target.signature, '')) STARTS WITH $name",
));
}
if name.len() >= MIN_FUZZY_LEN {
tiers.push((
MatchTier::Fuzzy,
"target.fqn CONTAINS $name OR (target.name + COALESCE(target.signature, '')) CONTAINS $name",
));
}
tiers
}
pub fn root_kind_rank(kind: Option<&str>) -> u8 {
let Some(kind) = kind else {
return 4;
};
match kind {
"class" | "interface" | "enum"
| "kotlin_class" | "kotlin_interface" | "kotlin_object"
| "kotlin_companion_object" | "kotlin_enum"
| "rust_struct" | "rust_enum" | "rust_union" | "rust_trait"
| "rust_type_alias"
| "python_class"
| "c_struct" | "cpp_class"
| "groovy_class" | "groovy_interface" | "groovy_trait" | "groovy_enum"
| "csharp_class" | "csharp_interface" | "csharp_struct" | "csharp_record"
| "csharp_enum" | "csharp_delegate" => 0,
"method" | "function"
| "kotlin_function" | "kotlin_method"
| "rust_function" | "rust_method" | "rust_macro_def"
| "rust_impl"
| "python_function" | "python_method"
| "c_function" | "cpp_method"
| "csharp_method" | "csharp_constructor" | "csharp_local_function"
| "csharp_operator" | "csharp_indexer"
| "groovy_method" | "groovy_function"
| "macro_definition"
| "scss_function" | "scss_mixin"
| "vcl_subroutine" | "vcl_builtin_sub"
| "vcc_function" | "vcc_method" => 1,
"constant"
| "kotlin_property"
| "rust_constant" | "rust_static"
| "python_constant"
| "csharp_property" | "csharp_field" | "csharp_constant" | "csharp_event"
| "groovy_property"
| "config_property"
| "helm_value"
| "css_variable"
| "scss_variable" => 2,
"rust_module" | "python_module"
| "cpp_namespace" | "csharp_namespace"
| "markdown_document" | "markdown_section" => 3,
_ => 4,
}
}
pub fn rank_root_candidates(mut candidates: Vec<RootCandidate>) -> Vec<RootCandidate> {
candidates.sort_by(|a, b| {
let ra = root_kind_rank(a.kind.as_deref());
let rb = root_kind_rank(b.kind.as_deref());
ra.cmp(&rb)
.then_with(|| a.file_path.cmp(&b.file_path))
.then_with(|| a.start_line.cmp(&b.start_line))
.then_with(|| a.uuid.cmp(&b.uuid))
});
candidates
}
pub fn relationship_query(rel_label: &str, repo_scoped: bool) -> String {
let repo_filter = if repo_scoped {
"WHERE target.repo_name IN $repo_names AND target.uuid IN $target_uuids"
} else {
"WHERE target.uuid IN $target_uuids"
};
format!(
"MATCH (entity:Entity)-[:{rel_label}]->(target:Entity)
{repo_filter}
RETURN entity.name, entity.kind, entity.file_path, entity.start_line, entity.signature,
target.name AS target_name, target.fqn AS target_fqn,
target.file_path AS target_file_path,
target.start_line AS target_start_line, target.signature AS target_signature
ORDER BY target.fqn, entity.file_path, entity.start_line"
)
}
pub fn overridden_by_query(repo_scoped: bool) -> String {
let repo_filter = if repo_scoped {
"WHERE target.repo_name IN $repo_names AND target.uuid IN $target_uuids"
} else {
"WHERE target.uuid IN $target_uuids"
};
format!(
"MATCH (entity:Entity)-[:OVERRIDES*1..]->(target:Entity)
{repo_filter}
AND entity.uuid <> target.uuid
RETURN DISTINCT entity.name, entity.kind, entity.file_path, entity.start_line, entity.signature,
target.name AS target_name, target.fqn AS target_fqn,
target.file_path AS target_file_path,
target.start_line AS target_start_line, target.signature AS target_signature
ORDER BY target.fqn, entity.file_path, entity.start_line"
)
}
pub fn overrides_query(repo_scoped: bool) -> String {
let repo_filter = if repo_scoped {
"WHERE entity.repo_name IN $repo_names AND entity.uuid IN $target_uuids"
} else {
"WHERE entity.uuid IN $target_uuids"
};
format!(
"MATCH (entity:Entity)-[:OVERRIDES*1..]->(target:Entity)
{repo_filter}
AND entity.uuid <> target.uuid
RETURN DISTINCT target.name AS `entity.name`, target.kind AS `entity.kind`,
target.file_path AS `entity.file_path`, target.start_line AS `entity.start_line`,
target.signature AS `entity.signature`,
entity.name AS target_name, entity.fqn AS target_fqn,
entity.file_path AS target_file_path,
entity.start_line AS target_start_line, entity.signature AS target_signature
ORDER BY entity.file_path, entity.start_line"
)
}
pub fn find_callers_query(repo_names: &[String]) -> String {
if !repo_names.is_empty() {
"MATCH (caller:Entity)-[:CALLS]->(callee:Entity)
WHERE callee.repo_name IN $repo_names
AND (callee.name = $name
OR callee.fqn = $name)
RETURN caller.name, caller.kind, caller.file_path, caller.start_line, caller.signature"
.to_string()
} else {
"MATCH (caller:Entity)-[:CALLS]->(callee:Entity)
WHERE callee.name = $name
OR callee.fqn = $name
RETURN caller.name, caller.kind, caller.file_path, caller.start_line, caller.signature"
.to_string()
}
}
pub fn get_file_entities_query(repo_names: &[String]) -> String {
if repo_names.len() == 1 {
"MATCH (e:Entity {file_path: $file_path, repo_name: $repo_name})
RETURN e.name, e.kind, e.signature, e.docstring, e.start_line, e.decorators
ORDER BY e.start_line"
.to_string()
} else if repo_names.len() > 1 {
"MATCH (e:Entity)
WHERE e.file_path = $file_path AND e.repo_name IN $repo_names
RETURN e.name, e.kind, e.signature, e.docstring, e.start_line, e.decorators
ORDER BY e.start_line"
.to_string()
} else {
"MATCH (e:Entity {file_path: $file_path})
RETURN e.name, e.kind, e.signature, e.docstring, e.start_line, e.decorators
ORDER BY e.start_line"
.to_string()
}
}
pub fn get_file_outgoing_references_query(repo_names: &[String]) -> String {
if repo_names.len() == 1 {
"MATCH (src:Entity {file_path: $file_path, repo_name: $repo_name})
-[r:REFERENCES|CALLS|EXTENDS|IMPLEMENTS]->
(dst:Entity)
WHERE dst.file_path <> $file_path OR NOT dst.repo_name IN $repo_names
RETURN type(r) AS rel,
dst.name AS name,
dst.kind AS kind,
dst.file_path AS file_path,
dst.start_line AS line
ORDER BY rel, name"
.to_string()
} else if repo_names.len() > 1 {
"MATCH (src:Entity)
-[r:REFERENCES|CALLS|EXTENDS|IMPLEMENTS]->
(dst:Entity)
WHERE src.file_path = $file_path AND src.repo_name IN $repo_names
AND (dst.file_path <> $file_path OR NOT dst.repo_name IN $repo_names)
RETURN type(r) AS rel,
dst.name AS name,
dst.kind AS kind,
dst.file_path AS file_path,
dst.start_line AS line
ORDER BY rel, name"
.to_string()
} else {
"MATCH (src:Entity {file_path: $file_path})
-[r:REFERENCES|CALLS|EXTENDS|IMPLEMENTS]->
(dst:Entity)
WHERE dst.file_path <> $file_path
RETURN type(r) AS rel,
dst.name AS name,
dst.kind AS kind,
dst.file_path AS file_path,
dst.start_line AS line
ORDER BY rel, name"
.to_string()
}
}
pub fn find_files_by_suffix_query(suffix_fragment: &str, repo_names: &[String]) -> String {
if !repo_names.is_empty() {
format!(
"MATCH (e:Entity) \
WHERE ({suffix_fragment}) AND e.repo_name IN $repo_names \
RETURN DISTINCT e.file_path AS file_path, e.repo_name AS repo_name \
ORDER BY e.file_path LIMIT 50"
)
} else {
format!(
"MATCH (e:Entity) \
WHERE ({suffix_fragment}) \
RETURN DISTINCT e.file_path AS file_path, e.repo_name AS repo_name \
ORDER BY e.file_path LIMIT 50"
)
}
}
impl GraphDb {
async fn resolve_reference_targets(
&self,
name: &str,
repo_names: &[String],
) -> Result<(Vec<TargetRow>, MatchTier, bool)> {
let tiers = target_resolution_tiers(name);
let repo_clause = if !repo_names.is_empty() {
"target.repo_name IN $repo_names AND "
} else {
""
};
for (tier, predicate) in tiers {
let query_str = format!(
"MATCH (target:Entity)
WHERE {repo_clause}({predicate})
RETURN target.uuid, target.name, target.fqn, target.kind, target.file_path, target.start_line
ORDER BY target.fqn"
);
let mut q = query(&query_str).param("name", name);
if !repo_names.is_empty() {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self.graph.execute(q).await.context(format!(
"Failed to resolve targets for tier {}",
tier.as_str()
))?;
let mut targets = Vec::new();
while let Ok(Some(row)) = rows.next().await {
let uuid = row.get::<String>("target.uuid").unwrap_or_default();
let name = row.get::<String>("target.name").unwrap_or_default();
let fqn = row.get::<String>("target.fqn").unwrap_or_default();
let kind = row.get::<String>("target.kind").unwrap_or_default();
let file_path = row.get::<String>("target.file_path").unwrap_or_default();
let start_line = row.get::<i64>("target.start_line").unwrap_or(0);
targets.push(TargetRow {
uuid,
name,
fqn,
kind,
file_path,
start_line,
});
}
if !targets.is_empty() {
let truncated = targets.len() > MAX_TARGETS;
if truncated {
targets.truncate(MAX_TARGETS);
}
return Ok((targets, tier, truncated));
}
}
let default_tier = if name.len() >= MIN_FUZZY_LEN {
MatchTier::Fuzzy
} else {
MatchTier::ExactName
};
Ok((Vec::new(), default_tier, false))
}
pub(crate) async fn resolve_subgraph_root(
&self,
name: &str,
repo_name: &str,
) -> Result<Option<(RootCandidate, MatchTier, usize)>> {
let tiers = target_resolution_tiers(name);
for (tier, predicate) in tiers {
let query_str = format!(
"MATCH (target:Entity)
WHERE target.repo_name = $repo_name AND ({predicate})
RETURN target.uuid, target.name, target.fqn, target.kind,
target.signature, target.docstring, target.file_path, target.start_line
ORDER BY target.fqn
LIMIT 25"
);
let q = query(&query_str)
.param("name", name)
.param("repo_name", repo_name);
let mut rows = self.graph.execute(q).await.context(format!(
"Failed to resolve subgraph root for tier {}",
tier.as_str()
))?;
let mut candidates = Vec::new();
while let Ok(Some(row)) = rows.next().await {
let uuid = row.get::<String>("target.uuid").unwrap_or_default();
let nm = row.get::<String>("target.name").unwrap_or_default();
let fqn = row.get::<String>("target.fqn").ok();
let kind = row.get::<String>("target.kind").ok();
let signature = row.get::<String>("target.signature").ok();
let docstring = row.get::<String>("target.docstring").ok();
let file_path = row.get::<String>("target.file_path").ok();
let start_line = row.get::<i64>("target.start_line").ok();
candidates.push(RootCandidate {
uuid,
name: nm,
fqn,
kind,
signature,
docstring,
file_path,
start_line,
});
}
if !candidates.is_empty() {
let total = candidates.len();
let ranked = rank_root_candidates(candidates);
let winner = ranked.into_iter().next().expect("ranked non-empty");
return Ok(Some((winner, tier, total)));
}
}
Ok(None)
}
async fn collect_reference_rows(
&self,
query_str: &str,
target_uuids: &[String],
repo_names: &[String],
label: &str,
) -> Result<Vec<serde_json::Value>> {
let mut q = query(query_str).param("target_uuids", target_uuids.to_vec());
if !repo_names.is_empty() {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self
.graph
.execute(q)
.await
.context(format!("Failed to query Neo4j for {label} relationships"))?;
let mut collected = Vec::new();
while let Ok(Some(row)) = rows.next().await {
collected.push(parse_reference_row(row));
}
Ok(collected)
}
}
#[expect(
async_fn_in_trait,
reason = "async trait method is required for the db interfaces"
)]
pub trait QueryExt {
async fn get_entities_with_dependencies(
&self,
uuids: &[String],
repo_names: &[String],
) -> Result<serde_json::Value>;
async fn find_references(
&self,
entity_name: &str,
repo_names: &[String],
) -> Result<serde_json::Value>;
async fn find_callers(
&self,
entity_name: &str,
repo_names: &[String],
) -> Result<serde_json::Value>;
async fn get_file_entities(
&self,
file_path: &str,
repo_names: &[String],
) -> Result<serde_json::Value>;
async fn find_entities_by_name_prefix(
&self,
prefix: &str,
repo_names: &[String],
limit: usize,
) -> Result<serde_json::Value>;
async fn get_file_outgoing_references(
&self,
file_path: &str,
repo_names: &[String],
) -> Result<serde_json::Value>;
async fn find_files_by_suffix(
&self,
suffix_fragment: &str,
repo_names: &[String],
) -> Result<serde_json::Value>;
}
impl QueryExt for GraphDb {
async fn get_entities_with_dependencies(
&self,
uuids: &[String],
repo_names: &[String],
) -> Result<serde_json::Value> {
if uuids.is_empty() {
return Ok(serde_json::json!([]));
}
let mut results = Vec::new();
let repo_clause = if !repo_names.is_empty() {
" AND m.repo_name IN $repo_names"
} else {
""
};
let query_str = format!(
"MATCH (m:Entity) WHERE m.uuid = $uuid{repo_clause}
OPTIONAL MATCH (m)-[:CALLS]->(dep:Entity)
RETURN m.name, m.kind, m.fqn, m.signature, m.docstring, m.file_path,
m.start_line, m.repo_name AS repo_name,
collect(dep.name) as dependencies"
);
for uuid in uuids {
let mut q = query(&query_str).param("uuid", uuid.as_str());
if !repo_names.is_empty() {
q = q.param("repo_names", repo_names.to_vec());
}
let mut row = self
.graph
.execute(q)
.await
.context("Failed to query Neo4j for entity dependencies")?;
if let Ok(Some(row_data)) = row.next().await {
let name = row_data.get::<String>("m.name").ok();
let kind = row_data.get::<String>("m.kind").ok();
let fqn = row_data.get::<String>("m.fqn").ok();
let signature = row_data.get::<String>("m.signature").ok();
let docstring = row_data.get::<String>("m.docstring").ok();
let file_path = row_data.get::<String>("m.file_path").ok();
let start_line = row_data.get::<i64>("m.start_line").ok();
let repo_name = row_data.get::<String>("repo_name").ok();
let dependencies = row_data
.get::<Vec<String>>("dependencies")
.unwrap_or_default();
let entity_json = serde_json::json!({
"uuid": uuid,
"name": name,
"kind": kind,
"fqn": fqn,
"signature": signature,
"docstring": docstring,
"file_path": file_path,
"start_line": start_line,
"repo_name": repo_name,
"dependencies": dependencies,
});
results.push(entity_json);
}
}
Ok(serde_json::json!(results))
}
async fn find_references(
&self,
entity_name: &str,
repo_names: &[String],
) -> Result<serde_json::Value> {
let mut results = serde_json::json!({
"calls": [],
"extends": [],
"implements": [],
"references": [],
"overridden_by": [],
"overrides": []
});
let (targets, tier, truncated) = self
.resolve_reference_targets(entity_name, repo_names)
.await?;
results["resolution"] = serde_json::json!({
"query": entity_name,
"tier": tier,
"fuzzy": matches!(tier, MatchTier::Fuzzy),
"truncated": truncated,
"targets": targets
});
if targets.is_empty() {
return Ok(results);
}
let target_uuids: Vec<String> = targets.iter().map(|t| t.uuid.clone()).collect();
let rel_types = [
("CALLS", "calls"),
("EXTENDS", "extends"),
("IMPLEMENTS", "implements"),
("REFERENCES", "references"),
];
for (rel_label, result_key) in rel_types {
let query_str = relationship_query(rel_label, !repo_names.is_empty());
let rows = self
.collect_reference_rows(&query_str, &target_uuids, repo_names, rel_label)
.await?;
if let Some(arr) = results.get_mut(result_key) {
*arr = serde_json::json!(rows);
}
}
for (result_key, query_str) in [
("overridden_by", overridden_by_query(!repo_names.is_empty())),
("overrides", overrides_query(!repo_names.is_empty())),
] {
let rows = self
.collect_reference_rows(&query_str, &target_uuids, repo_names, result_key)
.await?;
if let Some(arr) = results.get_mut(result_key) {
*arr = serde_json::json!(rows);
}
}
Ok(results)
}
async fn find_callers(
&self,
entity_name: &str,
repo_names: &[String],
) -> Result<serde_json::Value> {
let mut results = Vec::new();
let query_str = find_callers_query(repo_names);
let mut q = query(&query_str).param("name", entity_name);
if !repo_names.is_empty() {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self
.graph
.execute(q)
.await
.context("Failed to query Neo4j for callers")?;
while let Ok(Some(row)) = rows.next().await {
let caller_json = serde_json::json!({
"name": row.get::<String>("caller.name").ok(),
"kind": row.get::<String>("caller.kind").ok(),
"file_path": row.get::<String>("caller.file_path").ok(),
"start_line": row.get::<i64>("caller.start_line").ok(),
"signature": row.get::<String>("caller.signature").ok(),
});
results.push(caller_json);
}
Ok(serde_json::json!(results))
}
async fn get_file_entities(
&self,
file_path: &str,
repo_names: &[String],
) -> Result<serde_json::Value> {
let mut results = Vec::new();
let query_str = get_file_entities_query(repo_names);
let mut q = query(&query_str).param("file_path", file_path);
if repo_names.len() == 1 {
q = q.param("repo_name", repo_names[0].as_str());
} else if repo_names.len() > 1 {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self
.graph
.execute(q)
.await
.context("Failed to query Neo4j for file entities")?;
while let Ok(Some(row)) = rows.next().await {
let decorators = row.get::<Vec<String>>("e.decorators").unwrap_or_default();
let entity_json = serde_json::json!({
"name": row.get::<String>("e.name").ok(),
"kind": row.get::<String>("e.kind").ok(),
"signature": row.get::<String>("e.signature").ok(),
"docstring": row.get::<String>("e.docstring").ok(),
"start_line": row.get::<i64>("e.start_line").ok(),
"decorators": decorators,
});
results.push(entity_json);
}
Ok(serde_json::json!(results))
}
async fn find_entities_by_name_prefix(
&self,
prefix: &str,
repo_names: &[String],
limit: usize,
) -> Result<serde_json::Value> {
let repo_clause = if !repo_names.is_empty() {
" AND m.repo_name IN $repo_names"
} else {
""
};
let query_str = format!(
"MATCH (m:Entity)
WHERE toLower(m.name) STARTS WITH toLower($prefix){repo_clause}
OPTIONAL MATCH (m)-[:CALLS]->(dep:Entity)
RETURN m.uuid AS uuid, m.name, m.kind, m.fqn, m.signature, m.docstring,
m.file_path, m.start_line, m.repo_name AS repo_name,
collect(dep.name) as dependencies
ORDER BY CASE WHEN toLower(m.name) = toLower($prefix) THEN 0 ELSE 1 END,
size(m.name),
m.fqn,
m.uuid
LIMIT $limit"
);
let mut q = query(&query_str)
.param("prefix", prefix)
.param("limit", limit as i64);
if !repo_names.is_empty() {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self
.graph
.execute(q)
.await
.context("Failed to query Neo4j for entities by name prefix")?;
let mut results = Vec::new();
while let Ok(Some(row)) = rows.next().await {
let entity_json = serde_json::json!({
"uuid": row.get::<String>("uuid").ok(),
"name": row.get::<String>("m.name").ok(),
"kind": row.get::<String>("m.kind").ok(),
"fqn": row.get::<String>("m.fqn").ok(),
"signature": row.get::<String>("m.signature").ok(),
"docstring": row.get::<String>("m.docstring").ok(),
"file_path": row.get::<String>("m.file_path").ok(),
"start_line": row.get::<i64>("m.start_line").ok(),
"repo_name": row.get::<String>("repo_name").ok(),
"dependencies": row.get::<Vec<String>>("dependencies").unwrap_or_default(),
});
results.push(entity_json);
}
Ok(serde_json::json!(results))
}
async fn get_file_outgoing_references(
&self,
file_path: &str,
repo_names: &[String],
) -> Result<serde_json::Value> {
let mut results = Vec::new();
let query_str = get_file_outgoing_references_query(repo_names);
let mut q = query(&query_str).param("file_path", file_path);
if repo_names.len() == 1 {
q = q
.param("repo_name", repo_names[0].as_str())
.param("repo_names", repo_names.to_vec());
} else if repo_names.len() > 1 {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self
.graph
.execute(q)
.await
.context("Failed to query Neo4j for file outgoing references")?;
while let Ok(Some(row)) = rows.next().await {
let entry = serde_json::json!({
"rel": row.get::<String>("rel").ok(),
"name": row.get::<String>("name").ok(),
"kind": row.get::<String>("kind").ok(),
"file_path": row.get::<String>("file_path").ok(),
"line": row.get::<i64>("line").ok(),
});
results.push(entry);
}
Ok(serde_json::json!(results))
}
async fn find_files_by_suffix(
&self,
suffix_fragment: &str,
repo_names: &[String],
) -> Result<serde_json::Value> {
let query_str = find_files_by_suffix_query(suffix_fragment, repo_names);
let mut q = query(&query_str);
if !repo_names.is_empty() {
q = q.param("repo_names", repo_names.to_vec());
}
let mut rows = self
.graph
.execute(q)
.await
.context("Failed to query Neo4j for files by suffix")?;
let mut results = Vec::new();
while let Ok(Some(row)) = rows.next().await {
results.push(serde_json::json!({
"file_path": row.get::<String>("file_path").ok(),
"repo_name": row.get::<String>("repo_name").ok(),
}));
}
Ok(serde_json::json!(results))
}
}
fn parse_reference_row(row: neo4rs::Row) -> serde_json::Value {
serde_json::json!({
"name": row.get::<String>("entity.name").ok(),
"kind": row.get::<String>("entity.kind").ok(),
"file_path": row.get::<String>("entity.file_path").ok(),
"start_line": row.get::<i64>("entity.start_line").ok(),
"signature": row.get::<String>("entity.signature").ok(),
"target_name": row.get::<String>("target_name").ok(),
"target_fqn": row.get::<String>("target_fqn").ok(),
"target_file_path": row.get::<String>("target_file_path").ok(),
"target_start_line": row.get::<i64>("target_start_line").ok(),
"target_signature": row.get::<String>("target_signature").ok(),
})
}
#[cfg(test)]
mod tests {
use super::super::GraphDb;
use super::QueryExt;
use crate::db::graph::connection::ConnectExt;
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_get_entities_with_dependencies_empty() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db.get_entities_with_dependencies(&[], &[]).await;
assert!(result.is_ok());
let json = result.unwrap();
assert!(json.is_array());
assert_eq!(json.as_array().unwrap().len(), 0);
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_get_entities_with_dependencies() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let uuids = vec!["550e8400-e29b-41d4-a716-446655440000".to_string()];
let result = graph_db
.get_entities_with_dependencies(&uuids, &["test-repo".to_string()])
.await;
assert!(result.is_ok());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_find_references() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db.find_references("nonexistent_entity", &[]).await;
assert!(result.is_ok());
let json = result.unwrap();
assert!(json.is_object());
assert!(json.get("calls").is_some());
assert!(json.get("extends").is_some());
assert!(json.get("implements").is_some());
assert!(json.get("references").is_some());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_find_references_with_repo() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db
.find_references("nonexistent_entity", &["test-repo".to_string()])
.await;
assert!(result.is_ok());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_find_callers() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db.find_callers("nonexistent_entity", &[]).await;
assert!(result.is_ok());
let json = result.unwrap();
assert!(json.is_array());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_find_callers_with_repo() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db
.find_callers("nonexistent_entity", &["test-repo".to_string()])
.await;
assert!(result.is_ok());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_get_file_entities() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db
.get_file_entities("/test/path/File.java", &[])
.await;
assert!(result.is_ok());
let json = result.unwrap();
assert!(json.is_array());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn test_get_file_entities_with_repo() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let result = graph_db
.get_file_entities("/test/path/File.java", &["test-repo".to_string()])
.await;
assert!(result.is_ok());
}
#[ignore = "requires local Neo4j instance running on bolt://localhost:7687"]
#[tokio::test]
async fn param_binding_uses_repo_names_list() {
let graph_db = GraphDb::connect("bolt://localhost:7687", "neo4j", "password")
.await
.expect("Failed to connect to Neo4j");
let rows = graph_db
.collect_reference_rows(
"MATCH (target:Entity) WHERE target.repo_name IN $repo_names RETURN target.name AS entity.name",
&["uuid-1".to_string()],
&["repo1".to_string(), "repo2".to_string()],
"test",
)
.await;
assert!(rows.is_ok());
}
use super::{
MatchTier, RootCandidate, find_callers_query, find_files_by_suffix_query,
get_file_entities_query, get_file_outgoing_references_query, overridden_by_query,
overrides_query, rank_root_candidates, relationship_query, root_kind_rank,
target_resolution_tiers,
};
#[test]
fn test_tier_ladder_order_for_plain_name() {
let tiers = target_resolution_tiers("Offline");
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(mapped, vec![MatchTier::ExactName, MatchTier::Fuzzy]);
let tiers = target_resolution_tiers("Off");
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(mapped, vec![MatchTier::ExactName]);
}
#[test]
fn test_tier_ladder_includes_fqn_suffix_for_qualified_name() {
for name in ["GestureOwner.Off", "Config::load"] {
let tiers = target_resolution_tiers(name);
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(
mapped,
vec![
MatchTier::ExactFqn,
MatchTier::FqnSuffix,
MatchTier::ExactName,
MatchTier::Fuzzy
],
"unexpected ladder for `{name}`"
);
}
}
#[test]
fn test_tier_ladder_includes_signature_prefix_when_parenthesised() {
let tiers = target_resolution_tiers("accept(List");
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(
mapped,
vec![
MatchTier::ExactName,
MatchTier::SignaturePrefix,
MatchTier::Fuzzy
]
);
}
#[test]
fn test_tier_ladder_omits_fuzzy_for_short_names() {
let tiers = target_resolution_tiers("Id");
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(mapped, vec![MatchTier::ExactName]);
}
#[test]
fn test_dotted_query_starts_with_exact_fqn() {
let tiers = target_resolution_tiers("Foo.bar");
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(mapped[0], MatchTier::ExactFqn);
let tiers = target_resolution_tiers("Foo::bar");
let mapped: Vec<MatchTier> = tiers.iter().map(|(t, _)| *t).collect();
assert_eq!(mapped[0], MatchTier::ExactFqn);
}
#[test]
fn test_fqn_suffix_predicate_is_separator_anchored() {
let tiers = target_resolution_tiers("GestureOwner.Off");
let fqn_suffix_pred = tiers
.iter()
.find(|(t, _)| *t == MatchTier::FqnSuffix)
.unwrap()
.1;
assert!(fqn_suffix_pred.contains("ENDS WITH '.' + $name"));
assert!(fqn_suffix_pred.contains("ENDS WITH '::' + $name"));
assert!(!fqn_suffix_pred.contains("CONTAINS"));
}
#[test]
fn test_signature_predicate_is_prefix_anchored() {
let tiers = target_resolution_tiers("accept(List");
let sig_pred = tiers
.iter()
.find(|(t, _)| *t == MatchTier::SignaturePrefix)
.unwrap()
.1;
assert!(sig_pred.contains("STARTS WITH $name"));
assert!(!sig_pred.contains("CONTAINS"));
}
#[test]
fn test_relationship_query_matches_on_uuid_set() {
let query_str = relationship_query("CALLS", false);
assert!(query_str.contains("target.uuid IN $target_uuids"));
assert!(query_str.contains("ORDER BY target.fqn"));
assert!(!query_str.contains("$repo_names"));
}
#[test]
fn test_relationship_query_repo_scoped_variant() {
let query_str = relationship_query("CALLS", true);
assert!(query_str.contains("target.repo_name IN $repo_names"));
assert!(query_str.contains("target.uuid IN $target_uuids"));
}
#[test]
fn test_overridden_by_query_unscoped() {
let query_str = overridden_by_query(false);
assert!(query_str.contains("target.uuid IN $target_uuids"));
assert!(!query_str.contains("$repo_names"));
}
#[test]
fn test_overridden_by_query_repo_scoped() {
let query_str = overridden_by_query(true);
assert!(query_str.contains("target.repo_name IN $repo_names"));
assert!(query_str.contains("target.uuid IN $target_uuids"));
}
#[test]
fn test_overrides_query_unscoped() {
let query_str = overrides_query(false);
assert!(query_str.contains("entity.uuid IN $target_uuids"));
assert!(!query_str.contains("$repo_names"));
}
#[test]
fn test_overrides_query_repo_scoped() {
let query_str = overrides_query(true);
assert!(query_str.contains("entity.repo_name IN $repo_names"));
assert!(query_str.contains("entity.uuid IN $target_uuids"));
}
#[test]
fn test_find_callers_query_unscoped() {
let query_str = find_callers_query(&[]);
assert!(!query_str.contains("$repo_names"));
}
#[test]
fn test_find_callers_query_repo_scoped() {
let query_str = find_callers_query(&["a".to_string()]);
assert!(query_str.contains("callee.repo_name IN $repo_names"));
}
#[test]
fn test_get_file_entities_query_unscoped() {
let query_str = get_file_entities_query(&[]);
assert!(query_str.contains("MATCH (e:Entity {file_path: $file_path})"));
assert!(!query_str.contains("repo_name"));
}
#[test]
fn test_get_file_entities_query_single_repo() {
let query_str = get_file_entities_query(&["repo-a".to_string()]);
assert!(
query_str.contains("MATCH (e:Entity {file_path: $file_path, repo_name: $repo_name})")
);
}
#[test]
fn test_get_file_entities_query_multi_repo() {
let query_str = get_file_entities_query(&["repo-a".to_string(), "repo-b".to_string()]);
assert!(
query_str.contains("WHERE e.file_path = $file_path AND e.repo_name IN $repo_names")
);
}
#[test]
fn test_get_file_outgoing_references_query_unscoped() {
let query_str = get_file_outgoing_references_query(&[]);
assert!(query_str.contains("WHERE dst.file_path <> $file_path"));
assert!(!query_str.contains("$repo_names"));
}
#[test]
fn test_get_file_outgoing_references_query_repo_scoped() {
let query_str = get_file_outgoing_references_query(&["repo-a".to_string()]);
assert!(query_str.contains("NOT dst.repo_name IN $repo_names"));
}
#[test]
fn test_find_files_by_suffix_query_unscoped() {
let query_str = find_files_by_suffix_query("ENDS WITH '/Cargo.toml'", &[]);
assert!(!query_str.contains("$repo_names"));
}
#[test]
fn test_find_files_by_suffix_query_repo_scoped() {
let query_str =
find_files_by_suffix_query("ENDS WITH '/Cargo.toml'", &["repo-a".to_string()]);
assert!(query_str.contains("e.repo_name IN $repo_names"));
}
#[test]
fn test_root_kind_rank_prefers_type_declarations_over_callables() {
assert!(root_kind_rank(Some("csharp_class")) < root_kind_rank(Some("csharp_constructor")));
assert!(root_kind_rank(Some("class")) < root_kind_rank(Some("method")));
assert!(root_kind_rank(Some("class")) < root_kind_rank(Some("function")));
}
#[test]
fn test_root_kind_rank_containers_rank_below_types() {
assert!(root_kind_rank(Some("csharp_class")) < root_kind_rank(Some("csharp_namespace")));
assert!(root_kind_rank(Some("rust_struct")) < root_kind_rank(Some("rust_module")));
assert!(root_kind_rank(Some("python_class")) < root_kind_rank(Some("python_module")));
assert!(root_kind_rank(Some("cpp_class")) < root_kind_rank(Some("cpp_namespace")));
}
#[test]
fn test_root_kind_rank_handles_missing_kind() {
assert_eq!(root_kind_rank(None), 4);
}
#[test]
fn test_root_kind_rank_is_total_over_known_kinds() {
use crate::models::EntityKind;
let kinds = [
EntityKind::Class,
EntityKind::Interface,
EntityKind::Method,
EntityKind::Function,
EntityKind::Constant,
EntityKind::Enum,
EntityKind::KotlinClass,
EntityKind::KotlinInterface,
EntityKind::KotlinObject,
EntityKind::KotlinCompanionObject,
EntityKind::KotlinFunction,
EntityKind::KotlinMethod,
EntityKind::KotlinProperty,
EntityKind::KotlinEnum,
EntityKind::RustStruct,
EntityKind::RustEnum,
EntityKind::RustUnion,
EntityKind::RustTrait,
EntityKind::RustImpl,
EntityKind::RustFunction,
EntityKind::RustMethod,
EntityKind::RustMacroDef,
EntityKind::RustTypeAlias,
EntityKind::RustConstant,
EntityKind::RustStatic,
EntityKind::RustModule,
EntityKind::PythonClass,
EntityKind::PythonFunction,
EntityKind::PythonMethod,
EntityKind::PythonModule,
EntityKind::PythonConstant,
EntityKind::CStruct,
EntityKind::CFunction,
EntityKind::CppClass,
EntityKind::CppMethod,
EntityKind::CppNamespace,
EntityKind::MacroDefinition,
EntityKind::CSharpClass,
EntityKind::CSharpInterface,
EntityKind::CSharpStruct,
EntityKind::CSharpRecord,
EntityKind::CSharpEnum,
EntityKind::CSharpMethod,
EntityKind::CSharpConstructor,
EntityKind::CSharpProperty,
EntityKind::CSharpField,
EntityKind::CSharpConstant,
EntityKind::CSharpDelegate,
EntityKind::CSharpEvent,
EntityKind::CSharpIndexer,
EntityKind::CSharpOperator,
EntityKind::CSharpNamespace,
EntityKind::CSharpLocalFunction,
EntityKind::GroovyClass,
EntityKind::GroovyInterface,
EntityKind::GroovyTrait,
EntityKind::GroovyMethod,
EntityKind::GroovyFunction,
EntityKind::GroovyEnum,
EntityKind::GroovyProperty,
EntityKind::BuildDependency,
EntityKind::BuildPlugin,
EntityKind::ProjectIdentity,
EntityKind::MarkdownDocument,
EntityKind::MarkdownSection,
EntityKind::ConfigProperty,
];
for k in kinds {
let s = k.to_string();
let rank = root_kind_rank(Some(&s));
assert!(rank <= 4, "rank for {s} should be <= 4, got {rank}");
}
}
#[test]
fn test_rank_root_candidates_prefers_type_over_homonym() {
let candidates = vec![
RootCandidate {
uuid: "uuid-constructor".to_string(),
name: "UserService".to_string(),
fqn: Some("CodeMap.Services.UserService.UserService".to_string()),
kind: Some("csharp_constructor".to_string()),
signature: None,
docstring: None,
file_path: Some("Services/UserService.cs".to_string()),
start_line: Some(18),
},
RootCandidate {
uuid: "uuid-class".to_string(),
name: "UserService".to_string(),
fqn: Some("CodeMap.Services.UserService".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("Services/UserService.cs".to_string()),
start_line: Some(12),
},
];
let ranked = rank_root_candidates(candidates);
assert_eq!(ranked.len(), 2);
assert_eq!(ranked[0].kind.as_deref(), Some("csharp_class"));
assert_eq!(ranked[1].kind.as_deref(), Some("csharp_constructor"));
}
#[test]
fn test_rank_root_candidates_tie_breaks_by_file_then_line_then_uuid() {
let a = RootCandidate {
uuid: "uuid-a".to_string(),
name: "Config".to_string(),
fqn: Some("a.Config".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("src/z/config.cs".to_string()),
start_line: Some(10),
};
let b = RootCandidate {
uuid: "uuid-b".to_string(),
name: "Config".to_string(),
fqn: Some("a.Config".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("src/a/config.cs".to_string()),
start_line: Some(50),
};
let ranked = rank_root_candidates(vec![a, b]);
assert_eq!(ranked[0].file_path.as_deref(), Some("src/a/config.cs"));
assert_eq!(ranked[1].file_path.as_deref(), Some("src/z/config.cs"));
let c = RootCandidate {
uuid: "uuid-c".to_string(),
name: "Config".to_string(),
fqn: Some("a.Config".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("src/a/config.cs".to_string()),
start_line: Some(100),
};
let d = RootCandidate {
uuid: "uuid-d".to_string(),
name: "Config".to_string(),
fqn: Some("a.Config".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("src/a/config.cs".to_string()),
start_line: Some(20),
};
let ranked = rank_root_candidates(vec![c, d]);
assert_eq!(ranked[0].start_line, Some(20));
assert_eq!(ranked[1].start_line, Some(100));
let e = RootCandidate {
uuid: "uuid-zzz".to_string(),
name: "Config".to_string(),
fqn: Some("a.Config".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("src/a/config.cs".to_string()),
start_line: Some(20),
};
let f = RootCandidate {
uuid: "uuid-aaa".to_string(),
name: "Config".to_string(),
fqn: Some("a.Config".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("src/a/config.cs".to_string()),
start_line: Some(20),
};
let ranked = rank_root_candidates(vec![e, f]);
assert_eq!(ranked[0].uuid, "uuid-aaa");
assert_eq!(ranked[1].uuid, "uuid-zzz");
}
#[test]
fn test_rank_root_candidates_is_stable_for_equal_keys() {
let make = |uuid: &str| RootCandidate {
uuid: uuid.to_string(),
name: "Same".to_string(),
fqn: Some("x.Same".to_string()),
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("file.cs".to_string()),
start_line: Some(1),
};
let input = vec![make("uuid-1"), make("uuid-2"), make("uuid-3")];
let ranked = rank_root_candidates(input);
assert_eq!(
ranked.iter().map(|c| c.uuid.as_str()).collect::<Vec<_>>(),
vec!["uuid-1", "uuid-2", "uuid-3"],
"stable sort must preserve input order for equal keys"
);
}
#[test]
fn test_rank_root_candidates_handles_missing_fields() {
let a = RootCandidate {
uuid: "uuid-no-kind".to_string(),
name: "M".to_string(),
fqn: None,
kind: None,
signature: None,
docstring: None,
file_path: Some("a/x.cs".to_string()),
start_line: Some(10),
};
let b = RootCandidate {
uuid: "uuid-class".to_string(),
name: "M".to_string(),
fqn: None,
kind: Some("csharp_class".to_string()),
signature: None,
docstring: None,
file_path: Some("a/x.cs".to_string()),
start_line: Some(10),
};
let ranked = rank_root_candidates(vec![a, b]);
assert_eq!(ranked[0].kind.as_deref(), Some("csharp_class"));
assert_eq!(ranked[1].kind, None);
}
}