use super::*;
struct Expects(String);
impl Answerer for Expects {
fn answer(
&self,
_client: &crate::registry::Client,
request: Value,
) -> Box<dyn Iterator<Item = Value>> {
let named = request
.get("workspace")
.and_then(Value::as_str)
.unwrap_or_default();
Box::new(std::iter::once(
json!({"ok": named == self.0, "kind": "echo"}),
))
}
}
fn listening(dir: &std::path::Path, expects: &str) -> Listener {
mint(dir);
let listener = Listener::bind(
&fixture(dir, Role::Server, crate::test_support::wire::EPHEMERAL),
Arc::new(Expects(expects.to_owned())),
Presence::default(),
)
.expect("bind");
std::fs::write(dir.join(material::ADDRESS), listener.address()).expect("address");
listener
}
fn entry_dir(world: &crate::xdg::Env, leaf: &str) -> std::path::PathBuf {
material::dir(world).join(entries::ENTRIES).join(leaf)
}
#[test]
fn a_gesture_naming_an_entry_goes_down_that_entrys_channel() {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
let _home = listening(&material::dir(&world), "never");
let _far = listening(&entry_dir(&world, "work"), "work");
assert_eq!(
run(
&world,
&[r#"{"op":"conversations","workspace":"work"}"#.to_owned()]
),
0
);
}
#[test]
fn an_entry_that_renames_its_workspace_carries_the_hosts_name() {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
let _home = listening(&material::dir(&world), "never");
let dir = entry_dir(&world, "work");
let _far = listening(&dir, "home");
std::fs::write(dir.join(entries::WORKSPACE), "home\n").expect("write");
assert_eq!(
run(
&world,
&[r#"{"op":"conversations","workspace":"work"}"#.to_owned()]
),
0
);
}
#[test]
fn a_name_no_entry_holds_and_no_name_at_all_go_to_the_flat_root() {
for (expects, typed) in [
("other", r#"{"op":"conversations","workspace":"other"}"#),
("", r#"{"op":"workspaces"}"#),
] {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
let _home = listening(&material::dir(&world), expects);
let _far = listening(&entry_dir(&world, "work"), "never");
assert_eq!(run(&world, &[typed.to_owned()]), 0, "{typed}");
}
}
#[test]
fn a_half_provisioned_entry_refuses_in_its_own_words() {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
let dir = entry_dir(&world, "work");
std::fs::create_dir_all(&dir).expect("mkdir");
std::fs::write(dir.join(material::ANCHORS), "-----PEM-----\n").expect("write");
let typed = json!({"op": "conversations", "workspace": "work"});
let gesture = Gesture::Ask(Query::Conversations {
workspace: "work".to_owned(),
});
let refused = channel(&world, &gesture, &typed).err().unwrap_or_default();
assert!(
refused.contains("half-provisioned") && refused.contains("work"),
"{refused}"
);
assert_eq!(run(&world, &[typed.to_string()]), USAGE_EXIT);
}
#[test]
fn nothing_is_rewritten_where_no_entry_renames_anything() {
let (_tmp, world, _listener) = engine(true);
let typed = json!({"op": "conversations", "workspace": "anything"});
let gesture = Gesture::Ask(Query::Conversations {
workspace: "anything".to_owned(),
});
let flat = open(&world).expect("flat seat").address();
let (seat, carried) = channel(&world, &gesture, &typed).expect("the flat root");
assert_eq!(seat.address(), flat);
assert_eq!(carried, typed, "the envelope crosses as written");
let dir = entry_dir(&world, "anything");
mint(&dir);
std::fs::write(dir.join(material::ADDRESS), "127.0.0.1:7737").expect("address");
let (seat, carried) = channel(&world, &gesture, &typed).expect("the entry");
assert_ne!(seat.address(), flat, "the entry's own address");
assert_eq!(carried, typed, "and its own name, so nothing is rewritten");
}