use super::fanned;
use crate::channel::entries::WORKSPACE;
use crate::cli::Stream;
use crate::render::Form;
use crate::test_support::wire::{entry, flat, wired, yes};
use crate::test_support::{Scratch, mint};
use serde_json::json;
#[test]
fn every_channel_answers_under_its_own_name() {
let scratch = Scratch::new();
let own = wired(&scratch, &flat(), vec![vec![yes()]]);
let alpha = wired(
&scratch,
&entry("alpha"),
vec![vec![json!({"ok": true, "n": 1})]],
);
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
assert_eq!(verdict.code, 0);
assert_eq!(verdict.stream, Stream::Out);
assert_eq!(
verdict.text,
format!(
"(this box's own engine)\n {}\nalpha\n {}",
yes(),
json!({"ok": true, "n": 1})
)
);
assert!(own.heard().contains(&json!({"op": "workspaces"})));
assert!(alpha.heard().contains(&json!({"op": "workspaces"})));
}
#[test]
fn a_box_with_no_flat_channel_still_answers_from_its_entries() {
let scratch = Scratch::new();
wired(&scratch, &entry("alpha"), vec![vec![yes()]]);
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
assert_eq!(verdict.code, 0, "{}", verdict.text);
assert!(
verdict.text.contains(&yes().to_string()),
"{}",
verdict.text
);
}
#[test]
fn an_unprovisioned_own_engine_says_nothing_beside_the_channels_that_answered() {
let scratch = Scratch::new();
wired(&scratch, &entry("alpha"), vec![vec![yes()]]);
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
let lines: Vec<&str> = verdict.text.lines().collect();
assert_eq!(lines[0], "alpha", "{}", verdict.text);
assert!(
!verdict.text.contains("no wire provisioned"),
"{}",
verdict.text
);
}
#[test]
fn a_flat_root_that_holds_something_broken_still_says_so() {
let scratch = Scratch::new();
wired(&scratch, &entry("alpha"), vec![vec![yes()]]);
let bare = scratch.path().join(flat());
std::fs::create_dir_all(&bare).expect("the flat root");
std::fs::write(bare.join(crate::channel::material::ADDRESS), "h:1\n")
.expect("half the material");
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
let lines: Vec<&str> = verdict.text.lines().collect();
assert_eq!(lines[0], "(this box's own engine)");
assert!(lines[1].contains("half-provisioned"), "{}", lines[1]);
assert_eq!(lines[2], "alpha");
}
#[test]
fn a_box_that_holds_nothing_at_all_is_still_told_how_to_provision_one() {
let scratch = Scratch::new();
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
assert_eq!(verdict.code, 1);
assert!(
verdict.text.contains("no wire provisioned at"),
"{}",
verdict.text
);
}
#[test]
fn a_renamed_entry_is_stamped_the_way_the_listing_names_it() {
let scratch = Scratch::new();
wired(&scratch, &entry("alpha"), vec![vec![yes()]]);
std::fs::write(
scratch.path().join(entry("alpha")).join(WORKSPACE),
"personal",
)
.expect("the workspace file");
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
assert!(
verdict
.text
.contains(r#"alpha (named "personal" on its host)"#),
"{}",
verdict.text
);
}
#[test]
fn a_box_no_channel_of_which_answers_fails() {
let scratch = Scratch::new();
mint::provisioned(&scratch.path().join(entry("alpha")), "127.0.0.1:1");
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
assert_eq!(verdict.code, 1);
assert!(
verdict.text.contains("connect 127.0.0.1:1"),
"{}",
verdict.text
);
assert!(
!verdict.text.contains("no wire provisioned at"),
"a channel that answered nothing is still a channel this box holds, \
and the absent one is not: {}",
verdict.text
);
}
#[test]
fn a_channel_that_answers_no_is_printed_and_does_not_count_as_an_answer() {
let scratch = Scratch::new();
wired(
&scratch,
&flat(),
vec![vec![json!({"ok": false, "said": "no"})]],
);
let verdict = fanned(scratch.path(), &json!({"op": "workspaces"}), Form::Json);
assert_eq!(verdict.code, 1);
assert_eq!(verdict.stream, Stream::Out, "an answer is not a diagnosis");
assert!(verdict.text.contains(r#""said":"no""#), "{}", verdict.text);
}