Skip to content

Commit 8d31469

Browse files
arhxamclaude
andcommitted
fix(shared): roll a sub-minute duration up to "1m" instead of "60s"
`formatDuration` renders durations under a minute with `${Math.round(durationMs / 1_000)}s`. For any value in [59_500, 60_000) that rounds to 60 and prints "60s". The >= 60_000 branch already guards this exact case (`if (seconds === 60) return `${minutes + 1}m``), so the sub-minute branch was inconsistent with it: `formatDuration(59_500)` gave "60s" while `formatDuration(60_000)` gives "1m". Round first and roll 60 up to "1m". This is user-visible via thread duration display (`apps/mobile/src/lib/threadActivity.ts`). Added a test file for `formatDuration` covering the boundary and the surrounding formats. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent e482960 commit 8d31469

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { formatDuration } from "./orchestrationTiming.ts";
4+
5+
describe("formatDuration", () => {
6+
it("formats sub-second and second durations", () => {
7+
expect(formatDuration(0)).toBe("1ms");
8+
expect(formatDuration(250)).toBe("250ms");
9+
expect(formatDuration(1_500)).toBe("1.5s");
10+
expect(formatDuration(12_000)).toBe("12s");
11+
});
12+
13+
it("rolls a sub-minute duration up to 1m instead of 60s", () => {
14+
// Math.round(59_500 / 1_000) === 60, which must render as "1m", not "60s",
15+
// matching the >= 60_000 branch (formatDuration(60_000) === "1m").
16+
expect(formatDuration(59_499)).toBe("59s");
17+
expect(formatDuration(59_500)).toBe("1m");
18+
expect(formatDuration(60_000)).toBe("1m");
19+
});
20+
21+
it("formats minute durations", () => {
22+
expect(formatDuration(90_000)).toBe("1m 30s");
23+
expect(formatDuration(120_000)).toBe("2m");
24+
});
25+
26+
it("guards against invalid input", () => {
27+
expect(formatDuration(-1)).toBe("0ms");
28+
expect(formatDuration(Number.NaN)).toBe("0ms");
29+
});
30+
});

‎packages/shared/src/orchestrationTiming.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ export function formatDuration(durationMs: number): string {
1313
if (!Number.isFinite(durationMs) || durationMs < 0) return "0ms";
1414
if (durationMs < 1_000) return `${Math.max(1, Math.round(durationMs))}ms`;
1515
if (durationMs < 10_000) return `${(durationMs / 1_000).toFixed(1)}s`;
16-
if (durationMs < 60_000) return `${Math.round(durationMs / 1_000)}s`;
16+
if (durationMs < 60_000) {
17+
// Rounding can land on 60 (e.g. 59_500ms); roll that up to "1m" instead of
18+
// rendering "60s", matching the seconds === 60 guard in the branch below.
19+
const seconds = Math.round(durationMs / 1_000);
20+
return seconds === 60 ? "1m" : `${seconds}s`;
21+
}
1722
const minutes = Math.floor(durationMs / 60_000);
1823
const seconds = Math.round((durationMs % 60_000) / 1_000);
1924
if (seconds === 0) return `${minutes}m`;

0 commit comments

Comments
 (0)