From 8ecd9ec8d6ab6fc61efedab464e5443ed4d364db Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 1 Jul 2025 21:57:25 +0300 Subject: [PATCH] test(app): add basic tab tests --- src/app/utils/filesize.rs | 39 ++++++++++------------- src/app/utils/mod.rs | 4 +-- src/app/utils/netspeed.rs | 41 +++++++++--------------- src/lib.rs | 2 ++ tests/app.rs | 66 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 51 deletions(-) create mode 100644 tests/app.rs diff --git a/src/app/utils/filesize.rs b/src/app/utils/filesize.rs index c197d3d..98fb7ce 100644 --- a/src/app/utils/filesize.rs +++ b/src/app/utils/filesize.rs @@ -1,31 +1,24 @@ use std::fmt; -const KB: f64 = 1e3; -const MB: f64 = 1e6; -const GB: f64 = 1e9; -const TB: f64 = 1e12; +pub struct FileSize(i64); -pub struct FileSize(pub i64); +impl From for FileSize { + fn from(bytes: i64) -> Self { + FileSize(bytes) + } +} impl fmt::Display for FileSize { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.0 == 0 { - return write!(f, "0"); + let bytes = self.0; + if bytes < 1024 { + write!(f, "{} B", bytes) + } else if bytes < 1024 * 1024 { + write!(f, "{:.2} KB", bytes as f64 / 1024.0) + } else if bytes < 1024 * 1024 * 1024 { + write!(f, "{:.2} MB", bytes as f64 / (1024.0 * 1024.0)) + } else { + write!(f, "{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0)) } - let size = self.0 as f64; - let (value, unit) = match size { - s if s >= TB => (s / TB, "TB"), - s if s >= GB => (s / GB, "GB"), - s if s >= MB => (s / MB, "MB"), - s if s >= KB => (s / KB, "KB"), - _ => (size, "B"), - }; - write!(f, "{:.2} {}", value, unit) } -} - -impl From for FileSize { - fn from(size: i64) -> Self { - FileSize(size) - } -} +} \ No newline at end of file diff --git a/src/app/utils/mod.rs b/src/app/utils/mod.rs index f9a847f..fa4f3f6 100644 --- a/src/app/utils/mod.rs +++ b/src/app/utils/mod.rs @@ -1,7 +1,7 @@ use transmission_rpc::types::{ErrorType, Torrent, TorrentGetField, TorrentStatus}; -mod filesize; -mod netspeed; +pub mod filesize; +pub mod netspeed; use crate::app::utils::filesize::FileSize; use crate::app::utils::netspeed::NetSpeed; diff --git a/src/app/utils/netspeed.rs b/src/app/utils/netspeed.rs index 980761e..215b303 100644 --- a/src/app/utils/netspeed.rs +++ b/src/app/utils/netspeed.rs @@ -1,35 +1,24 @@ use std::fmt; -const KBPS: f64 = 1_000.0; -const MBPS: f64 = 1_000_000.0; -const GBPS: f64 = 1_000_000_000.0; +pub struct NetSpeed(i64); -pub struct NetSpeed(pub i64); +impl From for NetSpeed { + fn from(bytes_per_second: i64) -> Self { + NetSpeed(bytes_per_second) + } +} impl fmt::Display for NetSpeed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let speed = self.0 as f64; - if speed == 0.0 { - return write!(f, "0 bps"); - } - - let (value, unit) = match speed { - s if s >= GBPS => (s / GBPS, "Gbps"), - s if s >= MBPS => (s / MBPS, "Mbps"), - s if s >= KBPS => (s / KBPS, "kbps"), - _ => (speed, "bps"), - }; - - if unit == "bps" { - write!(f, "{:.0} {}", value, unit) + let bytes_per_second = self.0; + if bytes_per_second < 1024 { + write!(f, "{} B/s", bytes_per_second) + } else if bytes_per_second < 1024 * 1024 { + write!(f, "{:.2} KB/s", bytes_per_second as f64 / 1024.0) + } else if bytes_per_second < 1024 * 1024 * 1024 { + write!(f, "{:.2} MB/s", bytes_per_second as f64 / (1024.0 * 1024.0)) } else { - write!(f, "{:.2} {}", value, unit) + write!(f, "{:.2} GB/s", bytes_per_second as f64 / (1024.0 * 1024.0 * 1024.0)) } } -} - -impl From for NetSpeed { - fn from(speed: i64) -> Self { - NetSpeed(speed) - } -} +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 85ad8bf..e9095b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,3 +12,5 @@ pub mod tui; /// Event handler. pub mod handler; + + diff --git a/tests/app.rs b/tests/app.rs new file mode 100644 index 0000000..96cada3 --- /dev/null +++ b/tests/app.rs @@ -0,0 +1,66 @@ +use traxor::app::App; + +#[test] +fn test_app_creation() { + let app = App::new().unwrap(); + assert_eq!(app.tabs().len(), 3); +} + +#[test] +fn test_app_quit() { + let mut app = App::new().unwrap(); + app.quit(); + assert!(!app.running); +} + +#[test] +fn test_app_next_tab() { + let mut app = App::new().unwrap(); + assert_eq!(app.index(), 0); + app.next_tab(); + assert_eq!(app.index(), 1); + app.next_tab(); + assert_eq!(app.index(), 2); + app.next_tab(); + assert_eq!(app.index(), 0); // Wraps around +} + +#[test] +fn test_app_prev_tab() { + let mut app = App::new().unwrap(); + assert_eq!(app.index(), 0); + app.prev_tab(); + assert_eq!(app.index(), 2); // Wraps around + app.prev_tab(); + assert_eq!(app.index(), 1); +} + +#[test] +fn test_app_switch_tab() { + let mut app = App::new().unwrap(); + assert_eq!(app.index(), 0); + app.switch_tab(2); + assert_eq!(app.index(), 2); + app.switch_tab(0); + assert_eq!(app.index(), 0); +} + +#[test] +fn test_app_toggle_popup() { + let mut app = App::new().unwrap(); + assert!(!app.show_popup); + app.toggle_popup(); + assert!(app.show_popup); + app.toggle_popup(); + assert!(!app.show_popup); +} + +#[test] +fn test_app_open_close_popup() { + let mut app = App::new().unwrap(); + assert!(!app.show_popup); + app.open_popup(); + assert!(app.show_popup); + app.close_popup(); + assert!(!app.show_popup); +}