From cd18ebc396c824a29abe26af765a9fed14b0720c Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Thu, 18 Jun 2026 14:02:47 +0800 Subject: [PATCH] fix(cli): render a sub-minute duration that rounds to 60s as "1m" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit formatDuration formats durations under a minute with toFixed(1). Values from 59.95s up round to "60.0", so the tool printed "60.0s" (or "60s" with hideTrailingZeros) — not a valid sub-minute reading — instead of "1m". Detect when the rounded value reaches 60 and render it as the minute it rounds to, matching formatDuration(60000) === "1m". --- packages/cli/src/ui/utils/formatters.test.ts | 10 ++++++++++ packages/cli/src/ui/utils/formatters.ts | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/packages/cli/src/ui/utils/formatters.test.ts b/packages/cli/src/ui/utils/formatters.test.ts index 7ef73fd9436..d0b61767994 100644 --- a/packages/cli/src/ui/utils/formatters.test.ts +++ b/packages/cli/src/ui/utils/formatters.test.ts @@ -155,6 +155,16 @@ describe('formatters', () => { expect(formatDuration(-100)).toBe('0s'); }); + it('should roll a sub-minute value up to "1m" when it rounds to 60s', () => { + // 59.95s and up round to "60.0" at one decimal, which is not a valid + // sub-minute reading; it should render as the minute it rounds to, + // matching formatDuration(60000) === '1m'. + expect(formatDuration(59949)).toBe('59.9s'); + expect(formatDuration(59950)).toBe('1m'); + expect(formatDuration(59999)).toBe('1m'); + expect(formatDuration(59950, { hideTrailingZeros: true })).toBe('1m'); + }); + describe('with hideTrailingZeros', () => { it('drops .0 suffix for whole seconds under a minute', () => { expect(formatDuration(5000, { hideTrailingZeros: true })).toBe('5s'); diff --git a/packages/cli/src/ui/utils/formatters.ts b/packages/cli/src/ui/utils/formatters.ts index 36ed878d481..657c5f0b6c5 100644 --- a/packages/cli/src/ui/utils/formatters.ts +++ b/packages/cli/src/ui/utils/formatters.ts @@ -90,6 +90,12 @@ export const formatDuration = ( if (totalSeconds < 60) { const formatted = totalSeconds.toFixed(1); + // toFixed can round up across the minute boundary (e.g. 59.95s -> "60.0"), + // which is not a valid sub-minute reading. Render it as the minute it + // rounds to, matching formatDuration(60000) === '1m'. + if (parseFloat(formatted) >= 60) { + return '1m'; + } if (options?.hideTrailingZeros && formatted.endsWith('.0')) { return `${formatted.slice(0, -2)}s`; }