use super::*;
use crate::boundary::reply::Reply;
use serde_json::json;
fn clients(workspace: &str) -> Value {
json!({"op": "clients", "workspace": workspace})
}
fn frame(link: &mut Link, question: &Value) -> Option<Landed> {
link.settle();
link.ask(question)
}
#[test]
fn a_question_nobody_has_answered_yet_lands_nothing() {
let (mut link, mut end) = pair();
assert!(frame(&mut link, &clients("home")).is_none());
assert!(
end.standing().is_empty(),
"not declared until the next frame"
);
assert!(frame(&mut link, &clients("home")).is_none());
assert_eq!(end.standing(), vec![clients("home")], "declared");
}
#[test]
fn an_answer_lands_and_the_next_frame_reads_it() {
let (mut link, end) = pair();
frame(&mut link, &clients("home"));
frame(&mut link, &clients("home"));
assert!(end.publish(&clients("home"), Ok(Reply::Attention(Vec::new()))));
assert_eq!(
frame(&mut link, &clients("home")),
Some(Ok(Reply::Attention(Vec::new()))),
"and it survives every frame that keeps asking"
);
assert!(frame(&mut link, &clients("home")).is_some());
}
#[test]
fn an_unchanged_declaration_is_not_re_sent() {
let (mut link, mut end) = pair();
for _ in 0..4 {
frame(&mut link, &clients("home"));
}
assert_eq!(end.standing(), vec![clients("home")], "sent once");
assert_eq!(end.standing(), vec![clients("home")]);
}
#[test]
fn a_question_nobody_declares_is_forgotten() {
let (mut link, mut end) = pair();
frame(&mut link, &clients("home"));
frame(&mut link, &clients("home"));
end.publish(&clients("home"), Ok(Reply::Attention(Vec::new())));
assert!(frame(&mut link, &clients("home")).is_some());
assert!(frame(&mut link, &clients("other")).is_none());
frame(&mut link, &clients("other"));
assert_eq!(end.standing(), vec![clients("other")], "the new one only");
assert!(
frame(&mut link, &clients("home")).is_none(),
"the old answer went with the old question"
);
}
#[test]
fn a_refusal_lands_like_any_other_answer() {
let (mut link, end) = pair();
frame(&mut link, &clients("nowhere"));
frame(&mut link, &clients("nowhere"));
end.publish(&clients("nowhere"), Err("unknown workspace".to_owned()));
assert_eq!(
frame(&mut link, &clients("nowhere")),
Some(Err("unknown workspace".to_owned()))
);
}
#[test]
fn a_default_link_answers_nothing_and_does_not_fail() {
let mut link = Link::default();
for _ in 0..3 {
assert!(frame(&mut link, &clients("home")).is_none());
}
}
#[test]
fn a_publish_to_a_dropped_window_fails() {
let (link, end) = pair();
drop(link);
assert!(!end.publish(&clients("home"), Ok(Reply::Attention(Vec::new()))));
}