Tests: trim timezone in envelope timestamp helper (#12446)

This commit is contained in:
Mariano
2026-02-09 09:04:54 +01:00
committed by GitHub
parent ec910a235e
commit 5acb1e3c52
2 changed files with 11 additions and 3 deletions

View File

@@ -106,6 +106,11 @@ describe("web auto-reply", () => {
vi.useRealTimers();
});
it("handles helper envelope timestamps with trimmed timezones (regression)", () => {
const d = new Date("2025-01-01T00:00:00.000Z");
expect(() => formatEnvelopeTimestamp(d, " America/Los_Angeles ")).not.toThrow();
});
it("reconnects after a connection close", async () => {
const closeResolvers: Array<() => void> = [];
const sleep = vi.fn(async () => {});

View File

@@ -8,7 +8,8 @@ export { escapeRegExp } from "../../src/utils.js";
type EnvelopeTimestampZone = string;
export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone = "utc"): string {
const normalized = zone.trim().toLowerCase();
const trimmedZone = zone.trim();
const normalized = trimmedZone.toLowerCase();
const weekday = (() => {
try {
if (normalized === "utc" || normalized === "gmt") {
@@ -17,7 +18,9 @@ export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone
if (normalized === "local" || normalized === "host") {
return new Intl.DateTimeFormat("en-US", { weekday: "short" }).format(date);
}
return new Intl.DateTimeFormat("en-US", { timeZone: zone, weekday: "short" }).format(date);
return new Intl.DateTimeFormat("en-US", { timeZone: trimmedZone, weekday: "short" }).format(
date,
);
} catch {
return undefined;
}
@@ -31,7 +34,7 @@ export function formatEnvelopeTimestamp(date: Date, zone: EnvelopeTimestampZone
const ts = formatZonedTimestamp(date) ?? formatUtcTimestamp(date);
return weekday ? `${weekday} ${ts}` : ts;
}
const ts = formatZonedTimestamp(date, { timeZone: zone }) ?? formatUtcTimestamp(date);
const ts = formatZonedTimestamp(date, { timeZone: trimmedZone }) ?? formatUtcTimestamp(date);
return weekday ? `${weekday} ${ts}` : ts;
}