use super::{Outcome, Read, Reply, read};
use serde_json::json;
mod corpus;
#[test]
fn a_kind_this_build_paints_becomes_the_typed_answer() {
assert_eq!(
read(&json!({"ok": true, "kind": "nudged"})),
Read::Answer(Reply::Nudged)
);
}
#[test]
fn the_kindless_envelope_is_the_refusal_and_nothing_else() {
assert_eq!(
read(&json!({"ok": false, "error": "unknown workspace \"hoem\""})),
Read::Refusal("unknown workspace \"hoem\"".to_owned())
);
let Read::Unreadable(why) = read(&json!({"ok": true, "rows": []})) else {
panic!("an answer with no kind is not readable");
};
assert!(why.contains("an answer with no"), "{why}");
}
#[test]
fn a_refusal_missing_its_own_fields_is_unreadable_by_name() {
for (frame, named) in [
(json!({"ok": false}), "\"error\""),
(json!({"error": "no"}), "\"ok\""),
] {
let Read::Unreadable(why) = read(&frame) else {
panic!("{frame} is not readable");
};
assert!(why.contains(named), "{frame}: {why}");
}
}
#[test]
fn a_captured_run_takes_its_verdict_from_the_exit_code() {
let frame = json!({"ok": true, "kind": "outcome", "exit": 1,
"stdout": "", "stderr": "the gate said no\n"});
let Read::Answer(Reply::Outcome(outcome)) = read(&frame) else {
panic!("a captured run");
};
assert!(!outcome.ok(), "exit 1 is not ok, whatever `ok` said");
assert_eq!(outcome.stderr, "the gate said no\n");
assert!(
Outcome {
exit: 0,
stdout: String::new(),
stderr: String::new(),
}
.ok()
);
}
#[test]
fn an_exit_status_outside_its_own_type_is_unreadable() {
let frame = json!({"ok": true, "kind": "outcome", "exit": 9_999_999_999_i64,
"stdout": "", "stderr": ""});
let Read::Unreadable(why) = read(&frame) else {
panic!("out of range");
};
assert!(why.contains("out of range"), "{why}");
}
#[test]
fn a_kind_this_build_does_not_paint_refuses_by_name_with_a_remedy() {
let Read::Unreadable(why) = read(&json!({"ok": true, "kind": "invocations", "rows": []}))
else {
panic!("an unpainted kind");
};
assert!(why.contains("\"invocations\""), "{why}");
assert!(why.contains("upgrade"), "{why}");
}
#[test]
fn a_frame_that_is_not_a_reply_object_is_unreadable() {
for (frame, said) in [
(json!(["workspaces", []]), "not a JSON object"),
(json!("workspaces"), "not a JSON object"),
(json!({"ok": true, "kind": 7}), "non-string field"),
] {
let Read::Unreadable(why) = read(&frame) else {
panic!("{frame} is not readable");
};
assert!(why.contains(said), "{frame}: {why}");
}
}
#[test]
fn every_frame_lands_on_one_of_the_three_arms() {
for frame in [
json!(null),
json!(0),
json!({}),
json!({"ok": "yes"}),
json!({"kind": "workspaces"}),
json!({"ok": true, "kind": "workspaces"}),
json!({"ok": true, "kind": "workspaces", "rows": {}}),
json!({"ok": true, "kind": "workspaces", "rows": [7]}),
json!({"ok": true, "kind": "transcript", "rows": [{}]}),
json!({"ok": true, "kind": "follow"}),
json!({"ok": true, "kind": "conversations", "rows": [[]]}),
] {
let answered = read(&frame);
assert!(
matches!(
answered,
Read::Answer(_) | Read::Refusal(_) | Read::Unreadable(_)
),
"{frame}: {answered:?}"
);
}
}