use super::*;
use pretty_assertions::assert_eq;
const ROOT: &str = "root";
fn options(mode: &str) -> Vec<v2::SessionConfigOption> {
let values = ["default", "plan", "bypassPermissions"]
.map(|value| v2::SessionConfigSelectOption::new(value, value));
vec![v2::SessionConfigOption::select(
"mode",
"Mode",
mode.to_owned(),
values.to_vec(),
)]
}
fn mode_update(session: &str, mode: &str) -> Inbound {
Inbound::Update(Box::new(v2::UpdateSessionNotification::new(
session.to_owned(),
v2::SessionUpdate::ConfigOptionUpdate(v2::ConfigOptionUpdate::new(options(mode))),
)))
}
fn running() -> Inbound {
Inbound::Update(Box::new(v2::UpdateSessionNotification::new(
ROOT.to_owned(),
v2::SessionUpdate::StateUpdate(v2::StateUpdate::Running(v2::RunningStateUpdate::new())),
)))
}
fn mode_of(options: &[v2::SessionConfigOption]) -> String {
match &options[0].kind {
v2::SessionConfigKind::Select(select) => select.current_value.to_string(),
_ => unreachable!("the test sets selects only"),
}
}
fn label(inbound: &Inbound) -> String {
match inbound {
Inbound::Update(notification) => match ¬ification.update {
v2::SessionUpdate::ConfigOptionUpdate(update) => {
format!(
"{} mode {}",
notification.session_id,
mode_of(&update.config_options)
)
}
_ => format!("{} update", notification.session_id),
},
Inbound::Took(_) => "took".to_owned(),
Inbound::Answered => "answered".to_owned(),
}
}
#[tokio::test]
async fn setup_reads_answers_and_config_updates_in_order() {
let cases = [
(
"a stale update before the answer loses to it",
vec![mode_update(ROOT, "bypassPermissions")],
vec![],
("default", None, vec![]),
),
(
"an update after the answer moves the options on",
vec![],
vec![mode_update(ROOT, "bypassPermissions")],
("default", Some("bypassPermissions"), vec![]),
),
(
"another session's update is no option of the root's",
vec![],
vec![mode_update("child", "bypassPermissions")],
("default", None, vec!["child mode bypassPermissions"]),
),
(
"other updates wait for the turn in order",
vec![running()],
vec![running(), mode_update(ROOT, "plan")],
("default", Some("plan"), vec!["root update", "root update"]),
),
];
for (case, before, after, expected) in cases {
let (mut updates, tx) = Updates::new();
for inbound in before {
tx.send(inbound).unwrap();
}
tx.send(Inbound::Answered).unwrap();
for inbound in after {
tx.send(inbound).unwrap();
}
let (answer_tx, answer_rx) = oneshot::channel();
answer_tx.send(Ok(options("default"))).unwrap();
let answered = mode_of(&updates.answer(answer_rx).await.unwrap());
let latest = updates
.latest_options(ROOT)
.map(|options| mode_of(&options));
let waiting: Vec<String> = updates.queued.iter().map(label).collect();
assert_eq!(
(answered.as_str(), latest.as_deref(), waiting),
(
expected.0,
expected.1,
expected.2.into_iter().map(str::to_owned).collect()
),
"{case}"
);
}
}