diff --git a/apps/status-page/src/lib/formatter.test.ts b/apps/status-page/src/lib/formatter.test.ts index bcba4dde..1e832ebc 100644 --- a/apps/status-page/src/lib/formatter.test.ts +++ b/apps/status-page/src/lib/formatter.test.ts @@ -6,6 +6,8 @@ import { formatDateRange, formatDateRangeParts, formatDateTime, + formatMilliseconds, + formatMillisecondsRange, formatTime, } from "./formatter"; @@ -60,6 +62,36 @@ describe("formatter UTC rendering", () => { }); }); +describe("formatMilliseconds", () => { + test("renders sub-second values with the millisecond unit", () => { + expect(formatMilliseconds(111)).toBe("111 ms"); + }); + + test("converts values above 1000ms to seconds", () => { + expect(formatMilliseconds(1500)).toBe("1.5 sec"); + }); +}); + +describe("formatMillisecondsRange", () => { + // Regression: both endpoints below 1000ms must stay in milliseconds — the + // `min` side was previously divided by 1000, rendering 111ms as "0.111". + test("keeps both endpoints in milliseconds when both are below 1000", () => { + expect(formatMillisecondsRange(111, 155)).toBe("111 - 155 ms"); + }); + + test("does not divide the min endpoint by 1000 for sub-second ranges", () => { + expect(formatMillisecondsRange(111, 155)).not.toContain("0.111"); + }); + + test("shares the seconds unit when both endpoints exceed 1000", () => { + expect(formatMillisecondsRange(1500, 2300)).toBe("1.5 - 2.3 sec"); + }); + + test("formats each endpoint with its own unit for a mixed range", () => { + expect(formatMillisecondsRange(500, 1500)).toBe("500 ms - 1.5 sec"); + }); +}); + describe("formatDateRange", () => { test("collapses the `to` side to a time when both fall on the same UTC day", () => { expect( diff --git a/apps/status-page/src/lib/formatter.ts b/apps/status-page/src/lib/formatter.ts index 1ceff579..e5e554f5 100644 --- a/apps/status-page/src/lib/formatter.ts +++ b/apps/status-page/src/lib/formatter.ts @@ -17,10 +17,17 @@ export function formatMilliseconds(ms: number) { } export function formatMillisecondsRange(min: number, max: number) { - if ((min > 1000 && max > 1000) || (min < 1000 && max < 1000)) { - return `${formatNumber(min / 1000)} - ${formatMilliseconds(max)}`; + // Both above 1000ms: share the seconds unit, so only `max` carries it. + if (min > 1000 && max > 1000) { + return `${formatNumber(min / 1000, { maximumFractionDigits: 2 })} - ${formatMilliseconds(max)}`; } + // Both below 1000ms: share the milliseconds unit, so only `max` carries it. + if (min < 1000 && max < 1000) { + return `${formatNumber(min)} - ${formatMilliseconds(max)}`; + } + + // Mixed: format each endpoint with its own unit. return `${formatMilliseconds(min)} - ${formatMilliseconds(max)}`; }