Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

timestampFormat.test.ts

102 lines3.6 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 formatChatTimestampTooltip,
5 formatDayAwareTimestamp,
6 formatUpcomingTimestamp,
7} from "./timestampFormat";
8​
9// Node re-resolves the default zone when TZ is assigned, like a renderer after the OS zone changes.
10function useTimeZone(timeZone: string): Disposable {
11 const previous = process.env.TZ;
12 process.env.TZ = timeZone;
13 return {
14 [Symbol.dispose]: () => {
15 if (previous === undefined) delete process.env.TZ;
16 else process.env.TZ = previous;
17 },
18 };
20​
21it("localizes the day period while honoring the system clock and explicit 24-hour mode", async () => {
22 using _zone = useTimeZone("UTC");
23 vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => "en-US" } });
24 vi.resetModules();
25 try {
26 const { formatShortTimestamp } = await import("./timestampFormat");
27 const date = "2026-11-19T18:05:00.000Z";
28 expect(formatShortTimestamp(date, "locale", "zh")).toBe("下午6:05");
29 expect(formatShortTimestamp(date, "24-hour", "zh")).toBe("18:05");
30 } finally {
31 vi.unstubAllGlobals();
32 }
33});
34​
35describe("timestamps in an explicit time zone", () => {
36 // 23:00 UTC on July 1 is 7 PM on July 1 in New York, whatever the host zone.
37 const now = Date.parse("2026-07-01T23:00:00.000Z");
38 const zone = "America/New_York";
39​
40 it("picks relative days from the calendar in that zone", () => {
41 expect(formatUpcomingTimestamp("2026-07-02T00:30:00.000Z", "12-hour", now, zone)).not.toMatch(
42 /tomorrow/,
43 );
44 expect(formatUpcomingTimestamp("2026-07-02T05:30:00.000Z", "12-hour", now, zone)).toMatch(
45 /^tomorrow at /,
46 );
47 expect(formatDayAwareTimestamp("2026-07-01T03:30:00.000Z", "12-hour", now, zone)).toMatch(
48 /^yesterday at /,
49 );
50 });
51});
52​
53describe("timestamps after the system time zone changes", () => {
54 // Built fresh with an explicit zone, so expectations follow the host locale but not a stale zone.
55 const timeIn = (iso: string, timeZone: string) =>
56 new Intl.DateTimeFormat(undefined, {
57 hour: "numeric",
58 minute: "2-digit",
59 hour12: true,
60 timeZone,
61 }).format(new Date(iso));
62 const dayIn = (iso: string, timeZone: string) =>
63 new Intl.DateTimeFormat(undefined, { month: "numeric", day: "numeric", timeZone }).format(
64 new Date(iso),
65 );
66​
67 it("renders the gutter label in the new zone", () => {
68 const sameDay = "2026-11-07T04:45:43.877Z";
69 const earlierInWeek = "2026-11-04T20:00:00.000Z";
70 const now = Date.parse(sameDay);
71 using _zone = useTimeZone("America/New_York");
72 expect([
73 formatDayAwareTimestamp(sameDay, "12-hour", now),
74 formatDayAwareTimestamp(earlierInWeek, "12-hour", now),
75 ]).toEqual([
76 timeIn(sameDay, "America/New_York"),
77 `${dayIn(earlierInWeek, "America/New_York")} ${timeIn(earlierInWeek, "America/New_York")}`,
78 ]);
79​
80 process.env.TZ = "Asia/Bangkok";
81 expect([
82 formatDayAwareTimestamp(sameDay, "12-hour", now),
83 formatDayAwareTimestamp(earlierInWeek, "12-hour", now),
84 ]).toEqual([
85 timeIn(sameDay, "Asia/Bangkok"),
86 `${dayIn(earlierInWeek, "Asia/Bangkok")} ${timeIn(earlierInWeek, "Asia/Bangkok")}`,
87 ]);
88 });
89​
90 it("renders the tooltip in the new zone", () => {
91 const monthEnd = "2026-10-31T20:00:00.000Z";
92 using _zone = useTimeZone("America/New_York");
93 expect(formatChatTimestampTooltip(monthEnd, "12-hour")).toBe(
94 `${timeIn(monthEnd, "America/New_York")}, 31st October 2026`,
95 );
96​
97 process.env.TZ = "Asia/Bangkok";
98 expect(formatChatTimestampTooltip(monthEnd, "12-hour")).toBe(
99 `${timeIn(monthEnd, "Asia/Bangkok")}, 1st November 2026`,
100 );
101 });
102});