test(app): add basic tab tests

This commit is contained in:
Kristofers Solo 2025-07-01 21:57:25 +03:00
parent 135a8a0c39
commit 8ecd9ec8d6
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
5 changed files with 101 additions and 51 deletions

View File

@ -1,31 +1,24 @@
use std::fmt; use std::fmt;
const KB: f64 = 1e3; pub struct FileSize(i64);
const MB: f64 = 1e6;
const GB: f64 = 1e9;
const TB: f64 = 1e12;
pub struct FileSize(pub i64); impl From<i64> for FileSize {
fn from(bytes: i64) -> Self {
FileSize(bytes)
}
}
impl fmt::Display for FileSize { impl fmt::Display for FileSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0 == 0 { let bytes = self.0;
return write!(f, "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<i64> for FileSize {
fn from(size: i64) -> Self {
FileSize(size)
} }
} }

View File

@ -1,7 +1,7 @@
use transmission_rpc::types::{ErrorType, Torrent, TorrentGetField, TorrentStatus}; use transmission_rpc::types::{ErrorType, Torrent, TorrentGetField, TorrentStatus};
mod filesize; pub mod filesize;
mod netspeed; pub mod netspeed;
use crate::app::utils::filesize::FileSize; use crate::app::utils::filesize::FileSize;
use crate::app::utils::netspeed::NetSpeed; use crate::app::utils::netspeed::NetSpeed;

View File

@ -1,35 +1,24 @@
use std::fmt; use std::fmt;
const KBPS: f64 = 1_000.0; pub struct NetSpeed(i64);
const MBPS: f64 = 1_000_000.0;
const GBPS: f64 = 1_000_000_000.0;
pub struct NetSpeed(pub i64); impl From<i64> for NetSpeed {
fn from(bytes_per_second: i64) -> Self {
NetSpeed(bytes_per_second)
}
}
impl fmt::Display for NetSpeed { impl fmt::Display for NetSpeed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let speed = self.0 as f64; let bytes_per_second = self.0;
if speed == 0.0 { if bytes_per_second < 1024 {
return write!(f, "0 bps"); 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)
let (value, unit) = match speed { } else if bytes_per_second < 1024 * 1024 * 1024 {
s if s >= GBPS => (s / GBPS, "Gbps"), write!(f, "{:.2} MB/s", bytes_per_second as f64 / (1024.0 * 1024.0))
s if s >= MBPS => (s / MBPS, "Mbps"),
s if s >= KBPS => (s / KBPS, "kbps"),
_ => (speed, "bps"),
};
if unit == "bps" {
write!(f, "{:.0} {}", value, unit)
} else { } else {
write!(f, "{:.2} {}", value, unit) write!(f, "{:.2} GB/s", bytes_per_second as f64 / (1024.0 * 1024.0 * 1024.0))
} }
} }
} }
impl From<i64> for NetSpeed {
fn from(speed: i64) -> Self {
NetSpeed(speed)
}
}

View File

@ -12,3 +12,5 @@ pub mod tui;
/// Event handler. /// Event handler.
pub mod handler; pub mod handler;

66
tests/app.rs Normal file
View File

@ -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);
}