use serde::{Deserialize, Serialize};
use crate::db::graph::MatchTier;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_subgraph_result_serialises_root_resolution_when_present() {
let chosen = RootCandidateLite {
uuid: "u1".to_string(),
name: "UserService".to_string(),
fqn: Some("MyApp.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 result = SubgraphResult {
root_id: Some("u1".to_string()),
nodes: vec![],
edges: vec![],
truncated: false,
total_nodes_found: 0,
root_resolution: Some(RootResolution {
query: "UserService".to_string(),
tier: MatchTier::ExactName,
total_candidates: 2,
chosen: chosen.clone(),
candidates: vec![chosen],
}),
};
let json = serde_json::to_string(&result).unwrap();
assert!(json.contains("\"root_resolution\""), "got: {json}");
assert!(json.contains("\"tier\":\"exact_name\""), "got: {json}");
assert!(json.contains("\"total_candidates\":2"), "got: {json}");
assert!(json.contains("\"query\":\"UserService\""), "got: {json}");
}
#[test]
fn test_subgraph_result_omits_root_resolution_when_none() {
let result = SubgraphResult {
root_id: Some("u1".to_string()),
nodes: vec![],
edges: vec![],
truncated: false,
total_nodes_found: 0,
root_resolution: None,
};
let json = serde_json::to_string(&result).unwrap();
assert!(
!json.contains("root_resolution"),
"root_resolution should be omitted when None, got: {json}"
);
}
#[test]
fn test_subgraph_result_deserializes_without_root_resolution() {
let legacy_json = r#"{
"root_id": "u1",
"nodes": [],
"edges": [],
"truncated": false,
"total_nodes_found": 0
}"#;
let result: SubgraphResult = serde_json::from_str(legacy_json).unwrap();
assert!(result.root_resolution.is_none());
assert_eq!(result.root_id.as_deref(), Some("u1"));
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SubgraphDirection {
Outgoing,
Incoming,
#[default]
Both,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubgraphNode {
pub uuid: String,
pub name: String,
pub kind: Option<String>,
pub fqn: Option<String>,
pub signature: Option<String>,
pub docstring: Option<String>,
pub file_path: Option<String>,
pub start_line: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubgraphEdge {
pub source_uuid: String,
pub target_uuid: String,
pub relationship: String,
}
#[derive(Debug, Clone)]
pub struct SubgraphQueryOptions<'a> {
pub entity_name: &'a str,
pub repo_name: &'a str,
pub depth: u32,
pub relationships: &'a [&'a str],
pub direction: SubgraphDirection,
pub max_nodes: usize,
pub entity_uuid: Option<&'a str>,
pub visible_kinds: Option<&'a [&'a str]>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RootResolution {
pub query: String,
pub tier: MatchTier,
pub total_candidates: usize,
pub chosen: RootCandidateLite,
pub candidates: Vec<RootCandidateLite>,
}
pub type RootCandidateLite = crate::db::graph::RootCandidate;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubgraphResult {
pub root_id: Option<String>,
pub nodes: Vec<SubgraphNode>,
pub edges: Vec<SubgraphEdge>,
pub truncated: bool,
pub total_nodes_found: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub root_resolution: Option<RootResolution>,
}