A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.
Rust
Fix byte formatting for signed, fractional and extreme values (#48)
Vishnu Srivatsava054b7cad52f3
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 1422d3c..f9950ed 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -70,6 +70,9 @@ jobs:- name: Install frontend dependenciesrun: npm ci+ - name: Test frontend helpers+ run: npm test+- name: Test daemon protocol on hostif: runner.os != 'Windows'run: python3 -m unittest discover -s tests -vdiff --git a/README.md b/README.mdindex f9ac4c0..cdc3af2 100644--- a/README.md+++ b/README.md@@ -274,6 +274,9 @@ npm run tauri dev# macOS/Linux: compiles the host daemon and uses disposable local filespython3 -m unittest discover -s tests -v+# Frontend helper tests; no app or connected device required+npm test+# Build the frontend first when testing from a clean checkoutnpm run buildcargo test --locked --manifest-path src-tauri/Cargo.toml --libdiff --git a/package.json b/package.jsonindex 377f961..b3f2bf6 100644--- a/package.json+++ b/package.json@@ -7,6 +7,7 @@"scripts": {"dev": "vite","build": "tsc && vite build",+ "test": "node --test tests/format_bytes.test.mjs","preview": "vite preview","tauri": "tauri"},diff --git a/src/App.tsx b/src/App.tsxindex 1c906be..abf1cc4 100644--- a/src/App.tsx+++ b/src/App.tsx@@ -1,6 +1,7 @@import { useState, useCallback, useEffect, useRef } from "react";import { Channel, invoke } from "@tauri-apps/api/core";import { Treemap, ResponsiveContainer, Tooltip as RechartsTooltip } from "recharts";+import { formatBytes } from "./formatBytes";import "./App.css";// ── Types ───────────────────────────────────────────────────────────────────@@ -51,14 +52,6 @@ type AppPhase = "setup" | "connecting" | "scanning" | "result";// ── Helpers ─────────────────────────────────────────────────────────────────-function formatBytes(bytes: number): string {- if (bytes === 0) return "0 B";- const units = ["B", "KB", "MB", "GB", "TB"];- const i = Math.floor(Math.log(bytes) / Math.log(1024));- const value = bytes / Math.pow(1024, i);- return `${value.toFixed(i === 0 ? 0 : 1)} ${units[i]}`;-}-function formatNumber(n: number): string {return n.toLocaleString("en-US");}diff --git a/src/formatBytes.ts b/src/formatBytes.tsnew file mode 100644index 0000000..fddad4f--- /dev/null+++ b/src/formatBytes.ts@@ -0,0 +1,21 @@+const UNITS = ["B", "KB", "MB", "GB", "TB"];++/** Keep existing 1024-based labels and precision for ordinary file sizes. */+export function formatBytes(bytes: number): string {+ if (!Number.isFinite(bytes)) return "Unknown";+ if (bytes === 0) return "0 B";++ const sign = bytes < 0 ? "-" : "";+ let value = Math.abs(bytes);+ let unit = 0;+ while (value >= 1024 && unit < UNITS.length - 1) {+ value /= 1024;+ unit++;+ }++ // Scientific notation preserves tiny nonzero values and bounds huge output.+ const amount = value < 0.1 || value >= 1e21+ ? value.toExponential(1)+ : value.toFixed(unit === 0 && Number.isInteger(value) ? 0 : 1);+ return `${sign}${amount} ${UNITS[unit]}`;+}diff --git a/tests/format_bytes.test.mjs b/tests/format_bytes.test.mjsnew file mode 100644index 0000000..f2a1805--- /dev/null+++ b/tests/format_bytes.test.mjs@@ -0,0 +1,60 @@+import assert from "node:assert/strict";+import { readFile } from "node:fs/promises";+import test from "node:test";+import ts from "typescript";++// CI uses Node 20: compile the production TypeScript with the existing compiler.+const source = await readFile(new URL("../src/formatBytes.ts", import.meta.url), "utf8");+const { outputText } = ts.transpileModule(source, {+ compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.ESNext },+});+const { formatBytes } = await import(+ `data:text/javascript;base64,${Buffer.from(outputText).toString("base64")}`+);++const cases = [+ ["zero", 0, "0 B"],+ ["negative zero", -0, "0 B"],+ ["one byte", 1, "1 B"],+ ["last byte before KB", 1023, "1023 B"],+ ["KB boundary", 1024, "1.0 KB"],+ ["KB precision", 1536, "1.5 KB"],+ ["last byte before MB", 1024 ** 2 - 1, "1024.0 KB"],+ ["MB boundary", 1024 ** 2, "1.0 MB"],+ ["last byte before GB", 1024 ** 3 - 1, "1024.0 MB"],+ ["GB boundary", 1024 ** 3, "1.0 GB"],+ ["last byte before TB", 1024 ** 4 - 1, "1024.0 GB"],+ ["TB boundary", 1024 ** 4, "1.0 TB"],+ ["fraction below one byte", 0.5, "0.5 B"],+ ["fraction above one byte", 1.5, "1.5 B"],+ ["small fraction", 0.005, "5.0e-3 B"],+ ["smallest positive number", Number.MIN_VALUE, "4.9e-324 B"],+ ["negative bytes", -1, "-1 B"],+ ["negative scaled value", -1536, "-1.5 KB"],+ ["negative fraction", -0.5, "-0.5 B"],+ ["negative small fraction", -0.005, "-5.0e-3 B"],+ ["beyond unit range", 1024 ** 5, "1024.0 TB"],+ ["largest exact integer", Number.MAX_SAFE_INTEGER, "8192.0 TB"],+ ["huge finite value", Number.MAX_VALUE, "1.6e+296 TB"],+ ["huge negative value", -Number.MAX_VALUE, "-1.6e+296 TB"],+ ["NaN", NaN, "Unknown"],+ ["positive infinity", Infinity, "Unknown"],+ ["negative infinity", -Infinity, "Unknown"],+];++for (const [label, bytes, expected] of cases) {+ test(label, () => assert.equal(formatBytes(bytes), expected));+}++test("finite values retain their sign and produce bounded, valid labels", () => {+ for (let exponent = -1074; exponent <= 1023; exponent++) {+ const magnitude = 2 ** exponent;+ for (const bytes of [magnitude, -magnitude]) {+ const result = formatBytes(bytes);+ assert.match(result, /^-?\d+(?:\.\d+)?(?:e[+-]\d+)? (?:B|KB|MB|GB|TB)$/);+ assert.equal(result.startsWith("-"), bytes < 0);+ assert.ok(result.length <= 32, result);+ assert.ok(Number(result.split(" ")[0]) !== 0, result);+ }+ }+});