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