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 a_channel_that_cannot_answer_says_so_beside_the_ones_that_did() {
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], "(this box's own engine)");
assert!(lines[1].contains("no wire provisioned at"), "{}", lines[1]);
assert_eq!(lines[2], "alpha");
}
#[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"),
"{}",
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);
}