use flash_watcher::stats::{format_duration, StatsCollector};
use std::time::Duration;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stats_collector_new() {
let stats = StatsCollector::new();
assert_eq!(stats.file_changes, 0);
assert_eq!(stats.watcher_calls, 0);
assert!(stats.start_time.elapsed().as_secs() < 1); }
#[test]
fn test_record_file_change() {
let mut stats = StatsCollector::new();
assert_eq!(stats.file_changes, 0);
stats.record_file_change();
assert_eq!(stats.file_changes, 1);
stats.record_file_change();
assert_eq!(stats.file_changes, 2);
for _ in 0..10 {
stats.record_file_change();
}
assert_eq!(stats.file_changes, 12);
}
#[test]
fn test_record_watcher_call() {
let mut stats = StatsCollector::new();
assert_eq!(stats.watcher_calls, 0);
stats.record_watcher_call();
assert_eq!(stats.watcher_calls, 1);
stats.record_watcher_call();
assert_eq!(stats.watcher_calls, 2);
for _ in 0..100 {
stats.record_watcher_call();
}
assert_eq!(stats.watcher_calls, 102);
}
#[test]
fn test_update_resource_usage() {
let mut stats = StatsCollector::new();
assert_eq!(stats.last_memory_usage, 0);
assert_eq!(stats.last_cpu_usage, 0.0);
stats.update_resource_usage();
assert!(stats.last_memory_usage < 1024 * 1024 * 1024); assert!(stats.last_cpu_usage >= 0.0);
}
#[test]
fn test_display_stats() {
let mut stats = StatsCollector::new();
stats.record_file_change();
stats.record_file_change();
stats.record_watcher_call();
stats.record_watcher_call();
stats.record_watcher_call();
stats.update_resource_usage();
stats.display_stats();
assert_eq!(stats.file_changes, 2);
assert_eq!(stats.watcher_calls, 3);
}
#[test]
fn test_format_duration_seconds() {
assert_eq!(format_duration(Duration::from_secs(0)), "0s");
assert_eq!(format_duration(Duration::from_secs(1)), "1s");
assert_eq!(format_duration(Duration::from_secs(30)), "30s");
assert_eq!(format_duration(Duration::from_secs(59)), "59s");
}
#[test]
fn test_format_duration_minutes() {
assert_eq!(format_duration(Duration::from_secs(60)), "1m 0s");
assert_eq!(format_duration(Duration::from_secs(61)), "1m 1s");
assert_eq!(format_duration(Duration::from_secs(90)), "1m 30s");
assert_eq!(format_duration(Duration::from_secs(120)), "2m 0s");
assert_eq!(format_duration(Duration::from_secs(3599)), "59m 59s");
}
#[test]
fn test_format_duration_hours() {
assert_eq!(format_duration(Duration::from_secs(3600)), "1h 0m 0s");
assert_eq!(format_duration(Duration::from_secs(3661)), "1h 1m 1s");
assert_eq!(format_duration(Duration::from_secs(7200)), "2h 0m 0s");
assert_eq!(format_duration(Duration::from_secs(7323)), "2h 2m 3s");
assert_eq!(format_duration(Duration::from_secs(86400)), "24h 0m 0s");
}
#[test]
fn test_format_duration_edge_cases() {
assert_eq!(format_duration(Duration::from_millis(500)), "0s");
assert_eq!(format_duration(Duration::from_millis(999)), "0s");
assert_eq!(format_duration(Duration::from_secs(90061)), "25h 1m 1s");
assert_eq!(format_duration(Duration::from_secs(359999)), "99h 59m 59s");
}
#[test]
fn test_stats_collector_concurrent_access() {
use std::sync::{Arc, Mutex};
use std::thread;
let stats = Arc::new(Mutex::new(StatsCollector::new()));
let mut handles = vec![];
for _ in 0..10 {
let stats_clone = Arc::clone(&stats);
let handle = thread::spawn(move || {
for _ in 0..100 {
{
let mut stats = stats_clone.lock().unwrap();
stats.record_file_change();
}
{
let mut stats = stats_clone.lock().unwrap();
stats.record_watcher_call();
}
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
let final_stats = stats.lock().unwrap();
assert_eq!(final_stats.file_changes, 1000); assert_eq!(final_stats.watcher_calls, 1000); }
#[test]
fn test_stats_collector_uptime() {
let stats = StatsCollector::new();
std::thread::sleep(Duration::from_millis(10));
let uptime = stats.start_time.elapsed();
assert!(uptime >= Duration::from_millis(10));
assert!(uptime < Duration::from_secs(1)); }
#[test]
fn test_stats_collector_memory_usage_bounds() {
let mut stats = StatsCollector::new();
stats.update_resource_usage();
assert!(stats.last_memory_usage < 1024 * 1024 * 100); }
#[test]
fn test_stats_collector_cpu_usage_bounds() {
let mut stats = StatsCollector::new();
stats.update_resource_usage();
assert!(stats.last_cpu_usage >= 0.0);
assert!(stats.last_cpu_usage <= 200.0); }
#[test]
fn test_stats_collector_multiple_updates() {
let mut stats = StatsCollector::new();
for _ in 0..5 {
stats.update_resource_usage();
std::thread::sleep(Duration::from_millis(1));
}
assert!(stats.last_memory_usage < 1024 * 1024 * 1024); assert!(stats.last_cpu_usage >= 0.0);
}
}