use super::*;
use crate::tui::questionnaire::QuestionnaireResponseChannel;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind};
use pretty_assertions::assert_eq;
use rho_sdk::{HostChoice, HostInputRequest, HostInputResponse, HostQuestion, SelectionMode};
fn countdown() -> QuestionnaireComposer {
let request = HostInputRequest::questionnaire(
"Color",
vec![HostQuestion::new(
"color",
"Color?",
vec![HostChoice::new("blue", "Blue")],
SelectionMode::One,
)
.unwrap()
.allow_other()],
)
.unwrap()
.with_timeout_fallback(
HostInputResponse::new().answer("color", ["blue"]),
"Use blue",
)
.unwrap();
let (tx, _rx) = tokio::sync::oneshot::channel();
let mut composer = QuestionnaireComposer::new(request, QuestionnaireResponseChannel::new(tx));
composer.count_down_to_fallback(Duration::from_secs(60), Instant::now());
composer
}
#[test]
fn only_the_users_input_pauses_the_countdown() {
for (event, running) in [
(
Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
false,
),
(Event::Paste("blue".into()), false),
(
Event::Mouse(MouseEvent {
kind: MouseEventKind::Moved,
column: 0,
row: 0,
modifiers: KeyModifiers::NONE,
}),
false,
),
(Event::Resize(80, 24), true),
(Event::FocusGained, true),
] {
let mut composer = countdown();
composer.observe_input(&event);
assert_eq!(composer.timeout_running(), running, "{event:?}");
}
}
#[test]
fn deferred_text_edits_pause_the_countdown_without_another_terminal_event() {
for paste in [false, true] {
let mut composer = countdown();
let edited = if paste {
composer.insert_text("green")
} else {
composer.insert_char('g')
};
assert_eq!((edited, composer.timeout_running()), (true, false));
}
}
#[test]
fn a_waiting_form_counts_down_only_once_started_and_untouched() {
for (touched, expected) in [(false, (false, true, false)), (true, (false, false, true))] {
let mut composer = countdown();
composer.wait_for_countdown();
let waiting = composer.timeout_running();
if touched {
composer.observe_input(&Event::Key(KeyEvent::new(
KeyCode::Char('g'),
KeyModifiers::NONE,
)));
}
composer.start_countdown(Duration::from_secs(60), Instant::now());
assert_eq!(
(
waiting,
composer.timeout_running(),
composer.timeout_paused()
),
expected,
"touched {touched}"
);
}
}