mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 04:47:03 -06:00
fix(disk-space): promote rounded GiB boundary
Round MiB before selecting the display unit so low-disk warnings do not render boundary values as 1024 MiB. Adds regression coverage for the GiB boundary. Fixes #90245.
This commit is contained in:
@@ -77,4 +77,13 @@ describe("disk-space helpers", () => {
|
||||
expect(formatDiskSpaceBytes(420 * 1024 * 1024)).toBe("420 MiB");
|
||||
expect(formatDiskSpaceBytes(1536 * 1024 * 1024)).toBe("1.5 GiB");
|
||||
});
|
||||
|
||||
it("promotes MiB values that round up to 1024 into GiB", () => {
|
||||
// mib in [1023.5, 1024) rounds to 1024; must render as GiB, not "1024 MiB".
|
||||
expect(formatDiskSpaceBytes(Math.round(1023.6 * 1024 * 1024))).toBe("1.0 GiB");
|
||||
expect(formatDiskSpaceBytes(Math.round(1023.9 * 1024 * 1024))).toBe("1.0 GiB");
|
||||
expect(formatDiskSpaceBytes(1024 * 1024 * 1024)).toBe("1.0 GiB");
|
||||
// Just below the rounding boundary still reads as MiB.
|
||||
expect(formatDiskSpaceBytes(Math.round(1023.4 * 1024 * 1024))).toBe("1023 MiB");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -65,8 +65,11 @@ export function tryReadDiskSpace(targetPath: string): DiskSpaceSnapshot | null {
|
||||
/** Formats byte counts for compact operator-facing disk-space warnings. */
|
||||
export function formatDiskSpaceBytes(bytes: number): string {
|
||||
const mib = bytes / (1024 * 1024);
|
||||
if (mib < 1024) {
|
||||
return `${Math.max(0, Math.round(mib))} MiB`;
|
||||
// Round before choosing the unit so a value that rounds up to 1024 MiB is
|
||||
// promoted to "1.0 GiB" instead of printing the impossible "1024 MiB".
|
||||
const roundedMib = Math.max(0, Math.round(mib));
|
||||
if (roundedMib < 1024) {
|
||||
return `${roundedMib} MiB`;
|
||||
}
|
||||
const gib = mib / 1024;
|
||||
return `${gib.toFixed(gib < 10 ? 1 : 0)} GiB`;
|
||||
|
||||
Reference in New Issue
Block a user