use super::*;
fn dep(node: &str, needs: &[&str]) -> CodeDep {
seamed(node, needs, &[])
}
fn seamed(node: &str, needs: &[&str], seam: &[&str]) -> CodeDep {
let own = |xs: &[&str]| -> Vec<String> {
xs.iter().map(|n| (*n).to_string()).collect()
};
CodeDep {
node: node.to_string(),
needs: own(needs),
seam: own(seam),
}
}
#[test]
fn a_node_with_no_sibling_deps_is_ready_and_a_dependent_waits() {
let s = schedule(&[
dep("src/cli", &["src/fed"]),
dep("src/fed", &[]),
dep("src/tokens", &[]),
]);
assert_eq!(
s.rounds,
vec![
vec!["src/fed".to_string(), "src/tokens".to_string()],
vec!["src/cli".to_string()],
]
);
assert!(s.blocked.is_empty(), "{s:?}");
}
#[test]
fn depth_and_width_are_pinned_by_a_known_fixture() {
let s = schedule(&[
dep("a", &[]),
dep("b", &["a"]),
dep("c", &["b"]),
dep("d", &[]),
]);
assert_eq!(s.depth(), 3, "a -> b -> c is the critical path: {s:?}");
assert_eq!(s.width(), 2, "round 1 holds `a` and `d`");
assert_eq!(schedule(&[]).width(), 0, "nothing to schedule is 0 wide");
assert_eq!(schedule(&[]).depth(), 0);
}
#[test]
fn a_cycle_is_reported_rather_than_looped() {
let s = schedule(&[
dep("src/a", &["src/b"]),
dep("src/b", &["src/a"]),
dep("src/free", &[]),
]);
assert_eq!(s.rounds, vec![vec!["src/free".to_string()]]);
assert_eq!(
s.blocked,
vec!["src/a".to_string(), "src/b".to_string()],
"both members are named, not just the one reached first"
);
}
#[test]
fn a_self_edge_is_a_cycle_of_one() {
let s = schedule(&[dep("src/a", &["src/a"])]);
assert!(s.rounds.is_empty(), "{s:?}");
assert_eq!(s.blocked, vec!["src/a".to_string()]);
}
fn code_dag_fixture() -> Result<crate::testrepo::TestRepo, String> {
let r = crate::testrepo::TestRepo::new("wave")?;
let kids = ["a".to_string(), "b".to_string()];
r.write("SPEC.md", &crate::spec::scaffold(".", &["src".to_string()]))?;
r.write("src/SPEC.md", &crate::spec::scaffold("src", &kids))?;
r.write("src/a/SPEC.md", &crate::spec::scaffold("src/a", &[]))?;
r.write("src/b/SPEC.md", &crate::spec::scaffold("src/b", &[]))?;
r.write("src/a/mod.rs", "pub fn a() {}\n")?;
r.write("src/b/mod.rs", "use crate::{a};\npub fn b() {}\n")?;
Ok(r)
}
#[test]
fn the_schedule_follows_the_use_crate_edges() -> Result<(), String> {
let r = code_dag_fixture()?;
let s = wave(r.path(), r.path());
assert_eq!(s.depth(), 2, "b waits for a: {s:?}");
assert!(
s.rounds
.first()
.is_some_and(|f| f.contains(&"src/a".into())),
"a waits for nothing: {s:?}"
);
assert_eq!(s.rounds.get(1), Some(&vec!["src/b".to_string()]));
assert!(s.blocked.is_empty());
Ok(())
}
#[test]
fn an_import_naming_only_a_type_is_an_edge_and_not_a_wait() -> Result<(), String>
{
let r = code_dag_fixture()?;
r.write("src/a/mod.rs", "pub struct Level;\npub fn a() {}\n")?;
r.write("src/b/mod.rs", "use crate::a::Level;\npub fn b() {}\n")?;
let s = wave(r.path(), r.path());
assert_eq!(s.depth(), 1, "one round, behind a seam: {s:?}");
assert_eq!(s.width(), 4, "every node of the fixture at once: {s:?}");
assert_eq!((s.edges, s.blocking), (1, 0), "an edge, and not a wait");
r.write("src/b/mod.rs", "use crate::a::a;\npub fn b() {}\n")?;
let s = wave(r.path(), r.path());
assert_eq!(s.depth(), 2, "a function is a wait: {s:?}");
assert_eq!((s.edges, s.blocking), (1, 1));
r.write(
"src/b/mod.rs",
"use crate::a::Level;\nuse crate::a::a;\npub fn b() {}\n",
)?;
let s = wave(r.path(), r.path());
assert_eq!(s.depth(), 2, "{s:?}");
assert_eq!((s.edges, s.blocking), (1, 1), "counted ONCE, blocking");
Ok(())
}
#[test]
fn a_type_only_edge_is_counted_and_never_scheduled_against() {
let s = schedule(&[
seamed("src/rules", &[], &["src/lint"]),
dep("src/lint", &[]),
]);
assert_eq!(s.depth(), 1, "{s:?}");
assert_eq!((s.edges, s.blocking), (1, 0));
}
#[test]
fn the_federation_table_would_have_scheduled_them_together()
-> Result<(), String> {
let r = code_dag_fixture()?;
let path = r.path().join("src/SPEC.md");
let src = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
let children: Vec<String> =
fed::edges(&src).iter().map(|e| e.dir.clone()).collect();
assert_eq!(
children,
vec!["a".to_string(), "b".to_string()],
"the §F table declares them as co-children, with no edge between"
);
assert_eq!(wave(r.path(), r.path()).depth(), 2);
Ok(())
}
#[test]
fn a_scoped_wave_drops_an_edge_that_leaves_the_scope() -> Result<(), String> {
let r = code_dag_fixture()?;
let s = wave(r.path(), &r.path().join("src/b"));
assert_eq!(s.rounds, vec![vec!["src/b".to_string()]], "{s:?}");
assert!(s.blocked.is_empty(), "the dropped edge is not a cycle");
Ok(())
}