From eff0fe20e42c32a170072a7f167d978bba74d0f5 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 14 Jul 2025 14:08:00 +0300 Subject: [PATCH] test: fix errors --- src/app/utils/filesize.rs | 24 ++++++++++ src/app/utils/netspeed.rs | 24 ++++++++++ src/app/utils/unit.rs | 34 +++++++++++++- src/handler.rs | 7 ++- tests/handler.rs | 98 ++++++++++++++++++++++++++------------- 5 files changed, 153 insertions(+), 34 deletions(-) diff --git a/src/app/utils/filesize.rs b/src/app/utils/filesize.rs index 3e82174..6affe33 100644 --- a/src/app/utils/filesize.rs +++ b/src/app/utils/filesize.rs @@ -20,3 +20,27 @@ impl Display for FileSize { write!(f, "{}", UnitDisplay::new(&self.0, UNITS)) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_new() { + let file_size = FileSize::new(1024); + assert_eq!(file_size.0.as_raw(), 1024); + } + + #[test] + fn test_display() { + let file_size = FileSize::new(1024); + assert_eq!(file_size.to_string(), "1.00 KB"); + + let file_size = FileSize::new(1024 * 1024); + assert_eq!(file_size.to_string(), "1.00 MB"); + + let file_size = FileSize::new(1024 * 1024 * 1024); + assert_eq!(file_size.to_string(), "1.00 GB"); + } +} + diff --git a/src/app/utils/netspeed.rs b/src/app/utils/netspeed.rs index ea25299..b6159d6 100644 --- a/src/app/utils/netspeed.rs +++ b/src/app/utils/netspeed.rs @@ -20,3 +20,27 @@ impl Display for NetSpeed { write!(f, "{}", UnitDisplay::new(&self.0, UNITS)) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_new() { + let net_speed = NetSpeed::new(1024); + assert_eq!(net_speed.0.as_raw(), 1024); + } + + #[test] + fn test_display() { + let net_speed = NetSpeed::new(1024); + assert_eq!(net_speed.to_string(), "1.00 KB/s"); + + let net_speed = NetSpeed::new(1024 * 1024); + assert_eq!(net_speed.to_string(), "1.00 MB/s"); + + let net_speed = NetSpeed::new(1024 * 1024 * 1024); + assert_eq!(net_speed.to_string(), "1.00 GB/s"); + } +} + diff --git a/src/app/utils/unit.rs b/src/app/utils/unit.rs index b892942..0b7c82e 100644 --- a/src/app/utils/unit.rs +++ b/src/app/utils/unit.rs @@ -58,7 +58,7 @@ impl Unit { #[inline] #[must_use] - pub const fn value(&self) -> u64 { + pub const fn as_raw(&self) -> u64 { self.0 } } @@ -126,3 +126,35 @@ macro_rules! impl_unit_newtype { } }; } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_unit_from_u64() { + let unit = Unit::from(1024u64); + assert_eq!(unit.as_raw(), 1024); + } + + #[test] + fn test_unit_from_raw() { + let unit = Unit::from_raw(1024); + assert_eq!(unit.as_raw(), 1024); + } + + #[test] + fn test_unit_display() { + let unit = Unit::from_raw(1024); + let display = UnitDisplay::new(&unit, &["B", "KB", "MB"]); + assert_eq!(display.to_string(), "1.00 KB"); + + let unit = Unit::from_raw(1024 * 1024); + let display = UnitDisplay::new(&unit, &["B", "KB", "MB"]); + assert_eq!(display.to_string(), "1.00 MB"); + + let unit = Unit::from_raw(512); + let display = UnitDisplay::new(&unit, &["B", "KB", "MB"]); + assert_eq!(display.to_string(), "512 B"); + } +} \ No newline at end of file diff --git a/src/handler.rs b/src/handler.rs index f65d100..29da634 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -125,6 +125,9 @@ fn parse_keybind(key_str: &str) -> Result { for raw in key_str.split('+') { let part = raw.trim(); if part.is_empty() { + if raw.contains(' ') { + key_code = Some(KeyCode::Char(' ')); + } continue; } let low = part.to_lowercase(); @@ -176,8 +179,8 @@ fn parse_keybind(key_str: &str) -> Result { } // single‐character fallback - _ if low.len() == 1 => { - if let Some(ch) = low.trim().chars().next() { + _ if part.len() == 1 => { + if let Some(ch) = part.chars().next() { key_code = Some(KeyCode::Char(ch)); } } diff --git a/tests/handler.rs b/tests/handler.rs index aff7bcb..299c9f7 100644 --- a/tests/handler.rs +++ b/tests/handler.rs @@ -1,86 +1,122 @@ use crossterm::event::{KeyCode, KeyEvent}; -use traxor::{app::action::Action, app::App, config::Config, handler::get_action}; +use traxor::{app::App, app::action::Action, config::Config, handler::get_action}; -#[test] -fn test_get_action_quit() { +#[tokio::test] +async fn test_get_action_quit() { let config = Config::load().unwrap(); - let app = App::new(config).unwrap(); + let mut app = App::new(config).unwrap(); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('q')), &app), + get_action(KeyEvent::from(KeyCode::Char('q')), &mut app) + .await + .unwrap(), Some(Action::Quit) ); } -#[test] -fn test_get_action_navigation() { +#[tokio::test] +async fn test_get_action_navigation() { let config = Config::load().unwrap(); - let app = App::new(config).unwrap(); + let mut app = App::new(config).unwrap(); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('l')), &app), + get_action(KeyEvent::from(KeyCode::Char('l')), &mut app) + .await + .unwrap(), Some(Action::NextTab) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('h')), &app), + get_action(KeyEvent::from(KeyCode::Char('h')), &mut app) + .await + .unwrap(), Some(Action::PrevTab) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('j')), &app), + get_action(KeyEvent::from(KeyCode::Char('j')), &mut app) + .await + .unwrap(), Some(Action::NextTorrent) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('k')), &app), + get_action(KeyEvent::from(KeyCode::Char('k')), &mut app) + .await + .unwrap(), Some(Action::PrevTorrent) ); } -#[test] -fn test_get_action_switch_tab() { +#[tokio::test] +async fn test_get_action_switch_tab() { let config = Config::load().unwrap(); - let app = App::new(config).unwrap(); + let mut app = App::new(config).unwrap(); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('1')), &app), + get_action(KeyEvent::from(KeyCode::Char('1')), &mut app) + .await + .unwrap(), Some(Action::SwitchTab(0)) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('2')), &app), + get_action(KeyEvent::from(KeyCode::Char('2')), &mut app) + .await + .unwrap(), Some(Action::SwitchTab(1)) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('3')), &app), + get_action(KeyEvent::from(KeyCode::Char('3')), &mut app) + .await + .unwrap(), Some(Action::SwitchTab(2)) ); } -#[test] -fn test_get_action_torrent_actions() { +#[tokio::test] +async fn test_get_action_torrent_actions() { let config = Config::load().unwrap(); - let app = App::new(config).unwrap(); + let mut app = App::new(config).unwrap(); assert_eq!( - get_action(KeyEvent::from(KeyCode::Enter), &app), + get_action(KeyEvent::from(KeyCode::Enter), &mut app) + .await + .unwrap(), Some(Action::ToggleTorrent) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('a')), &app), + get_action(KeyEvent::from(KeyCode::Char('a')), &mut app) + .await + .unwrap(), Some(Action::ToggleAll) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('d')), &app), + get_action(KeyEvent::from(KeyCode::Char('d')), &mut app) + .await + .unwrap(), Some(Action::Delete(false)) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char('D')), &app), + get_action(KeyEvent::from(KeyCode::Char('D')), &mut app) + .await + .unwrap(), Some(Action::Delete(true)) ); assert_eq!( - get_action(KeyEvent::from(KeyCode::Char(' ')), &app), + get_action(KeyEvent::from(KeyCode::Char(' ')), &mut app) + .await + .unwrap(), Some(Action::Select) ); } -#[test] -fn test_get_action_unhandled() { +#[tokio::test] +async fn test_get_action_unhandled() { let config = Config::load().unwrap(); - let app = App::new(config).unwrap(); - assert_eq!(get_action(KeyEvent::from(KeyCode::Char('x')), &app), None); - assert_eq!(get_action(KeyEvent::from(KeyCode::F(1)), &app), None); + let mut app = App::new(config).unwrap(); + assert_eq!( + get_action(KeyEvent::from(KeyCode::Char('x')), &mut app) + .await + .unwrap(), + None + ); + assert_eq!( + get_action(KeyEvent::from(KeyCode::F(1)), &mut app) + .await + .unwrap(), + None + ); }