diff --git a/packages/cli/src/ui/format.test.ts b/packages/cli/src/ui/format.test.ts index f1d13fe2496..938d3a4a92a 100644 --- a/packages/cli/src/ui/format.test.ts +++ b/packages/cli/src/ui/format.test.ts @@ -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"); + }); +}); diff --git a/packages/cli/src/ui/format.ts b/packages/cli/src/ui/format.ts index df3d2683d20..c908beb375f 100644 --- a/packages/cli/src/ui/format.ts +++ b/packages/cli/src/ui/format.ts @@ -3,16 +3,16 @@ import { c } from "./colors.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`; } /**