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

timestampFormat.test.ts

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