use std::collections::BTreeSet;
use super::super::{Read, read};
use crate::channel::hello::PROTOCOL;
use crate::test_support::corpus::{CLASSES, files, fixture, record, root};
fn replay(class: &str, expected: impl Fn(&Read) -> bool) {
for path in files(class) {
let file = fixture(&path);
for frame in &file.frames {
let answered = read(frame);
assert!(
expected(&answered),
"{} is filed under {class} and read as {answered:?}",
file.path.display()
);
}
}
}
#[test]
fn the_answers_are_answers() {
replay("answers", |answered| matches!(answered, Read::Answer(_)));
}
#[test]
fn the_refusals_are_refusals() {
replay("refusals", |answered| matches!(answered, Read::Refusal(_)));
}
#[test]
fn the_unreadable_are_unreadable() {
replay("unreadable", |answered| {
matches!(answered, Read::Unreadable(_))
});
}
#[test]
fn every_corpus_file_belongs_to_a_class() {
let carried = ["README.md", "shapes.json", "request"];
for entry in std::fs::read_dir(root()).expect("the corpus").flatten() {
let path = entry.path();
let leaf = path.file_name().unwrap_or_default().to_string_lossy();
assert!(
carried.contains(&leaf.as_ref()) || CLASSES.contains(&leaf.as_ref()),
"corpus/{leaf} is in no class — file it under one of {CLASSES:?}"
);
}
}
#[test]
fn every_vendored_reply_shape_is_classified_exactly_once() {
let mut filed: Vec<String> = Vec::new();
for class in CLASSES {
for path in files(class) {
let file = fixture(&path);
let Some(shape) = file.shape else { continue };
let stem = path.file_stem().unwrap_or_default().to_string_lossy();
assert_eq!(
shape,
stem.as_ref(),
"{} declares shape {shape:?} — a vendored file is named for its \
shape, which is how the refresh finds it again",
file.path.display()
);
filed.push(format!("reply/{shape}"));
}
}
let known: BTreeSet<String> = filed.iter().cloned().collect();
assert_eq!(known.len(), filed.len(), "a shape filed under two classes");
let upstream: BTreeSet<String> = record()
.shapes
.into_keys()
.filter(|key| key.starts_with("reply/"))
.collect();
assert_eq!(
known, upstream,
"the vendored reply set and upstream's shape record disagree — \
run scripts/refresh-corpus.sh against a yog checkout"
);
}
#[test]
fn the_corpus_is_stamped_with_the_protocol_this_seat_speaks() {
let corpus = record().protocol;
assert_eq!(
corpus,
u64::from(PROTOCOL),
"corpus/shapes.json is protocol {corpus} and this seat speaks \
{PROTOCOL} — refresh the corpus, or the seat is behind the wire"
);
}