use serde_json::{Value, json};
use super::super::{
CREATE, DELIVER, EFFORT, FAN, FORK, OPS, PREPARE, PRIORITY, PROMPT, RETIRE, UPDATE, table,
};
use crate::envelope;
use crate::test_support::corpus::{Fixture, Signature, files, fixture, record, root};
mod emits;
fn emitted() -> Vec<String> {
table()
.iter()
.map(|verb| verb.word.to_owned())
.chain(
[
PREPARE, PROMPT, EFFORT, PRIORITY, OPS, FORK, CREATE, UPDATE, FAN, DELIVER, RETIRE,
]
.into_iter()
.map(str::to_owned),
)
.collect()
}
fn vendored() -> Vec<Fixture> {
files("request").iter().map(|p| fixture(p)).collect()
}
fn request(word: &str) -> Fixture {
fixture(&root().join("request").join(format!("{word}.json")))
}
fn addressed(signature: &Signature, frame: &Value) -> Option<String> {
let named = |field: &str| signature.contains_key(field);
if named("/workspace:string") {
return frame[envelope::WORKSPACE].as_str().map(str::to_owned);
}
if named("/prepared/workspace:string") {
return frame[envelope::PREPARED][envelope::WORKSPACE]
.as_str()
.map(str::to_owned);
}
if named("/target/workspace:string") {
return frame[envelope::TARGET][envelope::WORKSPACE]
.as_str()
.map(str::to_owned);
}
None
}
#[test]
fn every_request_shape_is_vendored_and_every_verb_is_one() {
let mut filed: Vec<String> = vendored()
.iter()
.map(|file| {
format!(
"request/{}",
file.shape.clone().expect("a vendored fixture")
)
})
.collect();
filed.sort();
let upstream: Vec<String> = record()
.shapes
.into_keys()
.filter(|key| key.starts_with("request/"))
.collect();
assert_eq!(
filed, upstream,
"the vendored request set and upstream's shape record disagree — \
run scripts/refresh-corpus.sh against a yog checkout"
);
for word in emitted() {
assert!(
filed.contains(&format!("request/{word}")),
"this seat emits {word:?} and the vocabulary has no such op"
);
}
}
#[test]
fn every_request_frame_is_a_gesture_this_seat_can_route() {
let shapes = record().shapes;
for file in vendored() {
let shape = file.shape.clone().expect("a vendored fixture");
let signature = &shapes[&format!("request/{shape}")];
for frame in &file.frames {
let parsed = envelope::parse(&frame.to_string())
.unwrap_or_else(|why| panic!("{}: {why}", file.path.display()));
assert_eq!(parsed[envelope::OP], json!(shape), "{shape}");
assert_eq!(
envelope::workspace(&parsed),
addressed(signature, frame),
"{shape}: the seat routes this gesture by a different address \
than the one its shape carries"
);
}
}
}