fix(cli): carry rounded-up seconds and kilobytes into the next unit (#7565)

formatDuration rounded seconds after splitting off minutes, so 59.96s printed
"60.0s" and 119.97s printed "1m 60.0s". formatBytes compared KB against the MB
threshold before rounding, so 1,048,575 bytes printed "1024.0 KB". Round first,
then split or compare.

Takes over #5817 by @rudycelekli, whose commits could not be merged unsigned.

Co-authored-by: Rudy Celekli <rudy@gradiahq.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-11 18:13:16 +00:00
committed by GitHub
co-authored by Rudy Celekli Claude Opus 5.5
parent ef5cdeb0a0
commit 182608311a
2 changed files with 35 additions and 6 deletions
+29
View File
@@ -1,6 +1,8 @@
import { describe, expect, it } from "vitest";
import {
formatBytes,
formatDuration,
formatRenderPipelineDetail,
formatRenderSummaryDetail,
formatScreenshotFallbackHint,
@@ -168,3 +170,30 @@ describe("formatRenderSummaryDetail", () => {
);
});
});
describe("formatDuration", () => {
it("shows seconds with one decimal under a minute", () => {
expect(formatDuration(0)).toBe("0.0s");
expect(formatDuration(1234)).toBe("1.2s");
expect(formatDuration(59_940)).toBe("59.9s");
});
it("carries a rounded-up 60.0 seconds into the next minute", () => {
expect(formatDuration(59_960)).toBe("1m 0.0s");
expect(formatDuration(119_970)).toBe("2m 0.0s");
});
it("splits minutes and seconds past one minute", () => {
expect(formatDuration(60_000)).toBe("1m 0.0s");
expect(formatDuration(83_400)).toBe("1m 23.4s");
});
});
describe("formatBytes", () => {
it("steps up a unit when rounding reaches the next one", () => {
expect(formatBytes(1023)).toBe("1023 B");
expect(formatBytes(1536)).toBe("1.5 KB");
expect(formatBytes(1_048_575)).toBe("1.0 MB");
expect(formatBytes(1_048_576)).toBe("1.0 MB");
});
});
+6 -6
View File
@@ -4,16 +4,16 @@ import { recordFailure } from "../utils/jsonError.js";
export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
const kb = Math.round((bytes / 1024) * 10) / 10;
if (kb < 1024) return `${kb.toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
export function formatDuration(ms: number): string {
const seconds = ms / 1000;
if (seconds < 60) return `${seconds.toFixed(1)}s`;
const minutes = Math.floor(seconds / 60);
const remaining = seconds - minutes * 60;
return `${minutes}m ${remaining.toFixed(1)}s`;
const tenths = Math.round(ms / 100);
if (tenths < 600) return `${(tenths / 10).toFixed(1)}s`;
const minutes = Math.floor(tenths / 600);
return `${minutes}m ${((tenths - minutes * 600) / 10).toFixed(1)}s`;
}
/**