use cbcl_core::{
attest::attestation_v3_preimage,
canonical::{
canonical_encode, canonical_params, dialect_canonical_bytes, dialect_hash, dialect_name,
},
keyid::SignatureSuite,
message::Message,
r4::dialect_sign_body,
sexpr::SExpr,
typed_addr::{field_sexpr, typed_root, FIELD_ORDER},
};
use cbcl_parser::{dialect_parser::parse_dialect, parser::parse};
#[test]
fn reference_examples_match_the_wire_values() {
let guide = include_str!("../../../docs/reference/canonical-encoding.md");
let source = guide
.split_once("```cbcl\n")
.unwrap()
.1
.lines()
.next()
.unwrap();
cbcl_parser::parse_and_install_dialect(&parse(source).unwrap()).unwrap();
let mut dialect = parse_dialect(&parse(source).unwrap()).unwrap();
let name = dialect_name(&dialect);
dialect.name.clear();
let empty_name_bytes = dialect_canonical_bytes(&dialect);
dialect.name = name.clone();
let reissued = source.replacen("define demo", &format!("define {name}"), 1);
cbcl_parser::parse_and_install_dialect(&parse(&reissued).unwrap()).unwrap();
let message_source = "(tell @bob \"hi\" :sender @alice)";
assert!(guide.contains(message_source));
let root = typed_root(&Message::try_from(&parse(message_source).unwrap()).unwrap());
for address in [&name, &dialect_hash(&dialect), &root] {
assert!(guide.contains(&format!("`{address}`")));
}
for bytes in [
empty_name_bytes,
dialect_sign_body(&dialect),
attestation_v3_preimage(&SignatureSuite::Ed25519, &root),
] {
let text = String::from_utf8(bytes).unwrap();
assert!(guide.contains(&format!("```text\n{text}\n```")));
}
}
#[test]
fn atom_table_matches_encoder() {
let guide = include_str!("../../../docs/reference/canonical-encoding.md");
let mut count = 0;
for row in guide
.lines()
.filter(|line| line.starts_with('|') && line.contains(" → "))
{
let example = row.split('|').nth(3).unwrap().trim();
let (source, expected) = example.split_once(" → ").unwrap();
let bytes = canonical_encode(&parse(source.trim_matches('`')).unwrap());
if let Some(hex) = expected.strip_prefix("hex ") {
let rendered = bytes
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<Vec<_>>()
.join(" ");
assert_eq!(rendered, hex.trim_matches('`'), "{source}");
} else {
assert_eq!(bytes, expected.trim_matches('`').as_bytes(), "{source}");
}
count += 1;
}
assert_eq!(count, 7);
}
#[test]
fn parameter_slots_and_message_fields_match_reference() {
let guide = include_str!("../../../docs/reference/canonical-encoding.md");
let tail = guide
.lines()
.find(|s| s.contains("the parameter tail `"))
.unwrap();
let mut quoted = tail.split('`').skip(1).step_by(2);
let list = |source: &str| parse(&format!("({source})")).unwrap();
let SExpr::List(params) = list(quoted.next().unwrap()) else {
unreachable!()
};
assert_eq!(
SExpr::List(canonical_params(¶ms)),
list(quoted.next().unwrap())
);
let example = guide
.lines()
.find(|s| s.contains("the six values in order"))
.unwrap();
let mut values = example.split('`').skip(1).step_by(2);
let msg = Message::try_from(&parse(values.next().unwrap()).unwrap()).unwrap();
for id in FIELD_ORDER {
assert_eq!(
field_sexpr(&msg, id),
parse(values.next().unwrap()).unwrap(),
"{id:?}"
);
}
assert_eq!(typed_root(&msg), values.next().unwrap());
}