Skip to content

Commit b8e2f0d

Browse files
committed
fix(disk-space): promote 1024 MiB to 1.0 GiB in disk warnings
formatDiskSpaceBytes tested the unrounded mib < 1024 but then rounded, so values in [1023.5, 1024) MiB printed the impossible "1024 MiB". Round before choosing the unit so they render as "1.0 GiB".
1 parent ec3aa5d commit b8e2f0d

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/infra/disk-space.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ describe("disk-space helpers", () => {
7777
expect(formatDiskSpaceBytes(420 * 1024 * 1024)).toBe("420 MiB");
7878
expect(formatDiskSpaceBytes(1536 * 1024 * 1024)).toBe("1.5 GiB");
7979
});
80+
81+
it("promotes MiB values that round up to 1024 into GiB", () => {
82+
// mib in [1023.5, 1024) rounds to 1024; must render as GiB, not "1024 MiB".
83+
expect(formatDiskSpaceBytes(Math.round(1023.6 * 1024 * 1024))).toBe("1.0 GiB");
84+
expect(formatDiskSpaceBytes(Math.round(1023.9 * 1024 * 1024))).toBe("1.0 GiB");
85+
expect(formatDiskSpaceBytes(1024 * 1024 * 1024)).toBe("1.0 GiB");
86+
// Just below the rounding boundary still reads as MiB.
87+
expect(formatDiskSpaceBytes(Math.round(1023.4 * 1024 * 1024))).toBe("1023 MiB");
88+
});
8089
});

src/infra/disk-space.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ export function tryReadDiskSpace(targetPath: string): DiskSpaceSnapshot | null {
6464
/** Formats byte counts for compact operator-facing disk-space warnings. */
6565
export function formatDiskSpaceBytes(bytes: number): string {
6666
const mib = bytes / (1024 * 1024);
67-
if (mib < 1024) {
68-
return `${Math.max(0, Math.round(mib))} MiB`;
67+
// Round before choosing the unit so a value that rounds up to 1024 MiB is
68+
// promoted to "1.0 GiB" instead of printing the impossible "1024 MiB".
69+
const roundedMib = Math.max(0, Math.round(mib));
70+
if (roundedMib < 1024) {
71+
return `${roundedMib} MiB`;
6972
}
7073
const gib = mib / 1024;
7174
return `${gib.toFixed(gib < 10 ? 1 : 0)} GiB`;

0 commit comments

Comments
 (0)