#[test]
fn test_project_context_graph_creation() {
let graph = ProjectContextGraph::new();
assert_eq!(graph.num_nodes(), 0);
assert_eq!(graph.num_edges(), 0);
}
#[test]
fn test_add_item_o1_lookup() {
let mut graph = ProjectContextGraph::new();
let func = AstItem::Function {
name: "test_func".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 1,
};
graph
.add_item("test_func".to_string(), func.clone())
.unwrap();
let retrieved = graph.get_item("test_func");
assert!(retrieved.is_some());
assert_eq!(retrieved.unwrap(), &func);
assert!(graph.get_item("nonexistent").is_none());
}
#[test]
fn test_add_duplicate_fails() {
let mut graph = ProjectContextGraph::new();
let func = AstItem::Function {
name: "dup".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 1,
};
graph.add_item("dup".to_string(), func.clone()).unwrap();
let result = graph.add_item("dup".to_string(), func);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Duplicate"));
}
#[test]
fn test_add_edge_relationships() {
let mut graph = ProjectContextGraph::new();
graph
.add_item(
"main".to_string(),
AstItem::Function {
name: "main".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 1,
},
)
.unwrap();
graph
.add_item(
"helper".to_string(),
AstItem::Function {
name: "helper".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 10,
},
)
.unwrap();
graph.add_edge("main", "helper").unwrap();
assert_eq!(graph.num_nodes(), 2);
assert_eq!(graph.num_edges(), 1);
}
#[test]
fn test_pagerank_hotness() {
let mut graph = ProjectContextGraph::new();
graph
.add_item(
"main".to_string(),
AstItem::Function {
name: "main".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 1,
},
)
.unwrap();
graph
.add_item(
"helper1".to_string(),
AstItem::Function {
name: "helper1".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 10,
},
)
.unwrap();
graph
.add_item(
"helper2".to_string(),
AstItem::Function {
name: "helper2".to_string(),
visibility: "pub".to_string(),
is_async: false,
line: 20,
},
)
.unwrap();
graph.add_edge("main", "helper1").unwrap();
graph.add_edge("main", "helper2").unwrap();
graph.add_edge("helper1", "helper2").unwrap();
graph.update_hotness().unwrap();
let hot = graph.hot_symbols();
assert_eq!(hot.len(), 3);
assert_eq!(hot[0].0, "helper2");
assert!(hot[0].1 > hot[1].1); assert!(hot[0].1 > hot[2].1); }
#[test]
fn test_hot_symbols_ranking() {
let mut graph = ProjectContextGraph::new();
for i in 0..5 {
graph
.add_item(
format!("func{}", i),
AstItem::Function {
name: format!("func{}", i),
visibility: "pub".to_string(),
is_async: false,
line: i * 10,
},
)
.unwrap();
}
for i in 0..4 {
graph.add_edge(&format!("func{}", i), "func4").unwrap();
}
graph.update_hotness().unwrap();
let hot = graph.hot_symbols();
assert_eq!(hot[0].0, "func4");
}
#[test]
fn test_empty_graph_pagerank() {
let mut graph = ProjectContextGraph::new();
graph.update_hotness().unwrap();
assert_eq!(graph.hot_symbols().len(), 0);
}