use super::*;
use serde_json::json;
use tempfile::tempdir;
fn provisioned(
root: &std::path::Path,
data: &std::path::Path,
bin: &std::path::Path,
) -> ConsumerCtx {
seed(data);
let world = crate::test_support::world_under(data);
crate::test_support::wire::mint(&crate::wire::material::dir(&world));
over_world(
root,
world_of(data, &[]),
data.to_path_buf(),
fake_litany(bin),
world,
)
}
fn prepare(ws: &str) -> serde_json::Value {
json!({"op": "prepare", "workspace": ws, "payload": {"rung": "bare"}})
}
#[test]
fn a_workspace_founded_in_world_is_seen_by_the_boxs_own_seats() {
let (root, data, bin) = (tempdir().unwrap(), tempdir().unwrap(), tempdir().unwrap());
let ctx = provisioned(root.path(), data.path(), bin.path());
assert_eq!(ctx.answer(&prepare("fleet"))["kind"], "prepared");
for role in crate::wire::material::SEATS {
let client = crate::registry::Client::parse(&role.common_name()).unwrap();
assert!(
crate::registry::registered(root.path(), &client).contains("fleet"),
"{} sees what this box founded",
role.common_name()
);
}
let seated = operator(
crate::registry::Client::parse(&crate::wire::material::Role::Client.common_name()).unwrap(),
);
assert_eq!(
listed(&ctx.answer_as(&seated, &json!({"op": "workspaces"}))),
["fleet".to_owned()]
);
assert!(listed(&ctx.answer_as(&seat("stranger"), &json!({"op": "workspaces"}))).is_empty());
}
#[test]
fn a_gesture_over_a_standing_workspace_seats_nothing() {
let (root, data, bin) = (tempdir().unwrap(), tempdir().unwrap(), tempdir().unwrap());
let ctx = provisioned(root.path(), data.path(), bin.path());
assert_eq!(ctx.answer(&prepare("fleet"))["kind"], "prepared");
let window = crate::registry::window();
std::fs::remove_file(crate::registry::registrations(root.path(), &window).join("fleet"))
.expect("the operator's own revocation");
assert_eq!(ctx.answer(&prepare("fleet"))["kind"], "prepared");
assert!(
!crate::registry::registered(root.path(), &window).contains("fleet"),
"a revocation stands until the operator writes it back"
);
}
#[test]
fn a_box_with_no_material_seats_no_one() {
let (root, data, bin) = (tempdir().unwrap(), tempdir().unwrap(), tempdir().unwrap());
seed(data.path());
let ctx = over_world(
root.path(),
world_of(data.path(), &[]),
data.path().to_path_buf(),
fake_litany(bin.path()),
crate::test_support::world_under(data.path()),
);
assert_eq!(ctx.answer(&prepare("fleet"))["kind"], "prepared");
assert!(
!root.path().join(crate::registry::CLIENTS).exists(),
"no leaf, no seat"
);
}
#[test]
fn the_doctor_answers_with_a_workspace_and_without_one() {
let (root, data, bin) = (tempdir().unwrap(), tempdir().unwrap(), tempdir().unwrap());
let ctx = provisioned(root.path(), data.path(), bin.path());
assert_eq!(ctx.answer(&prepare("fleet"))["kind"], "prepared");
let bare = ctx.answer(&json!({"op": "doctor"}));
assert_eq!(bare["kind"], "doctor", "{bare}");
let checks: Vec<&str> = bare["rows"]
.as_array()
.map(|rows| {
rows.iter()
.filter_map(|row| row["check"].as_str())
.collect()
})
.unwrap_or_default();
assert_eq!(checks, ["wire", "address", "listener", "git"], "{bare}");
let scoped = ctx.answer(&json!({"op": "doctor", "workspace": "fleet"}));
let named: Vec<&str> = scoped["rows"]
.as_array()
.map(|rows| {
rows.iter()
.filter_map(|row| row["check"].as_str())
.collect()
})
.unwrap_or_default();
assert_eq!(
named,
["wire", "address", "listener", "git", "wall", "clients"],
"{scoped}"
);
let refusal = ctx.answer(&json!({"op": "doctor", "workspace": "gone"}));
assert_eq!(refusal["ok"], false, "{refusal}");
}