use super::*;
use crate::registry::presence::Presence;
use crate::test_support::wire::{material as fixture, mint};
use crate::wire::server::{Answerer, Listener};
use serde_json::json;
use std::sync::Arc;
use tempfile::TempDir;
struct Verdict(bool);
impl Answerer for Verdict {
fn answer(&self, _client: &crate::registry::Client, request: Value) -> Vec<Value> {
vec![json!({"ok": self.0, "kind": "echo", "asked": request})]
}
}
fn engine(ok: bool) -> (TempDir, crate::xdg::Env, Listener) {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
let dir = material::dir(&world);
mint(&dir);
let listener = Listener::bind(
&fixture(&dir, Role::Server, crate::test_support::wire::EPHEMERAL),
Arc::new(Verdict(ok)),
Presence::default(),
)
.expect("bind");
std::fs::write(dir.join(material::ADDRESS), listener.address()).expect("address");
(tmp, world, listener)
}
#[test]
fn a_gesture_reaches_the_engine_and_its_verdict_is_the_exit() {
let (_tmp, world, _listener) = engine(true);
assert_eq!(run(&world, &[r#"{"op":"workspaces"}"#.to_owned()]), 0);
}
#[test]
fn a_line_is_the_same_envelope() {
let (_tmp, world, _listener) = engine(true);
assert_eq!(
run(&world, &["/workspaces".to_owned()]),
0,
"the line spelling reaches the same engine"
);
}
#[test]
fn a_refusal_from_the_engine_exits_one() {
let (_tmp, world, _listener) = engine(false);
assert_eq!(run(&world, &[r#"{"op":"workspaces"}"#.to_owned()]), 1);
}
#[test]
fn a_dead_engine_exits_one() {
let (_tmp, world, listener) = engine(true);
drop(listener);
assert_eq!(run(&world, &[r#"{"op":"workspaces"}"#.to_owned()]), 1);
}
#[test]
fn an_unprovisioned_machine_refuses_with_the_remedy() {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
assert_eq!(
run(&world, &[r#"{"op":"workspaces"}"#.to_owned()]),
USAGE_EXIT
);
}
#[test]
fn a_half_provisioned_machine_refuses() {
let (_tmp, world, _listener) = engine(true);
std::fs::remove_file(material::dir(&world).join("client.key")).expect("rm");
assert_eq!(
run(&world, &[r#"{"op":"workspaces"}"#.to_owned()]),
USAGE_EXIT
);
}
#[test]
fn unusable_material_refuses_before_dialling() {
let (_tmp, world, _listener) = engine(true);
std::fs::write(material::dir(&world).join("client.pem"), "").expect("write");
assert_eq!(
run(&world, &[r#"{"op":"workspaces"}"#.to_owned()]),
USAGE_EXIT
);
}
#[test]
fn bad_argv_never_reaches_the_wire() {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
assert_eq!(run(&world, &[]), USAGE_EXIT);
assert_eq!(
run(&world, &["--nonesuch".to_owned(), "x".to_owned()]),
USAGE_EXIT
);
}
#[test]
fn help_is_answered_without_a_wire() {
let tmp = TempDir::new().expect("tmp");
let world = crate::test_support::world_under(tmp.path());
assert_eq!(run(&world, &["--help".to_owned()]), 0);
assert_eq!(run(&world, &["--help".to_owned(), "close".to_owned()]), 0);
}
#[test]
fn an_empty_stream_is_not_an_answer() {
assert_eq!(report(&[]), 1);
assert_eq!(report(&[json!({"ok": true})]), 0);
}