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