use super::*;
#[test]
fn a_node_ceiling_comes_from_the_file_not_a_constant() {
crate::testrepo::dogfood(|| {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let tdd = ceiling_for(root, &root.join("src/tdd")).unwrap();
assert!(
tdd > tokens::DEFAULT_NODE,
"src/tdd is listed and should not fall back to the default: {tdd}"
);
assert_eq!(
ceiling_for(root, &root.join("src/nope")).unwrap(),
ceiling_for(root, &root.join("src")).unwrap()
);
assert_eq!(
ceiling_for(root, &root.join("docs")).unwrap(),
tokens::DEFAULT_NODE
);
});
}
#[test]
fn verdict_reports_direction_and_distance() {
assert_eq!(verdict(100, 500), Verdict::Fits { slack: 400 });
assert_eq!(verdict(900, 500), Verdict::Over { by: 400 });
}
#[test]
fn the_root_ceiling_comes_from_its_spec_row_not_the_default() {
crate::testrepo::dogfood(|| {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let c = ceiling_for(root, root).unwrap();
assert!(
c > tokens::DEFAULT_NODE,
"root must resolve to its SPEC.md row, got the default: {c}"
);
});
}
#[test]
fn depth_selects_something_or_it_is_a_flag_that_lies() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let rule = pack(root, root, Depth::Rule).unwrap();
let all = pack(root, root, Depth::All).unwrap();
assert!(
rule.cost.tokens < all.cost.tokens,
"rule {} must be cheaper than all {}",
rule.cost.tokens,
all.cost.tokens
);
assert!(all.text.contains("## \u{a7}B"), "all keeps the archive");
assert!(
!rule.text.contains("## \u{a7}B"),
"rule drops §B -- history is not a rule"
);
assert!(
!rule.text.contains("## \u{a7}R"),
"rule drops §R -- a measurement is not a rule"
);
assert!(
rule.text.contains("## \u{a7}V"),
"rule keeps §V -- that is the point of it"
);
}
#[test]
fn every_node_resolves_to_a_real_ceiling() {
crate::testrepo::dogfood(|| {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
for node in fed::discover(root) {
let c = ceiling_for(root, &node).unwrap();
assert_ne!(
c,
tokens::DEFAULT_NODE,
"{} fell back to the node default -- give it a row in \
.context-limits, or a prefix that covers it",
node.display()
);
}
});
}
#[test]
fn a_ceiling_is_compared_at_the_boundary_not_near_it() {
assert_eq!(verdict(500, 500), Verdict::Fits { slack: 0 });
assert_eq!(verdict(501, 500), Verdict::Over { by: 1 });
}
#[test]
fn chain_of_repo_root_is_at_least_the_root_spec() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
assert!(
!fed::chain(root, root).is_empty(),
"root SPEC.md must exist (V5)"
);
}
#[test]
fn a_dir_with_no_spec_is_an_error_not_its_ancestors_pack() -> Result<(), String>
{
let r = crate::testrepo::TestRepo::new("lens-nowhere")?;
let err = pack(r.path(), &r.path().join("nowhere"), Depth::Rule)
.err()
.ok_or("a missing node was packed")?;
assert!(err.to_string().contains("nowhere"), "{err}");
assert!(
pack(r.path(), r.path(), Depth::Rule).is_ok(),
"the root packs"
);
Ok(())
}
#[test]
fn own_is_the_node_spec_alone_not_its_chain() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("lens-own")?;
let child = "# SPEC\n\n## \u{a7}G GOAL\n\nthe child\n\n## \u{a7}V INVARIANTS\n\nV1: a child rule\n";
r.write("SPEC.md", "# SPEC\n\n## \u{a7}G GOAL\n\nthe root, long enough to cost a few tokens more than its child does\n")?;
r.write("a/SPEC.md", child)?;
let a = r.path().join("a");
let p = pack(r.path(), &a, Depth::Rule).map_err(|e| e.to_string())?;
let own = own_cost(&a, Depth::Rule).map_err(|e| e.to_string())?;
assert_eq!(p.chain.len(), 2, "root and child");
assert_eq!(
own.tokens,
tokens::count(&crate::spec::rule_depth(child)).tokens,
"own is the child's spec at rule depth"
);
assert!(own.tokens < p.cost.tokens, "the chain also pays the root");
assert!(
own_cost(&r.path().join("nowhere"), Depth::Rule).is_err(),
"no node is an error, never a zero cost"
);
Ok(())
}
#[test]
fn ancestors_task_rows_stay_out_of_the_pack() -> Result<(), String> {
let r = crate::testrepo::TestRepo::new("lens-own-tasks")?;
let spec = |t: &str, v: &str| {
format!(
"# SPEC\n\n## \u{a7}G GOAL\n\ng\n\n## \u{a7}V INVARIANTS\n\nV1: {v}\n\n## \u{a7}T TASKS\n\nid|status|task|cites\n{t}|.|work|V1\n"
)
};
r.write("SPEC.md", &spec("T1", "root-rule"))?;
r.write("a/SPEC.md", &spec("T2", "a-rule"))?;
r.write("a/b/SPEC.md", &spec("T3", "b-rule"))?;
let b = r.path().join("a/b");
for depth in [Depth::Rule, Depth::Why] {
let p = pack(r.path(), &b, depth).map_err(|e| e.to_string())?;
assert!(
!p.text.contains("T1|") && !p.text.contains("T2|"),
"{depth:?}: {}",
p.text
);
assert!(
p.text.contains("T3|"),
"{depth:?}: the target keeps its rows"
);
assert!(
p.text.contains("V1: root-rule") && p.text.contains("V1: a-rule"),
"{depth:?}: ancestors keep their rules: {}",
p.text
);
}
let all = pack(r.path(), &b, Depth::All).map_err(|e| e.to_string())?;
assert!(all.text.contains("T1|") && all.text.contains("T2|"));
Ok(())
}