use super::{OP, WORKSPACE, op, parse, succeeded, with_workspace, workspace};
use serde_json::json;
#[test]
fn parse_takes_a_gesture_and_refuses_everything_that_is_not_one() {
assert_eq!(
parse(r#"{"op":"workspaces"}"#).expect("a gesture"),
json!({"op": "workspaces"})
);
for (text, said) in [
("{", "not JSON"),
("[1,2]", "a gesture is a JSON object"),
(r#""workspaces""#, "a gesture is a JSON object"),
(r#"{"workspace":"home"}"#, "missing field"),
(r#"{"op":7}"#, "is not a string"),
] {
let refusal = parse(text).expect_err("refused");
assert!(refusal.contains(said), "{text}: {refusal}");
}
}
#[test]
fn an_unknown_op_is_carried_rather_than_judged() {
let envelope = parse(&format!(r#"{{"{OP}":"a-verb-from-the-future"}}"#)).expect("a gesture");
assert_eq!(workspace(&envelope), None);
}
#[test]
fn a_top_level_workspace_is_the_slot() {
let envelope = json!({"op": "conversations", "workspace": "home"});
assert_eq!(workspace(&envelope), Some("home".to_owned()));
assert_eq!(
with_workspace(&envelope, "personal"),
json!({"op": "conversations", "workspace": "personal"})
);
}
#[test]
fn a_prepared_body_carries_the_slot_one_level_down() {
let envelope = json!({
"op": "prompt",
"prepared": {"workspace": "home", "goal": "do the thing"},
"goal": "do the thing",
});
assert_eq!(workspace(&envelope), Some("home".to_owned()));
let written = with_workspace(&envelope, "personal");
assert_eq!(written["prepared"]["workspace"], json!("personal"));
assert_eq!(
written["prepared"]["goal"], envelope["prepared"]["goal"],
"the rewrite touched something other than the name"
);
}
#[test]
fn an_envelope_naming_no_workspace_crosses_unchanged() {
for envelope in [
json!({"op": "workspaces"}),
json!({"op": "prompt", "prepared": "not an object"}),
json!({"op": "prompt", "prepared": {"goal": "unaddressed"}}),
json!({"op": "conversations", "workspace": 7}),
json!({"op": "prompt", "prepared": {"workspace": 7}}),
json!({"op": "config", "target": "not an object"}),
json!({"op": "config", "target": {"file": "cadence"}}),
json!({"op": "config", "target": {"file": "brazen", "workspace": 7}}),
] {
assert_eq!(workspace(&envelope), None, "{envelope}");
assert_eq!(
with_workspace(&envelope, "personal"),
envelope,
"{envelope}"
);
}
}
#[test]
fn a_config_destination_carries_the_slot_one_level_down() {
let envelope = json!({
"op": "config",
"target": {"file": "brazen", "workspace": "clientleaf"},
"text": "",
});
assert_eq!(workspace(&envelope), Some("clientleaf".to_owned()));
let written = with_workspace(&envelope, "hostname");
assert_eq!(written["target"]["workspace"], json!("hostname"));
assert_eq!(
written["target"]["file"], envelope["target"]["file"],
"the rewrite touched something other than the name"
);
assert_eq!(written["text"], envelope["text"]);
}
#[test]
fn a_lineage_destination_is_addressed_the_same_way() {
let envelope = json!({
"op": "config",
"target": {
"file": "branch",
"lineage": "default",
"origin": "advance",
"path": "providers.yaml",
"workspace": "clientleaf",
},
});
assert_eq!(workspace(&envelope), Some("clientleaf".to_owned()));
assert_eq!(
with_workspace(&envelope, "hostname")["target"]["workspace"],
json!("hostname")
);
}
#[test]
fn a_workspace_nested_anywhere_else_is_not_the_address() {
let envelope = json!({
"op": "message",
"body": {"workspace": "home"},
});
assert_eq!(workspace(&envelope), None);
assert_eq!(with_workspace(&envelope, "personal"), envelope);
}
#[test]
fn the_outer_name_is_read_before_a_nested_one() {
let envelope = json!({
"op": "config",
"workspace": "outer",
"target": {"file": "brazen", "workspace": "inner"},
});
assert_eq!(workspace(&envelope), Some("outer".to_owned()));
let written = with_workspace(&envelope, "personal");
assert_eq!(written["workspace"], json!("personal"));
assert_eq!(written["target"]["workspace"], json!("inner"));
}
#[test]
fn a_value_that_is_not_an_object_has_no_slot() {
let envelope = json!([1, 2, 3]);
assert_eq!(workspace(&envelope), None);
assert_eq!(with_workspace(&envelope, "personal"), envelope);
}
#[test]
fn the_slot_is_the_wire_s_own_field_name() {
let envelope = json!({"op": "scan", WORKSPACE: "home"});
assert_eq!(workspace(&envelope), Some("home".to_owned()));
}
#[test]
fn the_verdict_is_the_last_frame_s_and_silence_is_never_yes() {
assert!(succeeded(&[json!({"ok": true})]));
assert!(succeeded(&[json!({"ok": false}), json!({"ok": true})]));
assert!(!succeeded(&[json!({"ok": true}), json!({"ok": false})]));
assert!(!succeeded(&[]));
assert!(!succeeded(&[json!({"kind": "rows"})]));
assert!(!succeeded(&[json!({"ok": "yes"})]));
}
#[test]
fn the_op_is_said_back_and_a_gesture_without_one_is_a_question_mark() {
assert_eq!(op(&json!({"op": "nudge", "workspace": "home"})), "nudge");
assert_eq!(op(&json!({"op": 7})), "?");
assert_eq!(op(&json!({})), "?");
assert_eq!(op(&json!("not an object")), "?");
}