From 89cc175b2ee074db9860e557bf4390587411f5bc Mon Sep 17 00:00:00 2001 From: Jayesh Betala Date: Sat, 13 Jun 2026 19:15:51 +0530 Subject: [PATCH] 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. --- src/infra/disk-space.test.ts | 9 +++++++++ src/infra/disk-space.ts | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/infra/disk-space.test.ts b/src/infra/disk-space.test.ts index 269d28008ebd..580949d1bdba 100644 --- a/src/infra/disk-space.test.ts +++ b/src/infra/disk-space.test.ts @@ -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"); + }); }); diff --git a/src/infra/disk-space.ts b/src/infra/disk-space.ts index 93a6d4aa558c..c6415d074ba2 100644 --- a/src/infra/disk-space.ts +++ b/src/infra/disk-space.ts @@ -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`;