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

timestampFormat.test.ts

323 lines11.5 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 formatDayAwareTimestamp,
5 formatElapsedDurationLabel,
6 formatExpiresInLabel,
7 formatRelativeTime,
8 formatRelativeTimeLabel,
9 formatRelativeTimeUntil,
10 formatRelativeTimeUntilLabel,
11 formatShortTimestamp,
12 formatTimestamp,
13 formatUpcomingTimestamp,
14 msUntilRelativeTimeChange,
15 relativeTimeElapsedMs,
16 resolveTimestampLocale,
17} from "./timestampFormat";
18​
19describe("resolveTimestampLocale", () => {
20 it("defers to the runtime default when the host reports no locale", () => {
21 expect(resolveTimestampLocale(null)).toBeUndefined();
22 expect(resolveTimestampLocale(undefined)).toBeUndefined();
23 expect(resolveTimestampLocale(" ")).toBeUndefined();
24 });
25​
26 it("uses a BCP-47 tag reported by the host", () => {
27 expect(resolveTimestampLocale("en-GB")).toBe("en-GB");
28 });
29​
30 it("defers to the runtime default rather than throwing on an unusable tag", () => {
31 expect(resolveTimestampLocale("not a locale")).toBeUndefined();
32 expect(resolveTimestampLocale("en_GB")).toBeUndefined();
33 });
34});
35​
36describe("formatShortTimestamp", () => {
37 afterEach(() => {
38 vi.unstubAllGlobals();
39 vi.resetModules();
40 });
41​
42 it.each([
43 ["en-GB", "15:44"],
44 ["en-US", "3:44 PM"],
45 ])("honors %s and the explicit hour-cycle settings", async (locale, localTime) => {
46 vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => locale } });
47 vi.resetModules();
48 const { formatShortTimestamp: format } = await import("./timestampFormat");
49 const date = new Date(2026, 3, 7, 15, 44).toISOString();
50 // ICU can separate the day period with a narrow no-break space.
51 expect(format(date, "locale").replace(/[  ]/g, " ")).toBe(localTime);
52 expect(format(date, "12-hour").replace(/[  ]/g, " ")).toMatch(/^3:44 [ap]m$/i);
53 expect(format(date, "24-hour")).toBe("15:44");
54 });
55});
56​
57describe("resolveWeekStartsOn", () => {
58 it.each([
59 ["en-US", 0],
60 ["en-GB", 1],
61 ["pl-PL", 1],
62 ["ar-EG", 6],
63 ])("starts the %s week on weekday %i", async (locale, weekday) => {
64 const { resolveWeekStartsOn } = await import("./timestampFormat");
65 expect(resolveWeekStartsOn(locale)).toBe(weekday);
66 });
67​
68 it("leaves the default to the caller for a malformed locale", async () => {
69 const { resolveWeekStartsOn } = await import("./timestampFormat");
70 expect(resolveWeekStartsOn("not a locale")).toBeUndefined();
71 });
72​
73 it("follows the locale the desktop host reports", async () => {
74 vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => "en-GB" } });
75 vi.resetModules();
76 const { weekStartsOn } = await import("./timestampFormat");
77 expect(weekStartsOn).toBe(1);
78 vi.unstubAllGlobals();
79 vi.resetModules();
80 });
81});
82​
83describe("formatChatTimestampTooltip", () => {
84 afterEach(() => {
85 vi.restoreAllMocks();
86 vi.resetModules();
87 });
88​
89 it.each(["de-DE", "it-IT"])("keeps the English date label in a %s runtime", async (locale) => {
90 const DateTimeFormat = Intl.DateTimeFormat;
91 vi.spyOn(Intl, "DateTimeFormat").mockImplementation(function (locales, options) {
92 return new DateTimeFormat(locales ?? locale, options);
93 });
94 vi.resetModules();
95 const { formatChatTimestampTooltip: format } = await import("./timestampFormat");
96 const date = new Date(2026, 5, 4, 14, 4).toISOString();
97​
98 expect(format(date, "24-hour")).toBe("14:04, 4th June 2026");
99 });
100});
101​
102describe("formatRelativeTimeUntilLabel", () => {
103 beforeEach(() => {
104 vi.useFakeTimers();
105 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
106 });
107​
108 afterEach(() => {
109 vi.useRealTimers();
110 });
111​
112 it("returns Expired when the instant is in the past", () => {
113 expect(formatRelativeTimeUntilLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
114 });
115​
116 it("formats seconds remaining", () => {
117 expect(formatRelativeTimeUntilLabel("2026-04-07T12:00:45.000Z")).toBe("45s left");
118 });
119​
120 it("formats minutes remaining", () => {
121 expect(formatRelativeTimeUntilLabel("2026-04-07T12:15:00.000Z")).toBe("15m left");
122 });
123​
124 it("formats hours remaining", () => {
125 expect(formatRelativeTimeUntilLabel("2026-04-07T18:00:00.000Z")).toBe("6h left");
126 });
127});
128​
129describe("formatExpiresInLabel", () => {
130 beforeEach(() => {
131 vi.useFakeTimers();
132 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
133 });
134​
135 afterEach(() => {
136 vi.useRealTimers();
137 });
138​
139 it("returns Expired when the instant is in the past", () => {
140 expect(formatExpiresInLabel("2026-04-07T11:59:00.000Z")).toBe("Expired");
141 });
142​
143 it("uses sub-minute second count", () => {
144 expect(formatExpiresInLabel("2026-04-07T12:00:45.000Z")).toBe("Expires in 45s");
145 });
146​
147 it("uses minutes and seconds under one hour", () => {
148 expect(formatExpiresInLabel("2026-04-07T12:04:12.000Z")).toBe("Expires in 4m 12s");
149 expect(formatExpiresInLabel("2026-04-07T12:15:00.000Z")).toBe("Expires in 15m");
150 });
151​
152 it("uses hours with minute and second remainder", () => {
153 expect(formatExpiresInLabel("2026-04-07T14:02:03.000Z")).toBe("Expires in 2h 2m 3s");
154 expect(formatExpiresInLabel("2026-04-07T18:00:00.000Z")).toBe("Expires in 6h");
155 });
156});
157​
158describe("formatDayAwareTimestamp", () => {
159 // Instants are built with the local-time Date constructor so the
160 // calendar-day boundaries hold in any test timezone or locale.
161 const iso = (y: number, monthIndex: number, d: number, h: number, mi: number) =>
162 new Date(y, monthIndex, d, h, mi).toISOString();
163 const now = new Date(2026, 7, 14, 12, 0).getTime();
164 const time = (isoDate: string) => formatShortTimestamp(isoDate, "12-hour");
165​
166 it("shows time only for today", () => {
167 const messageAt = iso(2026, 7, 14, 9, 30);
168 expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(time(messageAt));
169 });
170​
171 it("labels the previous calendar day as yesterday even when under 24h old", () => {
172 const messageAt = iso(2026, 7, 13, 23, 30);
173 const justPastMidnight = new Date(2026, 7, 14, 0, 30).getTime();
174 expect(formatDayAwareTimestamp(messageAt, "12-hour", justPastMidnight)).toBe(
175 `yesterday at ${time(messageAt)}`,
176 );
177 });
178​
179 it("prefixes older same-year messages with the numeric date", () => {
180 const messageAt = iso(2026, 7, 12, 12, 34);
181 const datePart = new Intl.DateTimeFormat(undefined, {
182 month: "numeric",
183 day: "numeric",
184 }).format(new Date(messageAt));
185 expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(
186 `${datePart} ${time(messageAt)}`,
187 );
188 });
189​
190 it("includes the year once the calendar year differs", () => {
191 const messageAt = iso(2025, 11, 31, 18, 0);
192 const datePart = new Intl.DateTimeFormat(undefined, {
193 month: "numeric",
194 day: "numeric",
195 year: "numeric",
196 }).format(new Date(messageAt));
197 expect(formatDayAwareTimestamp(messageAt, "12-hour", now)).toBe(
198 `${datePart} ${time(messageAt)}`,
199 );
200 });
201​
202 it("uses the host locale for both the numeric date and wall-clock time", async () => {
203 vi.stubGlobal("window", {
204 desktopBridge: { getSystemLocale: () => "en-GB" },
205 });
206 vi.resetModules();
207​
208 const { formatDayAwareTimestamp: formatWithHostLocale } = await import("./timestampFormat");
209 const messageAt = iso(2026, 7, 12, 15, 44);
210​
211 expect(formatWithHostLocale(messageAt, "locale", now)).toBe("12/08 15:44");
212​
213 vi.unstubAllGlobals();
214 });
215​
216 it("returns an empty string for invalid input", () => {
217 expect(formatDayAwareTimestamp("not-a-date", "12-hour", now)).toBe("");
218 });
219});
220​
221describe("formatUpcomingTimestamp", () => {
222 const now = new Date(2026, 7, 14, 12, 0).getTime();
223​
224 it.each([
225 [14, ""],
226 [15, "tomorrow at "],
227 [13, "yesterday at "],
228 ])("keeps the reset day visible for day %i", (day, prefix) => {
229 const resetAt = new Date(2026, 7, day, 14, 30).toISOString();
230 expect(formatUpcomingTimestamp(resetAt, "12-hour", now)).toBe(
231 `${prefix}${formatShortTimestamp(resetAt, "12-hour")}`,
232 );
233 });
234​
235 it("preserves the date of an older reset in the transcript", () => {
236 const resetAt = new Date(2026, 7, 12, 14, 30).toISOString();
237 expect(formatUpcomingTimestamp(resetAt, "12-hour", now)).toBe(
238 formatDayAwareTimestamp(resetAt, "12-hour", now),
239 );
240 });
241});
242​
243describe("invalid timestamp inputs", () => {
244 it("returns an empty timestamp instead of throwing", () => {
245 expect(formatTimestamp("not-a-date", "12-hour")).toBe("");
246 });
247​
248 it("returns an empty short timestamp instead of throwing", () => {
249 expect(formatShortTimestamp("not-a-date", "12-hour")).toBe("");
250 });
251​
252 it("returns an empty relative time label instead of a NaN label", () => {
253 expect(formatRelativeTime("not-a-date")).toBeNull();
254 expect(formatRelativeTimeLabel("not-a-date")).toBe("");
255 });
256​
257 it("has no relative time boundary for an invalid timestamp", () => {
258 expect(relativeTimeElapsedMs("not-a-date")).toBeNull();
259 expect(msUntilRelativeTimeChange("not-a-date")).toBeNull();
260 });
261​
262 it("returns an empty elapsed duration instead of a NaN label", () => {
263 expect(formatElapsedDurationLabel("not-a-date")).toBe("");
264 });
265​
266 it("returns an empty relative time until label instead of a NaN label", () => {
267 expect(formatRelativeTimeUntil("not-a-date")).toBeNull();
268 expect(formatRelativeTimeUntilLabel("not-a-date")).toBe("");
269 });
270​
271 it("returns an empty expires-in label instead of a NaN label", () => {
272 expect(formatExpiresInLabel("not-a-date")).toBe("");
273 });
274});
275​
276describe("relative time boundaries", () => {
277 const nowMs = Date.parse("2026-04-07T12:00:00.000Z");
278​
279 it("waits for the label to change instead of the next second", () => {
280 // "just now" turns into "1m ago" at the one-minute mark.
281 expect(msUntilRelativeTimeChange("2026-04-07T11:59:40.000Z", nowMs)).toBe(40_000);
282 // "2m ago" turns into "3m ago" a minute after the 2m mark.
283 expect(msUntilRelativeTimeChange("2026-04-07T11:57:30.000Z", nowMs)).toBe(30_000);
284 // "3h ago" holds until the 4h mark.
285 expect(msUntilRelativeTimeChange("2026-04-07T08:30:00.000Z", nowMs)).toBe(30 * 60_000);
286 // "2d ago" holds until the 3d mark.
287 expect(msUntilRelativeTimeChange("2026-04-05T00:00:00.000Z", nowMs)).toBe(12 * 3_600_000);
288 // A future timestamp reads "just now" until a minute after it.
289 expect(msUntilRelativeTimeChange("2026-04-07T12:00:10.000Z", nowMs)).toBe(70_000);
290 // A far-future timestamp stays within setTimeout's range.
291 expect(msUntilRelativeTimeChange("2026-06-01T00:00:00.000Z", nowMs)).toBe(86_400_000);
292 });
293​
294 it("keeps the elapsed snapshot stable while the label is unchanged", () => {
295 const at = "2026-04-07T11:57:30.000Z";
296 expect(relativeTimeElapsedMs(at, nowMs)).toBe(2 * 60_000);
297 expect(relativeTimeElapsedMs(at, nowMs + 29_999)).toBe(2 * 60_000);
298 expect(relativeTimeElapsedMs(at, nowMs + 30_000)).toBe(3 * 60_000);
299 });
300});
301​
302describe("formatElapsedDurationLabel", () => {
303 beforeEach(() => {
304 vi.useFakeTimers();
305 vi.setSystemTime(new Date("2026-04-07T12:00:00.000Z"));
306 });
307​
308 afterEach(() => {
309 vi.useRealTimers();
310 });
311​
312 it("returns just now when the instant is current or in the future", () => {
313 expect(formatElapsedDurationLabel("2026-04-07T12:00:00.000Z")).toBe("just now");
314 expect(formatElapsedDurationLabel("2026-04-07T12:01:00.000Z")).toBe("just now");
315 });
316​
317 it("formats seconds, minutes, hours, and days", () => {
318 expect(formatElapsedDurationLabel("2026-04-07T11:59:45.000Z")).toBe("15s");
319 expect(formatElapsedDurationLabel("2026-04-07T11:45:00.000Z")).toBe("15m");
320 expect(formatElapsedDurationLabel("2026-04-07T06:00:00.000Z")).toBe("6h");
321 expect(formatElapsedDurationLabel("2026-04-03T12:00:00.000Z")).toBe("4d");
322 });
323});