refactor: modularize codebase and add 404 unit tests (#13)
CI / Format (push) Successful in 4s
CI / Clippy (push) Successful in 4m19s
CI / Security Audit (push) Successful in 1m44s
CI / Tests (push) Successful in 5m15s
CI / Detect Changes (push) Successful in 5s
CI / Deploy Agent (push) Successful in 2s
CI / Deploy Dashboard (push) Successful in 2s
CI / Deploy Docs (push) Has been skipped
CI / Deploy MCP (push) Successful in 2s
CI / Format (push) Successful in 4s
CI / Clippy (push) Successful in 4m19s
CI / Security Audit (push) Successful in 1m44s
CI / Tests (push) Successful in 5m15s
CI / Detect Changes (push) Successful in 5s
CI / Deploy Agent (push) Successful in 2s
CI / Deploy Dashboard (push) Successful in 2s
CI / Deploy Docs (push) Has been skipped
CI / Deploy MCP (push) Successful in 2s
This commit was merged in pull request #13.
This commit is contained in:
@@ -94,3 +94,64 @@ fn build_context_header(file_path: &str, qualified_name: &str, kind: &str) -> St
|
||||
format!("// {file_path} | {kind}")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_build_context_header_with_parent() {
|
||||
let result =
|
||||
build_context_header("src/main.rs", "src/main.rs::MyStruct::my_method", "method");
|
||||
assert_eq!(result, "// src/main.rs | method in src/main.rs::MyStruct");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_context_header_top_level() {
|
||||
let result = build_context_header("src/lib.rs", "main", "function");
|
||||
assert_eq!(result, "// src/lib.rs | function");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_context_header_single_parent() {
|
||||
let result = build_context_header("src/lib.rs", "src/lib.rs::do_stuff", "function");
|
||||
assert_eq!(result, "// src/lib.rs | function in src/lib.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_context_header_deep_nesting() {
|
||||
let result = build_context_header(
|
||||
"src/mod.rs",
|
||||
"src/mod.rs::Outer::Inner::deep_fn",
|
||||
"function",
|
||||
);
|
||||
assert_eq!(
|
||||
result,
|
||||
"// src/mod.rs | function in src/mod.rs::Outer::Inner"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_context_header_empty_strings() {
|
||||
let result = build_context_header("", "", "function");
|
||||
assert_eq!(result, "// | function");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_code_chunk_struct_fields() {
|
||||
let chunk = CodeChunk {
|
||||
qualified_name: "main".to_string(),
|
||||
kind: "function".to_string(),
|
||||
file_path: "src/main.rs".to_string(),
|
||||
start_line: 1,
|
||||
end_line: 10,
|
||||
language: "rust".to_string(),
|
||||
content: "fn main() {}".to_string(),
|
||||
context_header: "// src/main.rs | function".to_string(),
|
||||
token_estimate: 3,
|
||||
};
|
||||
assert_eq!(chunk.start_line, 1);
|
||||
assert_eq!(chunk.end_line, 10);
|
||||
assert_eq!(chunk.language, "rust");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,3 +253,215 @@ fn detect_communities_with_assignment(code_graph: &mut CodeGraph) -> u32 {
|
||||
|
||||
next_id
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use compliance_core::models::graph::{CodeEdgeKind, CodeNode, CodeNodeKind};
|
||||
use petgraph::graph::DiGraph;
|
||||
|
||||
fn make_node(qualified_name: &str, graph_index: u32) -> CodeNode {
|
||||
CodeNode {
|
||||
id: None,
|
||||
repo_id: "test".to_string(),
|
||||
graph_build_id: "build1".to_string(),
|
||||
qualified_name: qualified_name.to_string(),
|
||||
name: qualified_name.to_string(),
|
||||
kind: CodeNodeKind::Function,
|
||||
file_path: "test.rs".to_string(),
|
||||
start_line: 1,
|
||||
end_line: 10,
|
||||
language: "rust".to_string(),
|
||||
community_id: None,
|
||||
is_entry_point: false,
|
||||
graph_index: Some(graph_index),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_empty_code_graph() -> CodeGraph {
|
||||
CodeGraph {
|
||||
graph: DiGraph::new(),
|
||||
node_map: HashMap::new(),
|
||||
nodes: Vec::new(),
|
||||
edges: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_communities_empty_graph() {
|
||||
let cg = make_empty_code_graph();
|
||||
assert_eq!(detect_communities(&cg), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_communities_single_node_no_edges() {
|
||||
let mut graph = DiGraph::new();
|
||||
let idx = graph.add_node("a".to_string());
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), idx);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![make_node("a", 0)],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
// Single node with no edges => 1 community (itself)
|
||||
assert_eq!(detect_communities(&cg), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_communities_isolated_nodes() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let c = graph.add_node("c".to_string());
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
node_map.insert("c".to_string(), c);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![make_node("a", 0), make_node("b", 1), make_node("c", 2)],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
// 3 isolated nodes => 3 communities
|
||||
assert_eq!(detect_communities(&cg), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_communities_fully_connected() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let c = graph.add_node("c".to_string());
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, c, CodeEdgeKind::Calls);
|
||||
graph.add_edge(c, a, CodeEdgeKind::Calls);
|
||||
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
node_map.insert("c".to_string(), c);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![make_node("a", 0), make_node("b", 1), make_node("c", 2)],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let num = detect_communities(&cg);
|
||||
// Fully connected triangle should converge to 1 community
|
||||
assert!(num >= 1);
|
||||
assert!(num <= 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_communities_two_clusters() {
|
||||
// Two separate triangles connected by a single weak edge
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let c = graph.add_node("c".to_string());
|
||||
let d = graph.add_node("d".to_string());
|
||||
let e = graph.add_node("e".to_string());
|
||||
let f = graph.add_node("f".to_string());
|
||||
|
||||
// Cluster 1: a-b-c fully connected
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, a, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, c, CodeEdgeKind::Calls);
|
||||
graph.add_edge(c, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(a, c, CodeEdgeKind::Calls);
|
||||
graph.add_edge(c, a, CodeEdgeKind::Calls);
|
||||
|
||||
// Cluster 2: d-e-f fully connected
|
||||
graph.add_edge(d, e, CodeEdgeKind::Calls);
|
||||
graph.add_edge(e, d, CodeEdgeKind::Calls);
|
||||
graph.add_edge(e, f, CodeEdgeKind::Calls);
|
||||
graph.add_edge(f, e, CodeEdgeKind::Calls);
|
||||
graph.add_edge(d, f, CodeEdgeKind::Calls);
|
||||
graph.add_edge(f, d, CodeEdgeKind::Calls);
|
||||
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
node_map.insert("c".to_string(), c);
|
||||
node_map.insert("d".to_string(), d);
|
||||
node_map.insert("e".to_string(), e);
|
||||
node_map.insert("f".to_string(), f);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![
|
||||
make_node("a", 0),
|
||||
make_node("b", 1),
|
||||
make_node("c", 2),
|
||||
make_node("d", 3),
|
||||
make_node("e", 4),
|
||||
make_node("f", 5),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let num = detect_communities(&cg);
|
||||
// Two disconnected clusters should yield 2 communities
|
||||
assert_eq!(num, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_communities_assigns_ids() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
|
||||
let mut cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![make_node("a", 0), make_node("b", 1)],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let count = apply_communities(&mut cg);
|
||||
assert!(count >= 1);
|
||||
// All nodes should have a community_id assigned
|
||||
for node in &cg.nodes {
|
||||
assert!(node.community_id.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_communities_empty() {
|
||||
let mut cg = make_empty_code_graph();
|
||||
assert_eq!(apply_communities(&mut cg), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_communities_isolated_nodes_get_own_community() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
|
||||
let mut cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![make_node("a", 0), make_node("b", 1)],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let count = apply_communities(&mut cg);
|
||||
assert_eq!(count, 2);
|
||||
// Each isolated node should be in a different community
|
||||
let c0 = cg.nodes[0].community_id.unwrap();
|
||||
let c1 = cg.nodes[1].community_id.unwrap();
|
||||
assert_ne!(c0, c1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,3 +172,185 @@ impl GraphEngine {
|
||||
ImpactAnalyzer::new(code_graph)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use compliance_core::models::graph::{CodeEdgeKind, CodeNode, CodeNodeKind};
|
||||
|
||||
fn make_node(qualified_name: &str) -> CodeNode {
|
||||
CodeNode {
|
||||
id: None,
|
||||
repo_id: "test".to_string(),
|
||||
graph_build_id: "build1".to_string(),
|
||||
qualified_name: qualified_name.to_string(),
|
||||
name: qualified_name
|
||||
.split("::")
|
||||
.last()
|
||||
.unwrap_or(qualified_name)
|
||||
.to_string(),
|
||||
kind: CodeNodeKind::Function,
|
||||
file_path: "src/main.rs".to_string(),
|
||||
start_line: 1,
|
||||
end_line: 10,
|
||||
language: "rust".to_string(),
|
||||
community_id: None,
|
||||
is_entry_point: false,
|
||||
graph_index: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_test_node_map(names: &[&str]) -> HashMap<String, NodeIndex> {
|
||||
let mut graph: DiGraph<String, String> = DiGraph::new();
|
||||
let mut map = HashMap::new();
|
||||
for name in names {
|
||||
let idx = graph.add_node(name.to_string());
|
||||
map.insert(name.to_string(), idx);
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_direct_match() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = build_test_node_map(&["src/main.rs::foo", "src/main.rs::bar"]);
|
||||
let result = engine.resolve_edge_target("src/main.rs::foo", &node_map);
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap(), node_map["src/main.rs::foo"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_short_name_match() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = build_test_node_map(&["src/main.rs::foo", "src/main.rs::bar"]);
|
||||
let result = engine.resolve_edge_target("foo", &node_map);
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap(), node_map["src/main.rs::foo"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_method_match() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = build_test_node_map(&["src/main.rs::MyStruct::do_thing"]);
|
||||
let result = engine.resolve_edge_target("do_thing", &node_map);
|
||||
assert!(result.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_self_method() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = build_test_node_map(&["src/main.rs::MyStruct::process"]);
|
||||
let result = engine.resolve_edge_target("self.process", &node_map);
|
||||
assert!(result.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_no_match() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = build_test_node_map(&["src/main.rs::foo"]);
|
||||
let result = engine.resolve_edge_target("nonexistent", &node_map);
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_empty_map() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = HashMap::new();
|
||||
let result = engine.resolve_edge_target("anything", &node_map);
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_resolve_edge_target_dot_notation() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let node_map = build_test_node_map(&["src/app.js.handler"]);
|
||||
let result = engine.resolve_edge_target("handler", &node_map);
|
||||
assert!(result.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_petgraph_empty() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let output = ParseOutput::default();
|
||||
let code_graph = engine.build_petgraph(output).unwrap();
|
||||
assert_eq!(code_graph.nodes.len(), 0);
|
||||
assert_eq!(code_graph.edges.len(), 0);
|
||||
assert_eq!(code_graph.graph.node_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_petgraph_nodes_get_graph_index() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let mut output = ParseOutput::default();
|
||||
output.nodes.push(make_node("src/main.rs::foo"));
|
||||
output.nodes.push(make_node("src/main.rs::bar"));
|
||||
|
||||
let code_graph = engine.build_petgraph(output).unwrap();
|
||||
assert_eq!(code_graph.nodes.len(), 2);
|
||||
assert_eq!(code_graph.graph.node_count(), 2);
|
||||
// All nodes should have a graph_index assigned
|
||||
for node in &code_graph.nodes {
|
||||
assert!(node.graph_index.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_petgraph_resolves_edges() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let mut output = ParseOutput::default();
|
||||
output.nodes.push(make_node("src/main.rs::foo"));
|
||||
output.nodes.push(make_node("src/main.rs::bar"));
|
||||
output.edges.push(CodeEdge {
|
||||
id: None,
|
||||
repo_id: "test".to_string(),
|
||||
graph_build_id: "build1".to_string(),
|
||||
source: "src/main.rs::foo".to_string(),
|
||||
target: "bar".to_string(), // short name, should resolve
|
||||
kind: CodeEdgeKind::Calls,
|
||||
file_path: "src/main.rs".to_string(),
|
||||
line_number: Some(5),
|
||||
});
|
||||
|
||||
let code_graph = engine.build_petgraph(output).unwrap();
|
||||
assert_eq!(code_graph.edges.len(), 1);
|
||||
assert_eq!(code_graph.graph.edge_count(), 1);
|
||||
// The resolved edge target should be the full qualified name
|
||||
assert_eq!(code_graph.edges[0].target, "src/main.rs::bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_petgraph_skips_unresolved_edges() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let mut output = ParseOutput::default();
|
||||
output.nodes.push(make_node("src/main.rs::foo"));
|
||||
output.edges.push(CodeEdge {
|
||||
id: None,
|
||||
repo_id: "test".to_string(),
|
||||
graph_build_id: "build1".to_string(),
|
||||
source: "src/main.rs::foo".to_string(),
|
||||
target: "external_crate::something".to_string(),
|
||||
kind: CodeEdgeKind::Calls,
|
||||
file_path: "src/main.rs".to_string(),
|
||||
line_number: Some(5),
|
||||
});
|
||||
|
||||
let code_graph = engine.build_petgraph(output).unwrap();
|
||||
assert_eq!(code_graph.edges.len(), 0);
|
||||
assert_eq!(code_graph.graph.edge_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_code_graph_node_map_consistency() {
|
||||
let engine = GraphEngine::new(1000);
|
||||
let mut output = ParseOutput::default();
|
||||
output.nodes.push(make_node("a::b"));
|
||||
output.nodes.push(make_node("a::c"));
|
||||
output.nodes.push(make_node("a::d"));
|
||||
|
||||
let code_graph = engine.build_petgraph(output).unwrap();
|
||||
assert_eq!(code_graph.node_map.len(), 3);
|
||||
assert!(code_graph.node_map.contains_key("a::b"));
|
||||
assert!(code_graph.node_map.contains_key("a::c"));
|
||||
assert!(code_graph.node_map.contains_key("a::d"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,3 +222,378 @@ impl<'a> ImpactAnalyzer<'a> {
|
||||
.find(|n| n.graph_index == Some(target_gi))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use compliance_core::models::graph::{CodeEdgeKind, CodeNode, CodeNodeKind};
|
||||
use petgraph::graph::DiGraph;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn make_node(
|
||||
qualified_name: &str,
|
||||
file_path: &str,
|
||||
start: u32,
|
||||
end: u32,
|
||||
graph_index: u32,
|
||||
is_entry: bool,
|
||||
kind: CodeNodeKind,
|
||||
) -> CodeNode {
|
||||
CodeNode {
|
||||
id: None,
|
||||
repo_id: "test".to_string(),
|
||||
graph_build_id: "build1".to_string(),
|
||||
qualified_name: qualified_name.to_string(),
|
||||
name: qualified_name
|
||||
.split("::")
|
||||
.last()
|
||||
.unwrap_or(qualified_name)
|
||||
.to_string(),
|
||||
kind,
|
||||
file_path: file_path.to_string(),
|
||||
start_line: start,
|
||||
end_line: end,
|
||||
language: "rust".to_string(),
|
||||
community_id: None,
|
||||
is_entry_point: is_entry,
|
||||
graph_index: Some(graph_index),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_fn_node(
|
||||
qualified_name: &str,
|
||||
file_path: &str,
|
||||
start: u32,
|
||||
end: u32,
|
||||
gi: u32,
|
||||
) -> CodeNode {
|
||||
make_node(
|
||||
qualified_name,
|
||||
file_path,
|
||||
start,
|
||||
end,
|
||||
gi,
|
||||
false,
|
||||
CodeNodeKind::Function,
|
||||
)
|
||||
}
|
||||
|
||||
/// Build a simple linear graph: A -> B -> C
|
||||
fn build_linear_graph() -> CodeGraph {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let c = graph.add_node("c".to_string());
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, c, CodeEdgeKind::Calls);
|
||||
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
node_map.insert("c".to_string(), c);
|
||||
|
||||
CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![
|
||||
make_fn_node("a", "src/main.rs", 1, 5, 0),
|
||||
make_fn_node("b", "src/main.rs", 7, 12, 1),
|
||||
make_fn_node("c", "src/main.rs", 14, 20, 2),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bfs_reachable_outgoing_linear() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let start = cg.node_map["a"];
|
||||
let reachable = analyzer.bfs_reachable(start, Direction::Outgoing);
|
||||
// From a, we can reach b and c
|
||||
assert_eq!(reachable.len(), 2);
|
||||
assert!(reachable.contains(&cg.node_map["b"]));
|
||||
assert!(reachable.contains(&cg.node_map["c"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bfs_reachable_incoming_linear() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let start = cg.node_map["c"];
|
||||
let reachable = analyzer.bfs_reachable(start, Direction::Incoming);
|
||||
// c is reached by a and b
|
||||
assert_eq!(reachable.len(), 2);
|
||||
assert!(reachable.contains(&cg.node_map["a"]));
|
||||
assert!(reachable.contains(&cg.node_map["b"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bfs_reachable_no_neighbors() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map: [("a".to_string(), a)].into_iter().collect(),
|
||||
nodes: vec![make_fn_node("a", "src/main.rs", 1, 5, 0)],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let reachable = analyzer.bfs_reachable(a, Direction::Outgoing);
|
||||
assert!(reachable.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bfs_reachable_cycle() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, a, CodeEdgeKind::Calls);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map: [("a".to_string(), a), ("b".to_string(), b)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
nodes: vec![
|
||||
make_fn_node("a", "f.rs", 1, 5, 0),
|
||||
make_fn_node("b", "f.rs", 6, 10, 1),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let reachable = analyzer.bfs_reachable(a, Direction::Outgoing);
|
||||
// Should handle cycle without infinite loop
|
||||
assert_eq!(reachable.len(), 1);
|
||||
assert!(reachable.contains(&b));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path_exists() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let path = analyzer.find_path(cg.node_map["a"], cg.node_map["c"], 10);
|
||||
assert!(path.is_some());
|
||||
let names = path.unwrap();
|
||||
assert_eq!(names, vec!["a", "b", "c"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path_direct() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let path = analyzer.find_path(cg.node_map["a"], cg.node_map["b"], 10);
|
||||
assert!(path.is_some());
|
||||
let names = path.unwrap();
|
||||
assert_eq!(names, vec!["a", "b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path_same_node() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let path = analyzer.find_path(cg.node_map["a"], cg.node_map["a"], 10);
|
||||
assert!(path.is_some());
|
||||
let names = path.unwrap();
|
||||
assert_eq!(names, vec!["a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path_no_connection() {
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
// No edge between a and b
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map: [("a".to_string(), a), ("b".to_string(), b)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
nodes: vec![
|
||||
make_fn_node("a", "f.rs", 1, 5, 0),
|
||||
make_fn_node("b", "f.rs", 6, 10, 1),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let path = analyzer.find_path(a, b, 10);
|
||||
assert!(path.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_path_depth_limited() {
|
||||
// Build a long chain: a -> b -> c -> d -> e
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let c = graph.add_node("c".to_string());
|
||||
let d = graph.add_node("d".to_string());
|
||||
let e = graph.add_node("e".to_string());
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, c, CodeEdgeKind::Calls);
|
||||
graph.add_edge(c, d, CodeEdgeKind::Calls);
|
||||
graph.add_edge(d, e, CodeEdgeKind::Calls);
|
||||
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
node_map.insert("c".to_string(), c);
|
||||
node_map.insert("d".to_string(), d);
|
||||
node_map.insert("e".to_string(), e);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![
|
||||
make_fn_node("a", "f.rs", 1, 2, 0),
|
||||
make_fn_node("b", "f.rs", 3, 4, 1),
|
||||
make_fn_node("c", "f.rs", 5, 6, 2),
|
||||
make_fn_node("d", "f.rs", 7, 8, 3),
|
||||
make_fn_node("e", "f.rs", 9, 10, 4),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
// Depth 3 won't reach e from a (path length 5)
|
||||
let path = analyzer.find_path(a, e, 3);
|
||||
assert!(path.is_none());
|
||||
// Depth 5 should reach
|
||||
let path = analyzer.find_path(a, e, 5);
|
||||
assert!(path.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_node_at_location_exact_line() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
// Node "b" is at lines 7-12
|
||||
let result = analyzer.find_node_at_location("src/main.rs", Some(9));
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap(), cg.node_map["b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_node_at_location_narrowest_match() {
|
||||
// Outer function 1-20, inner nested 5-10
|
||||
let mut graph = DiGraph::new();
|
||||
let outer = graph.add_node("outer".to_string());
|
||||
let inner = graph.add_node("inner".to_string());
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map: [("outer".to_string(), outer), ("inner".to_string(), inner)]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
nodes: vec![
|
||||
make_fn_node("outer", "src/main.rs", 1, 20, 0),
|
||||
make_fn_node("inner", "src/main.rs", 5, 10, 1),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
// Line 7 is inside both, but inner is narrower
|
||||
let result = analyzer.find_node_at_location("src/main.rs", Some(7));
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap(), inner);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_node_at_location_no_line_returns_file_node() {
|
||||
let mut graph = DiGraph::new();
|
||||
let file_node = graph.add_node("src/main.rs".to_string());
|
||||
let fn_node = graph.add_node("src/main.rs::foo".to_string());
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map: [
|
||||
("src/main.rs".to_string(), file_node),
|
||||
("src/main.rs::foo".to_string(), fn_node),
|
||||
]
|
||||
.into_iter()
|
||||
.collect(),
|
||||
nodes: vec![
|
||||
make_node(
|
||||
"src/main.rs",
|
||||
"src/main.rs",
|
||||
1,
|
||||
100,
|
||||
0,
|
||||
false,
|
||||
CodeNodeKind::File,
|
||||
),
|
||||
make_fn_node("src/main.rs::foo", "src/main.rs", 5, 10, 1),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let result = analyzer.find_node_at_location("src/main.rs", None);
|
||||
assert!(result.is_some());
|
||||
assert_eq!(result.unwrap(), file_node);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_node_at_location_wrong_file() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let result = analyzer.find_node_at_location("nonexistent.rs", Some(5));
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_node_at_location_line_out_of_range() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let result = analyzer.find_node_at_location("src/main.rs", Some(999));
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_basic() {
|
||||
// A (entry) -> B -> C
|
||||
let mut graph = DiGraph::new();
|
||||
let a = graph.add_node("a".to_string());
|
||||
let b = graph.add_node("b".to_string());
|
||||
let c = graph.add_node("c".to_string());
|
||||
graph.add_edge(a, b, CodeEdgeKind::Calls);
|
||||
graph.add_edge(b, c, CodeEdgeKind::Calls);
|
||||
|
||||
let mut node_map = HashMap::new();
|
||||
node_map.insert("a".to_string(), a);
|
||||
node_map.insert("b".to_string(), b);
|
||||
node_map.insert("c".to_string(), c);
|
||||
|
||||
let cg = CodeGraph {
|
||||
graph,
|
||||
node_map,
|
||||
nodes: vec![
|
||||
make_node("a", "src/main.rs", 1, 5, 0, true, CodeNodeKind::Function),
|
||||
make_fn_node("b", "src/main.rs", 7, 12, 1),
|
||||
make_fn_node("c", "src/main.rs", 14, 20, 2),
|
||||
],
|
||||
edges: Vec::new(),
|
||||
};
|
||||
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let result = analyzer.analyze("repo1", "finding1", "build1", "src/main.rs", Some(9));
|
||||
// B's blast radius: C is reachable forward
|
||||
assert_eq!(result.blast_radius, 1);
|
||||
// B has A as direct caller
|
||||
assert_eq!(result.direct_callers, vec!["a"]);
|
||||
// B calls C
|
||||
assert_eq!(result.direct_callees, vec!["c"]);
|
||||
// A is an entry point that reaches B
|
||||
assert_eq!(result.affected_entry_points, vec!["a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_analyze_no_matching_node() {
|
||||
let cg = build_linear_graph();
|
||||
let analyzer = ImpactAnalyzer::new(&cg);
|
||||
let result = analyzer.analyze("repo1", "f1", "b1", "nonexistent.rs", Some(1));
|
||||
assert_eq!(result.blast_radius, 0);
|
||||
assert!(result.affected_entry_points.is_empty());
|
||||
assert!(result.direct_callers.is_empty());
|
||||
assert!(result.direct_callees.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user