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