use super::*;
use crate::cli_outbound::Cli;
use serde_json::json;
use tempfile::tempdir;
#[test]
fn a_wire_client_enumerates_only_its_registrations() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let ctx = over(
root.path(),
world_of(data.path(), &["home", "corp"]),
data.path().to_path_buf(),
Cli::new("/no/such/lernie"),
);
let phone = client("phone");
crate::registry::register(root.path(), &phone, "home").unwrap();
let reply = ctx.answer_as(&phone, &json!({"op": "workspaces"}));
assert_eq!(listed(&reply), ["home".to_owned()]);
assert_eq!(
listed(&ctx.answer(&json!({"op": "workspaces"}))),
["corp".to_owned(), "home".to_owned()]
);
assert!(listed(&ctx.answer_as(&client("stranger"), &json!({"op": "workspaces"}))).is_empty());
}
#[test]
fn an_unregistered_workspace_refuses_exactly_as_an_unknown_one() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let ctx = over(
root.path(),
world_of(data.path(), &["home", "corp"]),
data.path().to_path_buf(),
Cli::new("/no/such/lernie"),
);
let phone = client("phone");
crate::registry::register(root.path(), &phone, "home").unwrap();
let hidden = ctx.answer_as(&phone, &json!({"op": "conversations", "workspace": "corp"}));
let absent = ctx.answer_as(
&phone,
&json!({"op": "conversations", "workspace": "corp2"}),
);
assert_eq!(hidden["ok"], false);
assert_eq!(hidden["error"], "unknown workspace \"corp\"");
assert_eq!(absent["error"], "unknown workspace \"corp2\"");
assert_eq!(
ctx.answer_as(&phone, &json!({"op": "conversations", "workspace": "home"}))["ok"],
true
);
}
#[test]
fn a_torn_envelope_refuses_over_the_wire_too() {
let root = tempdir().unwrap();
let refusal = ctx(root.path()).answer_as(&client("phone"), &json!({"op": "enhance"}));
assert_eq!(refusal["ok"], false);
assert!(
refusal["error"]
.as_str()
.unwrap_or_default()
.contains("enhance")
);
}
#[test]
fn a_workspace_created_over_the_wire_registers_its_creator() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let bin = tempdir().unwrap();
seed(data.path());
let ctx = over(
root.path(),
world_of(data.path(), &[]),
data.path().to_path_buf(),
fake_lernie(bin.path()),
);
let phone = client("phone");
let reply = ctx.answer_as(
&phone,
&json!({"op": "prepare", "workspace": "fresh", "payload": {"rung": "bare"}}),
);
assert_eq!(reply["kind"], "prepared", "{reply}");
assert!(
crate::binding::workspace_path(data.path(), "fresh").is_dir(),
"the raise founded it under yog's flat names root"
);
assert_eq!(
crate::registry::registered(root.path(), &phone),
std::collections::BTreeSet::from(["fresh".to_owned()])
);
assert!(crate::registry::registered(root.path(), &client("laptop")).is_empty());
}
#[test]
fn a_create_naming_a_workspace_that_exists_refuses_rather_than_joining_it() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let bin = tempdir().unwrap();
seed(data.path());
let taken = crate::binding::workspace_path(data.path(), "corp");
std::fs::create_dir_all(&taken).unwrap();
let ctx = over(
root.path(),
world_of(data.path(), &["corp"]),
data.path().to_path_buf(),
fake_lernie(bin.path()),
);
let phone = client("phone");
let refusal = ctx.answer_as(
&phone,
&json!({"op": "prepare", "workspace": "corp", "payload": {"rung": "bare"}}),
);
assert_eq!(refusal["ok"], false);
assert_eq!(refusal["error"], "unknown workspace \"corp\"");
assert!(crate::registry::registered(root.path(), &phone).is_empty());
}
#[test]
fn a_raise_naming_something_that_is_not_a_plain_component_refuses() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let ctx = over(
root.path(),
world_of(data.path(), &[]),
data.path().to_path_buf(),
Cli::new("/no/such/lernie"),
);
for name in ["../elsewhere", ".."] {
let refusal = ctx.answer_as(
&client("phone"),
&json!({"op": "prepare", "workspace": name, "payload": {"rung": "bare"}}),
);
assert_eq!(refusal["ok"], false, "{name}");
assert_eq!(refusal["error"], format!("unknown workspace {name:?}"));
}
assert!(crate::registry::registered(root.path(), &client("phone")).is_empty());
}
#[test]
fn a_follow_resolves_its_address_under_the_callers_scope() {
let root = tempdir().unwrap();
let data = tempdir().unwrap();
let ctx = over(
root.path(),
world_of(data.path(), &["home", "corp"]),
data.path().to_path_buf(),
Cli::new("/no/such/lernie"),
);
let phone = client("phone");
crate::registry::register(root.path(), &phone, "home").unwrap();
let follow = |workspace: &str, agent: &str| json!({"op": "follow", "workspace": workspace, "agent": agent});
assert!(ctx.follow(&phone, &follow("corp", "c-1")).is_none());
assert!(ctx.follow(&phone, &follow("home", "nobody")).is_none());
let said = |workspace: &str| {
ctx.answer_as(&phone, &follow(workspace, "c-1"))["error"]
.as_str()
.unwrap_or_default()
.replace(workspace, "<name>")
};
assert_eq!(
said("corp"),
said("nowhere"),
"absence, not a scope error: a workspace this seat is not seated in \
refuses in the identical bytes one nobody founded earns"
);
}
#[test]
fn nothing_but_a_follow_is_a_stream() {
let root = tempdir().unwrap();
let ctx = ctx(root.path());
for request in [
json!({"op": "workspaces"}),
json!({"op": "teleport"}),
json!("not even an object"),
] {
assert!(ctx.follow(&client("phone"), &request).is_none());
}
}