use cbcl_core::dialect::{DialectInstallError, DialectRegistry};
use cbcl_core::protocol::{repeat_base_name, NodeRef};
use cbcl_core::r6::r6_violations;
use cbcl_parser::{parse, parse_dialect, parse_protocol, ProtocolParseError};
fn parse_dialect_src(src: &str) -> cbcl_core::dialect::Dialect {
parse_dialect(&parse(src).expect("sexpr parses")).expect("dialect parses")
}
fn install(src: &str) -> Result<(), DialectInstallError> {
let d = parse_dialect_src(src);
let mut reg = DialectRegistry::new();
reg.install(d)
}
fn single(name: &str) -> NodeRef {
NodeRef::Single(name.into())
}
#[test]
fn repeat_3_x_y_expands_to_six_step_chain() {
let sexpr = parse("(protocol (then begin (repeat 3 x y)))").unwrap();
let proto = parse_protocol(&sexpr).unwrap();
let expected = ["x#1", "y#1", "x#2", "y#2", "x#3", "y#3"];
assert_eq!(proto.steps.len(), 1 + expected.len());
for name in expected {
assert!(proto.steps.contains_key(name), "missing step {name}");
}
assert_eq!(proto.steps["begin"].successors, vec![single("x#1")]);
assert_eq!(proto.steps["x#1"].predecessors, vec![single("begin")]);
assert_eq!(proto.steps["x#1"].successors, vec![single("y#1")]);
assert_eq!(proto.steps["y#1"].successors, vec![single("x#2")]); assert_eq!(proto.steps["x#2"].predecessors, vec![single("y#1")]);
assert_eq!(proto.steps["y#2"].successors, vec![single("x#3")]); assert_eq!(proto.steps["y#3"].predecessors, vec![single("x#3")]);
assert!(proto.steps["y#3"].successors.is_empty());
for name in expected {
assert_eq!(repeat_base_name(name), &name[..1]);
}
}
fn repeat_dialect_src() -> &'static str {
"(define rpt (cbcl) @author \
(:roles (a b)) \
(extend x (v) :from a :to (b) (tell @b)) \
(extend y (v) :from b :to (a) (tell @a)) \
(protocol (then begin (repeat 3 x y))))"
}
fn hand_unrolled_src() -> &'static str {
"(define hand (cbcl) @author \
(:roles (a b)) \
(extend x1 (v) :from a :to (b) (tell @b)) \
(extend y1 (v) :from b :to (a) (tell @a)) \
(extend x2 (v) :from a :to (b) (tell @b)) \
(extend y2 (v) :from b :to (a) (tell @a)) \
(extend x3 (v) :from a :to (b) (tell @b)) \
(extend y3 (v) :from b :to (a) (tell @a)) \
(protocol (then begin x1 y1 x2 y2 x3 y3)))"
}
#[test]
fn repeat_matches_hand_unrolled_under_r5_r6_and_r1_accepts() {
let rpt = parse_dialect_src(repeat_dialect_src());
let hand = parse_dialect_src(hand_unrolled_src());
let rp = rpt.causal_protocol.as_ref().unwrap();
let hp = hand.causal_protocol.as_ref().unwrap();
assert_eq!(rp.expansion_size(), hp.expansion_size());
assert_eq!(rp.expansion_size(), 6);
assert_eq!(r6_violations(&rpt), r6_violations(&hand));
assert_eq!(r6_violations(&rpt), Vec::new());
assert!(install(repeat_dialect_src()).is_ok());
assert!(install(hand_unrolled_src()).is_ok());
}
#[test]
fn undefined_base_performative_is_flagged_once_by_r5() {
let src = "(define bad (cbcl) @author \
(extend x (v) (tell @x)) \
(protocol (then begin (repeat 3 x y))))";
let err = install(src).unwrap_err();
match err {
DialectInstallError::R5Violation { shape_errors, .. } => {
let about_y: Vec<&String> = shape_errors.iter().filter(|e| e.contains("'y'")).collect();
assert_eq!(
about_y.len(),
1,
"one violation for base 'y': {shape_errors:?}"
);
}
other => panic!("expected R5Violation, got: {other}"),
}
}
#[test]
fn expansion_exceeding_declared_r2_bounds_is_rejected_at_install() {
let src = "(define tight (cbcl) @author \
(extend x (v) (tell @x)) \
(extend y (v) (tell @y)) \
(:resource-requirements ((max-depth 8) (max-expansion-size 8))) \
(protocol (then begin (repeat 10 x y))))";
let err = install(src).unwrap_err();
match err {
DialectInstallError::R2ProtocolBudgetExceeded {
expanded_steps,
budget,
..
} => {
assert_eq!(expanded_steps, 20);
assert_eq!(budget, 8);
}
other => panic!("expected R2ProtocolBudgetExceeded, got: {other}"),
}
}
#[test]
fn hand_unrolled_protocol_is_priced_identically_at_install() {
let mut steps = String::from("(then begin");
let mut extends = String::new();
for i in 1..=10 {
extends.push_str(&format!(
" (extend x{i} (v) (tell @x)) (extend y{i} (v) (tell @y))"
));
steps.push_str(&format!(" x{i} y{i}"));
}
steps.push(')');
let src = format!(
"(define tight-hand (cbcl) @author{extends} \
(:resource-requirements ((max-depth 8) (max-expansion-size 8))) \
(protocol {steps}))"
);
let err = install(&src).unwrap_err();
assert!(
matches!(
err,
DialectInstallError::R2ProtocolBudgetExceeded {
expanded_steps: 20,
budget: 8,
..
}
),
"got: {err}"
);
}
#[test]
fn expansion_beyond_absolute_ceiling_is_a_typed_parse_rejection() {
let sexpr = parse("(protocol (then begin (repeat 9000 x)))").unwrap();
let err = parse_protocol(&sexpr).unwrap_err();
assert!(
matches!(err, ProtocolParseError::RepeatBudgetExceeded { .. }),
"got: {err:?}"
);
}
#[test]
fn nested_repeat_multiplies_bounds() {
let sexpr = parse("(protocol (then begin (repeat 3 (repeat 4 x))))").unwrap();
let proto = parse_protocol(&sexpr).unwrap();
assert_eq!(proto.steps.len(), 1 + 12);
assert!(proto.steps.contains_key("x#1#1"));
assert!(proto.steps.contains_key("x#4#3"));
assert_eq!(repeat_base_name("x#4#3"), "x");
let sexpr = parse("(protocol (then begin (repeat 100 (repeat 100 x))))").unwrap();
assert!(matches!(
parse_protocol(&sexpr).unwrap_err(),
ProtocolParseError::RepeatBudgetExceeded { .. }
));
}
#[test]
fn lexically_similar_user_symbol_still_installs() {
let src = "(define similar (cbcl) @author \
(extend x (v) (tell @x)) \
(extend x1 (v) (tell @x)) \
(extend x-1 (v) (tell @x)) \
(extend x.1 (v) (tell @x)) \
(protocol (then begin (repeat 2 x) x1 x-1 x.1)))";
assert!(install(src).is_ok(), "{:?}", install(src));
let d = parse_dialect_src(src);
let proto = d.causal_protocol.unwrap();
assert!(proto.steps.contains_key("x#1"));
assert!(proto.steps.contains_key("x1"));
assert_eq!(repeat_base_name("x1"), "x1"); assert_eq!(repeat_base_name("x#1"), "x");
}
#[test]
fn copy_names_are_unwritable_in_source() {
assert!(parse("(protocol (then begin x#1))").is_err());
}
#[test]
fn programmatic_hash_symbol_is_rejected_fail_closed() {
use cbcl_core::sexpr::{Atom, SExpr};
let sym = |s: &str| SExpr::Atom(Atom::Symbol(s.into()));
let sexpr = SExpr::List(vec![
sym("protocol"),
SExpr::List(vec![sym("then"), sym("begin"), sym("x#1")]),
]);
assert!(matches!(
parse_protocol(&sexpr).unwrap_err(),
ProtocolParseError::InvalidNodeRef { .. }
));
}
#[test]
fn body_ending_in_any_seams_over_the_alternatives_copy_instances() {
let sexpr = parse("(protocol (then begin (repeat 2 x (any a b)) done))").unwrap();
let proto = parse_protocol(&sexpr).unwrap();
let any_copy =
|i: u64| NodeRef::Any([format!("a#{i}"), format!("b#{i}")].into_iter().collect());
assert_eq!(proto.steps["x#2"].predecessors, vec![any_copy(1)]);
assert_eq!(proto.steps["done"].predecessors, vec![any_copy(2)]);
assert_eq!(proto.steps["x#1"].successors, vec![any_copy(1)]);
assert_eq!(proto.steps["a#1"].predecessors, vec![single("x#1")]);
}
#[test]
fn body_ending_in_all_seams_on_the_fan_ins_copy_instance() {
let sexpr = parse("(protocol (then begin (repeat 2 x (all p q)) done))").unwrap();
let proto = parse_protocol(&sexpr).unwrap();
let all_copy =
|i: u64| NodeRef::All([format!("p#{i}"), format!("q#{i}")].into_iter().collect());
assert_eq!(proto.steps["x#2"].predecessors, vec![all_copy(1)]);
assert_eq!(proto.steps["done"].predecessors, vec![all_copy(2)]);
}
#[test]
fn any_seam_stays_chooser_coherent_per_copy() {
let coherent = "(define coh (cbcl) @author \
(:roles (r1 r2)) \
(extend x (v) :from r1 :to (r2) (tell @r2)) \
(extend a (v) :from r2 :to (r1) (tell @r1)) \
(extend b (v) :from r2 :to (r1) (tell @r1)) \
(protocol (then begin (repeat 2 x (any a b)))))";
assert!(install(coherent).is_ok(), "{:?}", install(coherent));
let incoherent = "(define incoh (cbcl) @author \
(:roles (r1 r2)) \
(extend x (v) :from r1 :to (r2) (tell @r2)) \
(extend a (v) :from r2 :to (r1) (tell @r1)) \
(extend b (v) :from r1 :to (r2) (tell @r2)) \
(protocol (then begin (repeat 2 x (any a b)))))";
let err = install(incoherent).unwrap_err();
match err {
DialectInstallError::R6Violation { violations, .. } => {
for copy in [
vec!["a#1".to_string(), "b#1".to_string()],
vec!["a#2".to_string(), "b#2".to_string()],
] {
assert!(
violations.contains(&cbcl_core::role::R6Violation::ChooserIncoherent {
members: copy.clone()
}),
"expected per-copy incoherence for {copy:?}: {violations:?}"
);
}
}
other => panic!("expected R6Violation, got: {other}"),
}
}
#[test]
fn nat_predicate_rejects_zero_negative_and_non_integer_counts() {
for bad in [
"(protocol (then begin (repeat 0 x)))",
"(protocol (then begin (repeat -3 x)))",
"(protocol (then begin (repeat many x)))",
"(protocol (then begin (repeat \"3\" x)))",
"(protocol (then begin (repeat (3) x)))",
] {
let sexpr = parse(bad).unwrap();
assert!(
matches!(
parse_protocol(&sexpr).unwrap_err(),
ProtocolParseError::RepeatCountNotNat { .. }
),
"expected RepeatCountNotNat for {bad}"
);
}
}
#[test]
fn repeat_rejects_empty_body_and_begin_in_body() {
let sexpr = parse("(protocol (then begin (repeat 3)))").unwrap();
assert!(matches!(
parse_protocol(&sexpr).unwrap_err(),
ProtocolParseError::RepeatEmptyBody
));
let sexpr = parse("(protocol (then a (repeat 3 begin x)))").unwrap();
assert!(matches!(
parse_protocol(&sexpr).unwrap_err(),
ProtocolParseError::RepeatContainsBegin
));
}
#[test]
fn repeat_1_is_the_identity_expansion() {
let sexpr = parse("(protocol (then begin (repeat 1 x y)))").unwrap();
let proto = parse_protocol(&sexpr).unwrap();
assert_eq!(proto.steps.len(), 3);
assert_eq!(proto.steps["x#1"].successors, vec![single("y#1")]);
}