use std::collections::BTreeMap;
use serde_json::{Map, Value, json};
use super::super::super::{
EFFORT, PREPARE, PRIORITY, PROMPT, effort, find, prepare, priority, prompt,
};
use super::{emitted, request};
use crate::envelope;
const BARE: &str = "bare";
const UNEMITTED: &[(&str, usize, &str)] = &[
(
"files",
3,
"the `at` and `path` forms: this seat composes the bare listing only \
— pinning a commit and previewing one file are controls the records \
pane does not have yet, and a seat that guessed either would answer \
a question nobody asked",
),
(
"stop",
1,
"the children cascade: this seat composes the bare stop only, and the \
flag that takes a whole subtree down is a second control with a second \
confirmation — it belongs beside the records that would say what is \
under there",
),
(
"prepare",
9,
"the path and ball rungs: this seat composes the bare rung only, and a \
seat that guessed a rung would found a claim nobody asked for",
),
(
"prompt",
6,
"a predicted conversation seed: this seat spells `seed` null, because \
the mint is the engine's and a seat that predicted one would have to \
fire the name it painted",
),
];
fn text(obj: &Map<String, Value>, key: &str) -> String {
obj.get(key)
.and_then(Value::as_str)
.unwrap_or_else(|| panic!("this shape's frame carries no field {key:?}"))
.to_owned()
}
fn rebuilt(frame: &Value) -> Option<Value> {
let obj = frame.as_object().expect("a gesture envelope");
let op = text(obj, envelope::OP);
if let Some(verb) = find(&op) {
if obj.len() != verb.params.len() + 1 {
return None;
}
let args = verb.params.iter().map(|p| text(obj, p)).collect();
return Some(verb.envelope(args).expect("the shape's own arity"));
}
match op.as_str() {
PREPARE => (obj["payload"] == json!({ "rung": BARE }))
.then(|| prepare(text(obj, envelope::WORKSPACE))),
EFFORT => Some(effort(
text(obj, envelope::WORKSPACE),
text(obj, "role"),
obj["level"].as_str().map(str::to_owned),
)),
PRIORITY => obj["on"]
.as_bool()
.map(|on| priority(text(obj, envelope::WORKSPACE), text(obj, "role"), on)),
PROMPT => obj["seed"].is_null().then(|| {
let staged = crate::reply::start::prepared(obj).expect("a staged body");
let address = staged.workspace.clone();
prompt(&staged, address, text(obj, "goal"))
}),
_ => None,
}
}
#[test]
fn what_the_seat_emits_is_what_the_corpus_carries() {
let mut declined: BTreeMap<String, usize> = BTreeMap::new();
for word in emitted() {
for frame in &request(&word).frames {
match rebuilt(frame) {
Some(built) => assert_eq!(&built, frame, "{word}"),
None => *declined.entry(word.clone()).or_default() += 1,
}
}
}
let recorded: BTreeMap<String, usize> = UNEMITTED
.iter()
.map(|(word, n, _)| ((*word).to_owned(), *n))
.collect();
assert_eq!(
declined, recorded,
"the frames this seat cannot compose have moved — amend UNEMITTED with \
the reason, or emit them"
);
}