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