|
|
|
|
@ -10,52 +10,52 @@ import { formatTimeAgo, formatRelativeTimestamp } from "./format-relative.js";
|
|
|
|
|
|
|
|
|
|
const invalidDurationInputs = [null, undefined, -100] as const;
|
|
|
|
|
|
|
|
|
|
function expectFormatterCases<TInput, TOutput>(
|
|
|
|
|
formatter: (value: TInput) => TOutput,
|
|
|
|
|
cases: ReadonlyArray<{ input: TInput; expected: TOutput }>,
|
|
|
|
|
) {
|
|
|
|
|
for (const { input, expected } of cases) {
|
|
|
|
|
expect(formatter(input), String(input)).toBe(expected);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("format-duration", () => {
|
|
|
|
|
describe("formatDurationCompact", () => {
|
|
|
|
|
it("returns undefined for null/undefined/non-positive", () => {
|
|
|
|
|
expect(formatDurationCompact(null)).toBeUndefined();
|
|
|
|
|
expect(formatDurationCompact(undefined)).toBeUndefined();
|
|
|
|
|
expect(formatDurationCompact(0)).toBeUndefined();
|
|
|
|
|
expect(formatDurationCompact(-100)).toBeUndefined();
|
|
|
|
|
it.each([null, undefined, 0, -100])("returns undefined for %j", (value) => {
|
|
|
|
|
expect(formatDurationCompact(value)).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("formats compact units and omits trailing zero components", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
[500, "500ms"],
|
|
|
|
|
[999, "999ms"],
|
|
|
|
|
[1000, "1s"],
|
|
|
|
|
[45000, "45s"],
|
|
|
|
|
[59000, "59s"],
|
|
|
|
|
[60000, "1m"], // not "1m0s"
|
|
|
|
|
[65000, "1m5s"],
|
|
|
|
|
[90000, "1m30s"],
|
|
|
|
|
[3600000, "1h"], // not "1h0m"
|
|
|
|
|
[3660000, "1h1m"],
|
|
|
|
|
[5400000, "1h30m"],
|
|
|
|
|
[86400000, "1d"], // not "1d0h"
|
|
|
|
|
[90000000, "1d1h"],
|
|
|
|
|
[172800000, "2d"],
|
|
|
|
|
] as const;
|
|
|
|
|
for (const [input, expected] of cases) {
|
|
|
|
|
expect(formatDurationCompact(input), String(input)).toBe(expected);
|
|
|
|
|
}
|
|
|
|
|
expectFormatterCases(formatDurationCompact, [
|
|
|
|
|
{ input: 500, expected: "500ms" },
|
|
|
|
|
{ input: 999, expected: "999ms" },
|
|
|
|
|
{ input: 1000, expected: "1s" },
|
|
|
|
|
{ input: 45000, expected: "45s" },
|
|
|
|
|
{ input: 59000, expected: "59s" },
|
|
|
|
|
{ input: 60000, expected: "1m" },
|
|
|
|
|
{ input: 65000, expected: "1m5s" },
|
|
|
|
|
{ input: 90000, expected: "1m30s" },
|
|
|
|
|
{ input: 3600000, expected: "1h" },
|
|
|
|
|
{ input: 3660000, expected: "1h1m" },
|
|
|
|
|
{ input: 5400000, expected: "1h30m" },
|
|
|
|
|
{ input: 86400000, expected: "1d" },
|
|
|
|
|
{ input: 90000000, expected: "1d1h" },
|
|
|
|
|
{ input: 172800000, expected: "2d" },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("supports spaced option", () => {
|
|
|
|
|
expect(formatDurationCompact(65000, { spaced: true })).toBe("1m 5s");
|
|
|
|
|
expect(formatDurationCompact(3660000, { spaced: true })).toBe("1h 1m");
|
|
|
|
|
expect(formatDurationCompact(90000000, { spaced: true })).toBe("1d 1h");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rounds at boundaries", () => {
|
|
|
|
|
// 59.5 seconds rounds to 60s = 1m
|
|
|
|
|
expect(formatDurationCompact(59500)).toBe("1m");
|
|
|
|
|
// 59.4 seconds rounds to 59s
|
|
|
|
|
expect(formatDurationCompact(59400)).toBe("59s");
|
|
|
|
|
it.each([
|
|
|
|
|
{ input: 65000, options: { spaced: true }, expected: "1m 5s" },
|
|
|
|
|
{ input: 3660000, options: { spaced: true }, expected: "1h 1m" },
|
|
|
|
|
{ input: 90000000, options: { spaced: true }, expected: "1d 1h" },
|
|
|
|
|
{ input: 59500, expected: "1m" },
|
|
|
|
|
{ input: 59400, expected: "59s" },
|
|
|
|
|
])("formats compact duration for %j", ({ input, options, expected }) => {
|
|
|
|
|
expect(formatDurationCompact(input, options)).toBe(expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@ -68,61 +68,47 @@ describe("format-duration", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("formats single-unit outputs and day threshold behavior", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
[500, "500ms"],
|
|
|
|
|
[5000, "5s"],
|
|
|
|
|
[180000, "3m"],
|
|
|
|
|
[7200000, "2h"],
|
|
|
|
|
[23 * 3600000, "23h"],
|
|
|
|
|
[24 * 3600000, "1d"],
|
|
|
|
|
[25 * 3600000, "1d"], // rounds
|
|
|
|
|
[172800000, "2d"],
|
|
|
|
|
] as const;
|
|
|
|
|
for (const [input, expected] of cases) {
|
|
|
|
|
expect(formatDurationHuman(input), String(input)).toBe(expected);
|
|
|
|
|
}
|
|
|
|
|
expectFormatterCases(formatDurationHuman, [
|
|
|
|
|
{ input: 500, expected: "500ms" },
|
|
|
|
|
{ input: 5000, expected: "5s" },
|
|
|
|
|
{ input: 180000, expected: "3m" },
|
|
|
|
|
{ input: 7200000, expected: "2h" },
|
|
|
|
|
{ input: 23 * 3600000, expected: "23h" },
|
|
|
|
|
{ input: 24 * 3600000, expected: "1d" },
|
|
|
|
|
{ input: 25 * 3600000, expected: "1d" },
|
|
|
|
|
{ input: 172800000, expected: "2d" },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("formatDurationPrecise", () => {
|
|
|
|
|
it("shows milliseconds for sub-second", () => {
|
|
|
|
|
expect(formatDurationPrecise(500)).toBe("500ms");
|
|
|
|
|
expect(formatDurationPrecise(999)).toBe("999ms");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("clamps negative and fractional sub-second values to non-negative milliseconds", () => {
|
|
|
|
|
expect(formatDurationPrecise(-1)).toBe("0ms");
|
|
|
|
|
expect(formatDurationPrecise(-500)).toBe("0ms");
|
|
|
|
|
expect(formatDurationPrecise(999.6)).toBe("1000ms");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("shows decimal seconds for >=1s", () => {
|
|
|
|
|
expect(formatDurationPrecise(1000)).toBe("1s");
|
|
|
|
|
expect(formatDurationPrecise(1500)).toBe("1.5s");
|
|
|
|
|
expect(formatDurationPrecise(1234)).toBe("1.23s");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns unknown for non-finite", () => {
|
|
|
|
|
expect(formatDurationPrecise(NaN)).toBe("unknown");
|
|
|
|
|
expect(formatDurationPrecise(Infinity)).toBe("unknown");
|
|
|
|
|
it.each([
|
|
|
|
|
{ input: 500, expected: "500ms" },
|
|
|
|
|
{ input: 999, expected: "999ms" },
|
|
|
|
|
{ input: -1, expected: "0ms" },
|
|
|
|
|
{ input: -500, expected: "0ms" },
|
|
|
|
|
{ input: 999.6, expected: "1000ms" },
|
|
|
|
|
{ input: 1000, expected: "1s" },
|
|
|
|
|
{ input: 1500, expected: "1.5s" },
|
|
|
|
|
{ input: 1234, expected: "1.23s" },
|
|
|
|
|
{ input: NaN, expected: "unknown" },
|
|
|
|
|
{ input: Infinity, expected: "unknown" },
|
|
|
|
|
])("formats precise duration for %j", ({ input, expected }) => {
|
|
|
|
|
expect(formatDurationPrecise(input)).toBe(expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("formatDurationSeconds", () => {
|
|
|
|
|
it("formats with configurable decimals", () => {
|
|
|
|
|
expect(formatDurationSeconds(1500, { decimals: 1 })).toBe("1.5s");
|
|
|
|
|
expect(formatDurationSeconds(1234, { decimals: 2 })).toBe("1.23s");
|
|
|
|
|
expect(formatDurationSeconds(1000, { decimals: 0 })).toBe("1s");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("supports seconds unit", () => {
|
|
|
|
|
expect(formatDurationSeconds(2000, { unit: "seconds" })).toBe("2 seconds");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("clamps negative values and rejects non-finite input", () => {
|
|
|
|
|
expect(formatDurationSeconds(-1500, { decimals: 1 })).toBe("0s");
|
|
|
|
|
expect(formatDurationSeconds(NaN)).toBe("unknown");
|
|
|
|
|
expect(formatDurationSeconds(Infinity)).toBe("unknown");
|
|
|
|
|
it.each([
|
|
|
|
|
{ input: 1500, options: { decimals: 1 }, expected: "1.5s" },
|
|
|
|
|
{ input: 1234, options: { decimals: 2 }, expected: "1.23s" },
|
|
|
|
|
{ input: 1000, options: { decimals: 0 }, expected: "1s" },
|
|
|
|
|
{ input: 2000, options: { unit: "seconds" as const }, expected: "2 seconds" },
|
|
|
|
|
{ input: -1500, options: { decimals: 1 }, expected: "0s" },
|
|
|
|
|
{ input: NaN, options: undefined, expected: "unknown" },
|
|
|
|
|
{ input: Infinity, options: undefined, expected: "unknown" },
|
|
|
|
|
])("formats seconds duration for %j", ({ input, options, expected }) => {
|
|
|
|
|
expect(formatDurationSeconds(input, options)).toBe(expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
@ -222,25 +208,24 @@ describe("format-relative", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("formats relative age around key unit boundaries", () => {
|
|
|
|
|
const cases = [
|
|
|
|
|
[0, "just now"],
|
|
|
|
|
[29000, "just now"], // rounds to <1m
|
|
|
|
|
[30000, "1m ago"], // 30s rounds to 1m
|
|
|
|
|
[300000, "5m ago"],
|
|
|
|
|
[7200000, "2h ago"],
|
|
|
|
|
[47 * 3600000, "47h ago"],
|
|
|
|
|
[48 * 3600000, "2d ago"],
|
|
|
|
|
[172800000, "2d ago"],
|
|
|
|
|
] as const;
|
|
|
|
|
for (const [input, expected] of cases) {
|
|
|
|
|
expect(formatTimeAgo(input), String(input)).toBe(expected);
|
|
|
|
|
}
|
|
|
|
|
expectFormatterCases(formatTimeAgo, [
|
|
|
|
|
{ input: 0, expected: "just now" },
|
|
|
|
|
{ input: 29000, expected: "just now" },
|
|
|
|
|
{ input: 30000, expected: "1m ago" },
|
|
|
|
|
{ input: 300000, expected: "5m ago" },
|
|
|
|
|
{ input: 7200000, expected: "2h ago" },
|
|
|
|
|
{ input: 47 * 3600000, expected: "47h ago" },
|
|
|
|
|
{ input: 48 * 3600000, expected: "2d ago" },
|
|
|
|
|
{ input: 172800000, expected: "2d ago" },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("omits suffix when suffix: false", () => {
|
|
|
|
|
expect(formatTimeAgo(0, { suffix: false })).toBe("0s");
|
|
|
|
|
expect(formatTimeAgo(300000, { suffix: false })).toBe("5m");
|
|
|
|
|
expect(formatTimeAgo(7200000, { suffix: false })).toBe("2h");
|
|
|
|
|
it.each([
|
|
|
|
|
{ input: 0, expected: "0s" },
|
|
|
|
|
{ input: 300000, expected: "5m" },
|
|
|
|
|
{ input: 7200000, expected: "2h" },
|
|
|
|
|
])("omits suffix for %j when disabled", ({ input, expected }) => {
|
|
|
|
|
expect(formatTimeAgo(input, { suffix: false })).toBe(expected);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|