use super::*;
use crate::boundary::dispatch::Caller;
use crate::boundary::tests::snapshot;
use crate::cli_outbound::Cli;
use crate::registry::Client;
use crate::registry::mailbox::{Mailbox, Taken};
use serde_json::json;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tempfile::tempdir;
fn tool(name: &str) -> Tool {
Tool {
name: name.to_owned(),
description: "does a thing".to_owned(),
input_schema: json!({"type": "object"}),
subject_cwd: false,
}
}
fn seated(state_root: &Path, client: &Client) -> Deps {
crate::registry::register(state_root, client, "alba").expect("the registration's own file");
deps(state_root, client.clone())
}
fn deps(state_root: &Path, client: Client) -> Deps {
Deps {
litany: Cli::new("/no/such/litany"),
bl: Cli::new("/no/such/bl"),
state_root: state_root.to_path_buf(),
home: PathBuf::from("/home/x"),
yog_data_root: PathBuf::from("/data"),
yog_binary: PathBuf::from("/no/such/yog"),
world: crate::xdg::Env::from_env(),
snapshot: Arc::new(snapshot(
Path::new("/names/alba"),
"alba",
Vec::new(),
Vec::new(),
)),
caller: Caller {
client,
..Caller::default()
},
}
}
#[test]
fn a_connections_set_lands_under_its_own_identity() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = seated(root.path(), &laptop);
assert_eq!(advertise(&deps, &[tool("Bash")]), Ok(advertised(true)));
assert_eq!(
crate::registry::tools::read(root.path(), &laptop),
vec![tool("Bash")]
);
}
#[test]
fn only_a_write_answers_true_and_a_restoration_is_a_write() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = seated(root.path(), &laptop);
assert_eq!(advertise(&deps, &[tool("Bash")]), Ok(advertised(true)));
assert_eq!(advertise(&deps, &[tool("Bash")]), Ok(advertised(false)));
assert_eq!(advertise(&deps, &[]), Ok(advertised(true)));
assert_eq!(advertise(&deps, &[tool("Bash")]), Ok(advertised(true)));
}
fn advertised(wrote: bool) -> crate::boundary::reply::Reply {
crate::boundary::reply::Reply::Advertised { wrote }
}
#[test]
fn an_in_world_caller_is_refused_with_a_sentence() {
let root = tempdir().expect("tempdir");
let refusal = advertise(&deps(root.path(), Client::local()), &[tool("Bash")])
.expect_err("refused in band");
assert!(refusal.contains("no client identity"), "{refusal}");
assert!(!root.path().join(crate::registry::CLIENTS).exists());
}
#[test]
fn a_colliding_name_declines_and_leaves_the_stored_set_alone() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = seated(root.path(), &laptop);
advertise(&deps, &[tool("Bash")]).expect("stored");
let refusal = advertise(&deps, &[tool("Read"), tool("Read")]).expect_err("declined");
assert!(refusal.contains("duplicate tool name"), "{refusal}");
assert_eq!(
crate::registry::tools::read(root.path(), &laptop),
vec![tool("Bash")],
"a refused presentation changes nothing"
);
}
fn patient(mut deps: Deps) -> Deps {
deps.caller.mailbox = Mailbox::holding(4000, Duration::from_millis(5));
deps
}
fn parked(deps: &Deps, client: &str) -> std::thread::JoinHandle<Result<Taken, String>> {
let (mail, name) = (deps.caller.mailbox.clone(), client.to_owned());
let reader = std::thread::spawn(move || mail.take(&name));
crate::test_support::foot::parked(&deps.caller.mailbox, client);
reader
}
#[test]
fn a_restart_with_a_changed_set_is_serving_at_once() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = patient(seated(root.path(), &laptop));
advertise(&deps, &[tool("Bash")]).expect("stored");
let old = parked(&deps, "laptop");
assert_eq!(
advertise(&deps, &[tool("Read")]),
Ok(Reply::Advertised { wrote: true })
);
assert_eq!(
crate::registry::tools::read(root.path(), &laptop),
vec![tool("Read")],
"the new set is the set in force"
);
let serving = deps.caller.mailbox.reading("laptop");
assert!(
serving.is_ok(),
"the new activation's read is granted at once"
);
assert_eq!(
old.join().expect("the old read"),
Ok(Taken::Superseded),
"and a predecessor that is still alive learns it lost the queue"
);
}
#[test]
fn a_restart_with_the_same_set_is_serving_at_once() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = patient(seated(root.path(), &laptop));
advertise(&deps, &[tool("Bash")]).expect("stored");
let old = parked(&deps, "laptop");
assert_eq!(
advertise(&deps, &[tool("Bash")]),
Ok(Reply::Advertised { wrote: false })
);
let serving = deps.caller.mailbox.reading("laptop");
assert!(serving.is_ok(), "granted at once");
assert_eq!(old.join().expect("the old read"), Ok(Taken::Superseded));
}
#[test]
fn a_refused_advertisement_leaves_the_parked_read_alone() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = seated(root.path(), &laptop);
let _parked = deps.caller.mailbox.reading("laptop").expect("its read");
advertise(&deps, &[tool("Read"), tool("Read")]).expect_err("declined");
assert!(deps.caller.mailbox.serving("laptop"));
}
#[test]
fn an_unwritable_store_refuses() {
let root = tempdir().expect("tempdir");
let laptop = Client::parse("laptop").expect("identity");
let deps = seated(root.path(), &laptop);
std::fs::create_dir_all(
crate::registry::dir(root.path(), &laptop).join(crate::registry::tools::TOOLS),
)
.expect("a directory in the document's place");
assert!(advertise(&deps, &[tool("Bash")]).is_err());
}
#[test]
fn a_client_registered_nowhere_is_refused_naming_the_registration() {
let root = tempdir().expect("tempdir");
let devbox = Client::parse("devbox").expect("identity");
let deps = deps(root.path(), devbox.clone());
let refusal = advertise(&deps, &[tool("Bash")]).expect_err("presented to nobody");
assert!(refusal.contains("registered in no workspace"), "{refusal}");
assert!(refusal.contains("clients/devbox/workspaces"), "{refusal}");
assert!(
crate::registry::tools::read(root.path(), &devbox).is_empty(),
"and nothing is stored for nobody to see"
);
}