use crate::reply::{Read, Reply, read};
use serde_json::json;
#[test]
fn a_staged_body_reads_its_address_and_its_goal_and_a_receipt_reads_its_name() {
let Read::Answer(Reply::Prepared(staged)) = read(&json!({
"ok": true, "kind": "prepared",
"prepared": {"workspace": "home", "goal": "do the thing"}}))
else {
panic!("a staged start is an answer this build paints");
};
assert_eq!(staged.workspace, "home");
assert_eq!(staged.goal, "do the thing");
assert_eq!(
read(&json!({"ok": true, "kind": "started", "conversation": "brisk-otter"})),
Read::Answer(Reply::Started {
conversation: "brisk-otter".to_owned()
})
);
}
#[test]
fn every_field_of_a_staged_body_survives_the_read_even_the_unpainted_ones() {
let body = json!({
"workspace": "home", "goal": "do it",
"binding": "/home/u/dev/thing", "lineage": "reviewer", "origin": "balls",
"a-field-this-build-has-never-seen": 7});
let Read::Answer(Reply::Prepared(staged)) =
read(&json!({"ok": true, "kind": "prepared", "prepared": body.clone()}))
else {
panic!("a staged start is an answer");
};
assert_eq!(staged.body, body);
}
#[test]
fn a_body_that_will_not_read_refuses_by_name() {
for (frame, named) in [
(
json!({"ok": true, "kind": "prepared", "prepared": {"workspace": "home"}}),
"\"goal\"",
),
(
json!({"ok": true, "kind": "prepared", "prepared": {"goal": "x"}}),
"\"workspace\"",
),
(
json!({"ok": true, "kind": "prepared", "prepared": "home"}),
"\"prepared\"",
),
(json!({"ok": true, "kind": "prepared"}), "\"prepared\""),
(
json!({"ok": true, "kind": "started", "conversation": 7}),
"\"conversation\"",
),
(json!({"ok": true, "kind": "started"}), "\"conversation\""),
] {
let Read::Unreadable(why) = read(&frame) else {
panic!("{frame} is not readable");
};
assert!(why.contains(named), "{why} should name {named}");
}
}