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