From 182608311ad9781669b26288a944cc85f28ab1cd Mon Sep 17 00:00:00 2001 From: James Russo Date: Wed, 11 Nov 2026 18:13:16 +0000 Subject: [PATCH] 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 Co-authored-by: Claude Opus 5.5 --- packages/cli/src/ui/format.test.ts | 29 +++++++++++++++++++++++++++++ packages/cli/src/ui/format.ts | 12 ++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/ui/format.test.ts b/packages/cli/src/ui/format.test.ts index f1d13fe249..938d3a4a92 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 9892c07d1d..6133be9eab 100644 --- a/packages/cli/src/ui/format.ts +++ b/packages/cli/src/ui/format.ts @@ -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`; } /**