Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

timestampFormat.test.ts

273 lines9.1 KB

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