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