#![cfg(feature = "server")]
use std::fs;
use std::sync::Arc;
use parking_lot::Mutex;
use serde_json::json;
use symforge::live_index::LiveIndex;
use symforge::protocol::SymForgeServer;
use symforge::watcher::WatcherInfo;
use tempfile::TempDir;
struct Fixture {
_dir: TempDir,
server: SymForgeServer,
}
impl Fixture {
fn new(files: &[(&str, &str)]) -> Self {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path().to_path_buf();
for (rel, content) in files {
let path = root.join(rel);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).expect("create parent dir");
}
fs::write(&path, content).expect("write fixture file");
}
let shared = LiveIndex::load(&root).expect("LiveIndex::load");
let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
let server = SymForgeServer::new(
shared,
"compact_map_ranking_test".to_string(),
watcher_info,
Some(root),
None,
);
Self { _dir: dir, server }
}
fn expected_dependents(&self, path: &str) -> usize {
self.server
.index()
.read()
.capture_find_dependents_view(path)
.files
.len()
}
async fn compact_map(&self) -> String {
self.server
.dispatch_tool_for_tests("get_repo_map", json!({}))
.await
}
async fn compact_map_explicit(&self) -> String {
self.server
.dispatch_tool_for_tests("get_repo_map", json!({ "detail": "compact" }))
.await
}
}
fn hub_and_leaf() -> Vec<(&'static str, &'static str)> {
vec![
(
"src/lib.rs",
"pub mod zeta;\npub mod alpha;\npub mod a;\npub mod b;\npub mod c;\n",
),
("src/zeta.rs", "pub struct Core {\n pub seed: u32,\n}\n"),
("src/alpha.rs", "pub struct Leaf {\n pub idle: u32,\n}\n"),
(
"src/a.rs",
"use crate::zeta::Core;\n\npub fn a(c: &Core) -> u32 {\n c.seed\n}\n",
),
(
"src/b.rs",
"use crate::zeta::Core;\n\npub fn b(c: &Core) -> u32 {\n c.seed + 1\n}\n",
),
(
"src/c.rs",
"use crate::zeta::Core;\n\npub fn c(c: &Core) -> u32 {\n c.seed + 2\n}\n",
),
]
}
fn entry_position(map: &str, file_rel: &str) -> Option<usize> {
let needle = format!("({file_rel})");
let mut in_section = false;
let mut ordinal = 0usize;
for line in map.lines() {
if line.starts_with("Key types:") {
in_section = true;
continue;
}
if !in_section {
continue;
}
if !line.starts_with(" ") {
break;
}
ordinal += 1;
if line.contains(&needle) {
return Some(ordinal);
}
}
None
}
fn entry_line<'a>(map: &'a str, file_rel: &str) -> Option<&'a str> {
let needle = format!("({file_rel})");
map.lines()
.filter(|l| l.starts_with(" "))
.find(|l| l.contains(&needle))
}
#[tokio::test]
async fn core_ranks_before_leaf_in_compact_map() {
let fx = Fixture::new(&hub_and_leaf());
let core_deps = fx.expected_dependents("src/zeta.rs");
let leaf_deps = fx.expected_dependents("src/alpha.rs");
assert!(
core_deps >= 2,
"fixture must give src/zeta.rs >= 2 distinct dependents for a meaningful \
ranking assertion (got {core_deps})"
);
assert_eq!(
leaf_deps, 0,
"fixture must give src/alpha.rs zero dependents (got {leaf_deps})"
);
let map = fx.compact_map().await;
let core_pos = entry_position(&map, "src/zeta.rs").unwrap_or_else(|| {
panic!("compact map must list src/zeta.rs in Key types:\n{map}");
});
let leaf_pos = entry_position(&map, "src/alpha.rs").unwrap_or_else(|| {
panic!("compact map must list src/alpha.rs in Key types:\n{map}");
});
assert!(
core_pos < leaf_pos,
"high-fan-in src/zeta.rs (deps={core_deps}) must rank before zero-fan-in \
src/alpha.rs (deps={leaf_deps}) despite sorting later alphabetically; \
core_pos={core_pos} leaf_pos={leaf_pos}\n{map}"
);
}
#[tokio::test]
async fn high_fan_in_annotated_low_fan_in_not() {
let fx = Fixture::new(&hub_and_leaf());
let core_deps = fx.expected_dependents("src/zeta.rs");
assert!(
core_deps >= 2,
"fixture precondition: hub has >= 2 dependents"
);
let map = fx.compact_map().await;
let core_line = entry_line(&map, "src/zeta.rs")
.unwrap_or_else(|| panic!("compact map must contain a src/zeta.rs entry line\n{map}"));
let leaf_line = entry_line(&map, "src/alpha.rs")
.unwrap_or_else(|| panic!("compact map must contain a src/alpha.rs entry line\n{map}"));
let needle = format!("(→{core_deps})");
assert!(
core_line.contains(&needle),
"src/zeta.rs entry must carry the fan-in annotation {needle:?}\nline: {core_line:?}"
);
assert!(
!leaf_line.contains("(→"),
"src/alpha.rs (0 dependents) must NOT carry a (→N) annotation\nline: {leaf_line:?}"
);
}
#[tokio::test]
async fn compact_map_render_is_deterministic() {
let fx = Fixture::new(&hub_and_leaf());
let first = fx.compact_map().await;
let second = fx.compact_map().await;
assert_eq!(
first, second,
"two renders of the same index must be byte identical (stable tie-break)\n\
first:\n{first}\nsecond:\n{second}"
);
}
#[tokio::test]
async fn default_detail_matches_explicit_compact() {
let fx = Fixture::new(&hub_and_leaf());
let default_map = fx.compact_map().await;
let explicit_map = fx.compact_map_explicit().await;
assert_eq!(
default_map, explicit_map,
"default (no detail) and detail=\"compact\" must render identically\n\
default:\n{default_map}\nexplicit:\n{explicit_map}"
);
}