From 85a8fc189625c7dc688a393b023434b34d518b0e Mon Sep 17 00:00:00 2001 From: Finjineersdev Date: Sat, 28 Nov 2026 03:16:42 +0500 Subject: [PATCH] fix(cli): sizes just under the next unit no longer print as "1000.0 KB" human() moved to the next unit only at 1000, then printed one decimal, so 999,950 to 999,999 bytes showed as "1000.0 KB" (and likewise for MB and GB). It now steps up once the value would round to 1000, so they show as "1.0 MB", as Finder does. Adds a unit test of the boundaries; the test module is renamed from day_tests to format_tests since it now covers both formatters. Co-Authored-By: Claude Opus 5.5 --- engine/crates/opensweep-cli/src/main.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/engine/crates/opensweep-cli/src/main.rs b/engine/crates/opensweep-cli/src/main.rs index 18e4f85..713bcc8 100644 --- a/engine/crates/opensweep-cli/src/main.rs +++ b/engine/crates/opensweep-cli/src/main.rs @@ -1325,7 +1325,8 @@ fn human(bytes: u64) -> String { const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"]; let mut value = bytes as f64; let mut unit = 0; - while value >= 1000.0 && unit < UNITS.len() - 1 { + // 999.95 KB would print as "1000.0 KB": step up once it rounds to 1000. + while value >= 999.95 && unit < UNITS.len() - 1 { value /= 1000.0; unit += 1; } @@ -1343,11 +1344,21 @@ fn day(unix: u64) -> String { } #[cfg(test)] -mod day_tests { +mod format_tests { #[test] fn dates_are_utc_calendar_days() { assert_eq!(super::day(0), "1970-01-01"); assert_eq!(super::day(951_782_400), "2000-02-29"); assert_eq!(super::day(1_795_651_200), "2026-11-26"); } + + #[test] + fn sizes_never_print_as_a_thousand_of_a_unit() { + assert_eq!(super::human(999), "999 B"); + assert_eq!(super::human(1_000), "1.0 KB"); + assert_eq!(super::human(999_949), "999.9 KB"); + assert_eq!(super::human(999_950), "1.0 MB"); + assert_eq!(super::human(999_999_999), "1.0 GB"); + assert_eq!(super::human(1_500_000_000_000), "1.5 TB"); + } }