crates/monetize-ui-server/tests/every_route_has_a_caller.rs
//! ★ UW-14 / UW-18: **every console route has a caller in the console** (owner: no legacy —
//! a route nobody calls is wired or deleted). Each `.route("…")` of `serve.rs` must be named
//! by the browser half (`monetize-ui-web/src`, comments not counted): its literal head, and
//! every literal segment after its first parameter. The routes no page calls are named here
//! with their caller.
const NOT_A_PAGE: &[&str] = &[
// The liveness probe.
"/health",
// The old product-addressed map: private-gunnar-ops' estate-box gate still reads it
// (A5/A6 `/api/estate/gunnar`); deleted when that gate reads `/api/estates/{e}/map`.
"/api/estate/{product}",
];
fn code_only(src: &str) -> String {
src.lines().filter(|l| !l.trim_start().starts_with("//")).collect::<Vec<_>>().join("\n")
}
fn web_source() -> String {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../monetize-ui-web/src");
let mut out = String::new();
let mut dirs = vec![root];
while let Some(d) = dirs.pop() {
for e in std::fs::read_dir(&d).unwrap().flatten() {
let p = e.path();
if p.is_dir() {
dirs.push(p);
} else if p.extension().is_some_and(|x| x == "rs") {
out.push_str(&code_only(&std::fs::read_to_string(&p).unwrap()));
out.push('\n');
}
}
}
out
}
fn routes() -> Vec<String> {
let serve = std::fs::read_to_string(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/serve.rs")).unwrap();
code_only(&serve).split(".route(\"").skip(1).filter_map(|rest| rest.split('"').next().map(str::to_owned)).collect()
}
fn called(route: &str, web: &str) -> bool {
let (head, tail) = route.split_once('{').unwrap_or((route, ""));
let literals = tail.split('/').skip(1).filter(|s| !s.is_empty() && !s.contains('{') && !s.contains('}'));
web.contains(head) && literals.into_iter().all(|seg| web.contains(&format!("/{seg}")))
}
#[test]
fn every_console_route_is_called_by_a_console_page() {
let web = web_source();
let all = routes();
assert!(all.len() > 30, "the router was not read: {all:?}");
let dead: Vec<&String> = all.iter().filter(|r| !NOT_A_PAGE.contains(&r.as_str()) && !called(r, &web)).collect();
assert!(dead.is_empty(), "console routes no page calls (wire them or delete them): {dead:?}");
}