use super::super::super::Capture;
use super::super::read::HAND_OFFS;
use super::{call, quick, ran, took};
#[test]
fn work_handed_to_a_read_that_never_answered_is_offered_again() {
let mail = quick();
let id = mail.post(10, "local", &call("laptop", "Bash"));
assert_eq!(took(&mail, "laptop").len(), 1, "handed over once");
assert_eq!(
took(&mail, "laptop").first().map(|i| i.id.clone()),
Some(id),
"the next read is the acknowledgement the first one never gave"
);
}
#[test]
fn an_answered_invocation_is_not_offered_again() {
let mail = quick();
let id = mail.post(10, "local", &call("laptop", "Bash"));
assert_eq!(took(&mail, "laptop").len(), 1);
assert_eq!(mail.complete("laptop", &id, &ran(0)), Ok(ran(0)));
assert!(
took(&mail, "laptop").is_empty(),
"answered work is finished work"
);
}
#[test]
fn a_read_acknowledges_only_its_own_clients_work() {
let mail = quick();
let theirs = mail.post(10, "local", &call("phone", "Bash"));
assert_eq!(took(&mail, "phone").len(), 1);
assert!(took(&mail, "laptop").is_empty());
assert_eq!(
took(&mail, "phone").first().map(|i| i.id.clone()),
Some(theirs)
);
}
#[test]
fn a_third_unanswered_hand_off_is_answered_in_doubt_naming_the_count() {
let mail = quick();
let id = mail.post(10, "local", &call("laptop", "Bash"));
for _ in 0..HAND_OFFS {
assert_eq!(
took(&mail, "laptop").first().map(|i| i.id.clone()),
Some(id.clone()),
"every read up to the bound is handed it again"
);
}
assert!(
took(&mail, "laptop").is_empty(),
"the fourth read is answered by the engine instead"
);
let said = mail
.collect("local", &id)
.expect("the asker's own handle")
.expect("the engine wrote the capture itself");
assert_eq!(said.stdout, String::new(), "nothing ran here");
assert_ne!(said.exit_code, 0, "a failed tool result, and read as one");
assert!(said.stderr.contains("\"laptop\""), "{}", said.stderr);
assert!(said.stderr.contains("3 times"), "{}", said.stderr);
assert!(
said.stderr.contains("each hand-off may have run it"),
"{}",
said.stderr
);
assert!(
said.stderr.contains("read the world before acting again"),
"{}",
said.stderr
);
}
#[test]
fn an_in_doubt_slot_is_not_offered_again() {
let mail = quick();
mail.post(10, "local", &call("laptop", "Bash"));
for _ in 0..=HAND_OFFS {
took(&mail, "laptop");
}
assert!(
took(&mail, "laptop").is_empty(),
"an answered slot is finished work, whoever answered it"
);
}
#[test]
fn a_late_completion_overwrites_an_uncollected_in_doubt_answer() {
let mail = quick();
let id = mail.post(10, "local", &call("laptop", "Bash"));
for _ in 0..=HAND_OFFS {
took(&mail, "laptop");
}
let landed = mail.complete("laptop", &id, &ran(0)).expect("in flight");
assert_eq!(landed.stdout, "out", "the box's own capture, kept whole");
assert_eq!(landed.exit_code, 0);
assert!(landed.stderr.contains("3 times"), "{}", landed.stderr);
assert_eq!(
mail.collect("local", &id),
Ok(Some(landed)),
"the driver collects what the engine stored, mark and all"
);
assert!(
mail.complete("laptop", &id, &ran(0)).is_err(),
"after the collect the handle is spent"
);
}
#[test]
fn a_capture_for_a_redelivered_invocation_carries_the_count() {
let mail = quick();
let id = mail.post(10, "local", &call("laptop", "Bash"));
assert_eq!(took(&mail, "laptop").len(), 1);
assert_eq!(took(&mail, "laptop").len(), 1);
let said = mail
.complete(
"laptop",
&id,
&Capture {
stdout: "2".to_owned(),
stderr: "warning: already applied".to_owned(),
exit_code: 0,
},
)
.expect("in flight");
assert_eq!(said.stdout, "2", "the box's own output is untouched");
assert_eq!(said.exit_code, 0, "and so is its verdict");
assert!(said.stderr.contains("\"laptop\""), "{}", said.stderr);
assert!(said.stderr.contains("2 times"), "{}", said.stderr);
assert!(
said.stderr
.contains("may be the effect of more than one run"),
"{}",
said.stderr
);
assert!(
said.stderr.ends_with("warning: already applied"),
"the mark goes first, and the tool's own stderr stays: {}",
said.stderr
);
assert_eq!(mail.collect("local", &id), Ok(Some(said)));
}
#[test]
fn a_capture_delivered_once_carries_no_mark() {
let mail = quick();
let id = mail.post(10, "local", &call("laptop", "Bash"));
assert_eq!(took(&mail, "laptop").len(), 1);
assert_eq!(
mail.complete("laptop", &id, &ran(0)),
Ok(ran(0)),
"the capture the box computed, verbatim"
);
}