use crate::tui::app::{App, AppEvent};
pub fn handle_event(app: &mut App, event: AppEvent, viewport_height: u16) -> bool {
if let AppEvent::ScrollDown = &event {
let content_height = app.content_height_for_iteration(app.viewing_iteration);
app.scroll_down(viewport_height, content_height);
return app.should_quit;
}
app.update(event);
app.should_quit
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_handle_event_quit() {
let mut app = App::default();
assert!(!app.should_quit);
let should_quit = handle_event(&mut app, AppEvent::Quit, 20);
assert!(should_quit);
assert!(app.should_quit);
}
#[test]
fn test_handle_event_scroll_up() {
let mut app = App::default();
app.scroll_offset = 5;
handle_event(&mut app, AppEvent::ScrollUp, 20);
assert_eq!(app.scroll_offset, 4);
}
#[test]
fn test_handle_event_toggle_pause() {
let mut app = App::default();
assert!(!app.is_paused);
handle_event(&mut app, AppEvent::TogglePause, 20);
assert!(app.is_paused);
handle_event(&mut app, AppEvent::TogglePause, 20);
assert!(!app.is_paused);
}
#[test]
fn test_handle_event_prev_iteration() {
let mut app = App::default();
app.current_iteration = 3;
app.viewing_iteration = 3;
app.scroll_offset = 10;
handle_event(&mut app, AppEvent::PrevIteration, 20);
assert_eq!(app.viewing_iteration, 2);
assert_eq!(app.scroll_offset, 0);
app.viewing_iteration = 1;
handle_event(&mut app, AppEvent::PrevIteration, 20);
assert_eq!(app.viewing_iteration, 1);
}
#[test]
fn test_handle_event_next_iteration() {
let mut app = App::default();
app.current_iteration = 3;
app.viewing_iteration = 1;
app.scroll_offset = 10;
handle_event(&mut app, AppEvent::NextIteration, 20);
assert_eq!(app.viewing_iteration, 2);
assert_eq!(app.scroll_offset, 0);
app.viewing_iteration = 3;
handle_event(&mut app, AppEvent::NextIteration, 20);
assert_eq!(app.viewing_iteration, 3);
}
#[test]
fn test_handle_event_context_usage() {
let mut app = App::default();
handle_event(&mut app, AppEvent::ContextUsage(0.75), 20);
assert!((app.context_usage - 0.75).abs() < f64::EPSILON);
handle_event(&mut app, AppEvent::ContextUsage(1.5), 20);
assert!((app.context_usage - 1.0).abs() < f64::EPSILON);
}
#[test]
fn test_handle_event_iteration_complete() {
let mut app = App::default();
app.current_iteration = 1;
app.current_task = 0;
handle_event(&mut app, AppEvent::IterationComplete { tasks_done: 3 }, 20);
assert_eq!(app.current_task, 3);
assert_eq!(app.viewing_iteration, 1);
}
#[test]
fn test_handle_event_render() {
let mut app = App::default();
let initial_offset = app.scroll_offset;
handle_event(&mut app, AppEvent::Render, 20);
assert_eq!(app.scroll_offset, initial_offset);
assert!(!app.should_quit);
}
}