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