test: table-drive format-time timestamp assertions

This commit is contained in:
Peter Steinberger
2026-02-18 23:30:31 +00:00
parent 20849df702
commit 281e9110cc

View File

@@ -120,42 +120,46 @@ describe("format-duration", () => {
describe("format-datetime", () => { describe("format-datetime", () => {
describe("resolveTimezone", () => { describe("resolveTimezone", () => {
it("returns valid IANA timezone strings", () => { it.each([
expect(resolveTimezone("America/New_York")).toBe("America/New_York"); { input: "America/New_York", expected: "America/New_York" },
expect(resolveTimezone("Europe/London")).toBe("Europe/London"); { input: "Europe/London", expected: "Europe/London" },
expect(resolveTimezone("UTC")).toBe("UTC"); { input: "UTC", expected: "UTC" },
}); { input: "Invalid/Timezone", expected: undefined },
{ input: "garbage", expected: undefined },
it("returns undefined for invalid timezones", () => { { input: "", expected: undefined },
expect(resolveTimezone("Invalid/Timezone")).toBeUndefined(); ] as const)("resolves $input", ({ input, expected }) => {
expect(resolveTimezone("garbage")).toBeUndefined(); expect(resolveTimezone(input)).toBe(expected);
expect(resolveTimezone("")).toBeUndefined();
}); });
}); });
describe("formatUtcTimestamp", () => { describe("formatUtcTimestamp", () => {
it("formats without seconds by default", () => { it.each([
{ displaySeconds: false, expected: "2024-01-15T14:30Z" },
{ displaySeconds: true, expected: "2024-01-15T14:30:45Z" },
])("formats UTC timestamp (displaySeconds=$displaySeconds)", ({ displaySeconds, expected }) => {
const date = new Date("2024-01-15T14:30:45.000Z"); const date = new Date("2024-01-15T14:30:45.000Z");
expect(formatUtcTimestamp(date)).toBe("2024-01-15T14:30Z"); const result = displaySeconds
}); ? formatUtcTimestamp(date, { displaySeconds: true })
: formatUtcTimestamp(date);
it("includes seconds when requested", () => { expect(result).toBe(expected);
const date = new Date("2024-01-15T14:30:45.000Z");
expect(formatUtcTimestamp(date, { displaySeconds: true })).toBe("2024-01-15T14:30:45Z");
}); });
}); });
describe("formatZonedTimestamp", () => { describe("formatZonedTimestamp", () => {
it("formats with timezone abbreviation", () => { it.each([
const date = new Date("2024-01-15T14:30:00.000Z"); {
const result = formatZonedTimestamp(date, { timeZone: "UTC" }); date: new Date("2024-01-15T14:30:00.000Z"),
expect(result).toMatch(/2024-01-15 14:30/); options: { timeZone: "UTC" },
}); expected: /2024-01-15 14:30/,
},
it("includes seconds when requested", () => { {
const date = new Date("2024-01-15T14:30:45.000Z"); date: new Date("2024-01-15T14:30:45.000Z"),
const result = formatZonedTimestamp(date, { timeZone: "UTC", displaySeconds: true }); options: { timeZone: "UTC", displaySeconds: true },
expect(result).toMatch(/2024-01-15 14:30:45/); expected: /2024-01-15 14:30:45/,
},
] as const)("formats zoned timestamp", ({ date, options, expected }) => {
const result = formatZonedTimestamp(date, options);
expect(result).toMatch(expected);
}); });
}); });
}); });
@@ -198,18 +202,16 @@ describe("format-relative", () => {
expect(formatRelativeTimestamp(null, { fallback: "unknown" })).toBe("unknown"); expect(formatRelativeTimestamp(null, { fallback: "unknown" })).toBe("unknown");
}); });
it("formats past timestamps", () => { it.each([
{ offsetMs: -10000, expected: "just now" },
{ offsetMs: -300000, expected: "5m ago" },
{ offsetMs: -7200000, expected: "2h ago" },
{ offsetMs: 30000, expected: "in <1m" },
{ offsetMs: 300000, expected: "in 5m" },
{ offsetMs: 7200000, expected: "in 2h" },
])("formats relative timestamp for offset $offsetMs", ({ offsetMs, expected }) => {
const now = Date.now(); const now = Date.now();
expect(formatRelativeTimestamp(now - 10000)).toBe("just now"); expect(formatRelativeTimestamp(now + offsetMs)).toBe(expected);
expect(formatRelativeTimestamp(now - 300000)).toBe("5m ago");
expect(formatRelativeTimestamp(now - 7200000)).toBe("2h ago");
});
it("formats future timestamps", () => {
const now = Date.now();
expect(formatRelativeTimestamp(now + 30000)).toBe("in <1m");
expect(formatRelativeTimestamp(now + 300000)).toBe("in 5m");
expect(formatRelativeTimestamp(now + 7200000)).toBe("in 2h");
}); });
it("falls back to date for old timestamps when enabled", () => { it("falls back to date for old timestamps when enabled", () => {