use super::{ask, entry, flat, route, wired, yes};
use crate::channel::entries::WORKSPACE;
use crate::cli::Stream;
use crate::test_support::{Scratch, mint};
use serde_json::json;
#[test]
fn an_unaddressed_gesture_goes_to_the_flat_root() {
let scratch = Scratch::new();
let engine = wired(&scratch, &flat(), vec![vec![yes()]]);
let verdict = ask(scratch.path(), &json!({"op": "workspaces"}));
assert_eq!(verdict.code, 0);
assert_eq!(verdict.stream, Stream::Out);
assert_eq!(verdict.text, yes().to_string());
assert!(
engine.heard().contains(&json!({"op": "workspaces"})),
"{:?}",
engine.heard()
);
}
#[test]
fn a_name_an_entry_holds_goes_down_that_entry_s_channel() {
let scratch = Scratch::new();
let engine = wired(&scratch, &entry("home"), vec![vec![yes()]]);
let verdict = ask(
scratch.path(),
&json!({"op": "conversations", "workspace": "home"}),
);
assert_eq!(verdict.code, 0);
assert!(
engine
.heard()
.contains(&json!({"op": "conversations", "workspace": "home"})),
"{:?}",
engine.heard()
);
}
#[test]
fn an_entry_that_renames_carries_the_host_s_name_across() {
let scratch = Scratch::new();
let engine = wired(&scratch, &entry("home"), vec![vec![yes()]]);
std::fs::write(
scratch.path().join(entry("home")).join(WORKSPACE),
"personal",
)
.expect("the workspace file");
assert_eq!(
ask(
scratch.path(),
&json!({"op": "conversations", "workspace": "home"})
)
.code,
0
);
assert!(
engine
.heard()
.contains(&json!({"op": "conversations", "workspace": "personal"})),
"the gesture crossed in this box's spelling: {:?}",
engine.heard()
);
}
#[test]
fn an_entry_that_renames_nothing_carries_the_envelope_unchanged() {
let scratch = Scratch::new();
mint::provisioned(&scratch.path().join(entry("home")), "engine.example:9000");
let envelope = json!({"op": "conversations", "workspace": "home", "extra": [1, 2]});
let (_channel, carried) = route(scratch.path(), &envelope).expect("routed");
assert_eq!(carried, envelope);
}
#[test]
fn a_hollow_entry_refuses_rather_than_falling_through_to_the_flat_root() {
let scratch = Scratch::new();
wired(&scratch, &flat(), vec![vec![yes()]]);
std::fs::create_dir_all(scratch.path().join(entry("home"))).expect("mkdir");
let verdict = ask(
scratch.path(),
&json!({"op": "conversations", "workspace": "home"}),
);
assert_eq!(verdict.code, 1);
assert_eq!(verdict.stream, Stream::Err);
assert!(
verdict.text.contains("is an empty entry"),
"{}",
verdict.text
);
}
#[test]
fn a_selector_naming_no_entry_refuses_instead_of_answering_from_the_flat_root() {
let scratch = Scratch::new();
let engine = wired(&scratch, &flat(), vec![vec![yes()]]);
mint::provisioned(&scratch.path().join(entry("alpha")), "alpha.example:9000");
let verdict = ask(
scratch.path(),
&json!({"op": "workspaces", "workspace": "NoSuchWs"}),
);
assert_eq!(verdict.code, 1);
assert_eq!(verdict.stream, Stream::Err);
assert!(
verdict.text.contains(r#"no channel named "NoSuchWs""#),
"{}",
verdict.text
);
assert!(
verdict.text.contains(r#"(this box's own engine), "alpha""#),
"it says what this box does hold: {}",
verdict.text
);
assert!(verdict.text.contains("`mv` it under"), "{}", verdict.text);
assert!(
engine.heard().is_empty(),
"nothing was asked of a channel nobody named: {:?}",
engine.heard()
);
}
#[test]
fn a_selector_naming_an_entry_reaches_that_entry_s_engine() {
let scratch = Scratch::new();
let engine = wired(&scratch, &entry("alpha"), vec![vec![yes()]]);
let verdict = ask(
scratch.path(),
&json!({"op": "workspaces", "workspace": "alpha"}),
);
assert_eq!(verdict.code, 0);
assert!(
engine
.heard()
.contains(&json!({"op": "workspaces", "workspace": "alpha"})),
"{:?}",
engine.heard()
);
}
#[test]
fn a_name_that_falls_through_to_an_unprovisioned_flat_root_names_itself() {
let scratch = Scratch::new();
mint::provisioned(&scratch.path().join(entry("alpha")), "alpha.example:9000");
let verdict = ask(
scratch.path(),
&json!({"op": "conversations", "workspace": "beta"}),
);
assert_eq!(verdict.code, 1);
assert_eq!(verdict.stream, Stream::Err);
assert!(
verdict.text.contains(r#"no entry here holds "beta""#),
"{}",
verdict.text
);
assert!(
verdict.text.contains(r#"(this box's own engine), "alpha""#),
"{}",
verdict.text
);
assert!(
verdict.text.contains("rather than minting a second leaf"),
"the rename is offered ahead of the mint: {}",
verdict.text
);
assert!(
verdict.text.contains("no wire provisioned"),
"the flat root's own sentence is still carried, as the cause: {}",
verdict.text
);
}
#[test]
fn a_name_no_entry_holds_still_reaches_the_flat_engine_when_the_op_takes_one() {
let scratch = Scratch::new();
let engine = wired(&scratch, &flat(), vec![vec![yes()]]);
mint::provisioned(&scratch.path().join(entry("alpha")), "alpha.example:9000");
let verdict = ask(
scratch.path(),
&json!({"op": "conversations", "workspace": "beta"}),
);
assert_eq!(verdict.code, 0);
assert!(
engine
.heard()
.contains(&json!({"op": "conversations", "workspace": "beta"})),
"{:?}",
engine.heard()
);
}