-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclocks.test.ts
More file actions
280 lines (239 loc) · 10.1 KB
/
Copy pathclocks.test.ts
File metadata and controls
280 lines (239 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { describe, expect, it } from 'bun:test'
import {
buildFormatters,
type ClockConfig,
buildUtcFormatter,
DEFAULT_CLOCKS,
deriveLabel,
formatDate,
formatTimeParts,
getDayPeriod,
getOffsetLabel,
getZonedHour,
isRtlLocale,
isValidTimeZone,
parseClockEntry,
parseClocks
} from './clocks.ts'
// A fixed absolute instant: Saturday 2026-06-20 13:30:45 UTC. Pairing it with an
// explicit zone makes every assertion independent of the machine timezone.
const INSTANT = new Date('2026-06-20T13:30:45Z')
const configFor = (overrides: Partial<ClockConfig> = {}): ClockConfig => ({
clocks: [],
locale: 'en-GB',
format: 'auto',
seconds: false,
title: '',
configured: false,
...overrides
})
describe('isValidTimeZone', () => {
it('accepts real IANA zones', () => {
expect(isValidTimeZone('America/New_York')).toBe(true)
expect(isValidTimeZone('Asia/Kolkata')).toBe(true)
expect(isValidTimeZone('UTC')).toBe(true)
})
it('rejects junk and empty strings', () => {
expect(isValidTimeZone('Mars/Olympus')).toBe(false)
expect(isValidTimeZone('not a zone')).toBe(false)
expect(isValidTimeZone('')).toBe(false)
})
})
describe('deriveLabel', () => {
it('uses the last path segment with underscores as spaces', () => {
expect(deriveLabel('America/New_York')).toBe('New York')
expect(deriveLabel('Asia/Kolkata')).toBe('Kolkata')
expect(deriveLabel('America/Argentina/Buenos_Aires')).toBe('Buenos Aires')
expect(deriveLabel('UTC')).toBe('UTC')
})
})
describe('parseClockEntry', () => {
it('parses a bare timezone, deriving the label', () => {
expect(parseClockEntry('Europe/London')).toEqual({
timeZone: 'Europe/London',
label: 'London'
})
})
it('parses an explicit label', () => {
expect(parseClockEntry('Asia/Tokyo|HQ Tokyo')).toEqual({
timeZone: 'Asia/Tokyo',
label: 'HQ Tokyo'
})
})
it('ignores any extra pipe segments (locale is global, not per-clock)', () => {
expect(parseClockEntry('Asia/Tokyo|HQ|ja-JP')).toEqual({
timeZone: 'Asia/Tokyo',
label: 'HQ'
})
})
it('falls back to the derived label when the label segment is empty', () => {
expect(parseClockEntry('Asia/Tokyo|')?.label).toBe('Tokyo')
})
it('returns null for an invalid timezone', () => {
expect(parseClockEntry('Mars/Olympus|Mars')).toBeNull()
expect(parseClockEntry('')).toBeNull()
})
})
describe('parseClocks', () => {
it('falls back to DEFAULT_CLOCKS when no clocks are given', () => {
const config = parseClocks('')
expect(config.clocks.map((c) => c.timeZone)).toEqual(DEFAULT_CLOCKS.map((c) => c.timeZone))
expect(config.configured).toBe(false)
})
it('marks configured true when the URL supplies a real clock', () => {
expect(parseClocks('?tz=Europe/London').configured).toBe(true)
})
it('marks configured false when every supplied zone is invalid', () => {
const config = parseClocks('?tz=Mars/Olympus')
expect(config.clocks.map((c) => c.timeZone)).toEqual(DEFAULT_CLOCKS.map((c) => c.timeZone))
expect(config.configured).toBe(false)
})
it('reads repeated tz params in order', () => {
const config = parseClocks('?tz=America/New_York|NYC&tz=Europe/London')
expect(config.clocks).toEqual([
{ timeZone: 'America/New_York', label: 'NYC' },
{ timeZone: 'Europe/London', label: 'London' }
])
})
it('reads a single comma-separated clocks param', () => {
const config = parseClocks('?clocks=America/New_York|NYC,Asia/Tokyo')
expect(config.clocks.map((c) => c.label)).toEqual(['NYC', 'Tokyo'])
})
it('skips invalid zones but keeps the valid ones', () => {
const config = parseClocks('?tz=Mars/Olympus&tz=Europe/Paris')
expect(config.clocks.map((c) => c.timeZone)).toEqual(['Europe/Paris'])
})
it('keeps a comma in a label from a repeated tz param (not split like clocks=)', () => {
const config = parseClocks('?tz=Asia/Tokyo|Tokyo, Japan')
expect(config.clocks).toEqual([{ timeZone: 'Asia/Tokyo', label: 'Tokyo, Japan' }])
})
it('resolves a single global locale, defaulting to en-GB', () => {
expect(parseClocks('').locale).toBe('en-GB')
expect(parseClocks('?locale=de-DE').locale).toBe('de-DE')
// An unsupported locale falls back to the neutral default.
expect(parseClocks('?locale=not-a-locale').locale).toBe('en-GB')
})
it('parses the global format, seconds and title flags', () => {
expect(parseClocks('?format=12').format).toBe('12')
expect(parseClocks('?format=24').format).toBe('24')
expect(parseClocks('?format=nonsense').format).toBe('auto')
expect(parseClocks('?seconds').seconds).toBe(true)
expect(parseClocks('?seconds=0').seconds).toBe(false)
expect(parseClocks('?title=Trading%20Desk').title).toBe('Trading Desk')
})
})
describe('time formatting (zoned, 12/24h, localized)', () => {
// Locale is global, so it travels in the config, not the per-clock spec.
const fmt = (spec: { timeZone: string; locale: string }, config: Partial<ClockConfig> = {}) =>
buildFormatters(
{ timeZone: spec.timeZone, label: 'x' },
configFor({ locale: spec.locale, ...config })
)
it('renders a zoned 24h clock for en-GB', () => {
const f = fmt({ timeZone: 'Europe/London', locale: 'en-GB' })
const { time, period } = formatTimeParts(f.time, INSTANT)
expect(time).toBe('14:30')
expect(period).toBe('')
})
it('renders a zoned 12h clock with AM/PM for en-US', () => {
const f = fmt({ timeZone: 'America/New_York', locale: 'en-US' })
const { time, period } = formatTimeParts(f.time, INSTANT)
expect(time).toBe('9:30')
expect(period).toMatch(/AM/i)
})
it('forces 12h when format=12 even for a 24h locale', () => {
const f = fmt({ timeZone: 'Europe/London', locale: 'en-GB' }, { format: '12' })
const { time, period } = formatTimeParts(f.time, INSTANT)
expect(time).toBe('2:30')
expect(period).toMatch(/PM/i)
})
it('forces 24h when format=24 even for a 12h locale', () => {
const f = fmt({ timeZone: 'America/New_York', locale: 'en-US' }, { format: '24' })
// en-US under a 24h cycle zero-pads the hour ("09:30"), and there is no period.
const { time, period } = formatTimeParts(f.time, INSTANT)
expect(time).toBe('09:30')
expect(period).toBe('')
})
it('includes seconds when requested', () => {
const f = fmt({ timeZone: 'Europe/London', locale: 'en-GB' }, { seconds: true })
expect(formatTimeParts(f.time, INSTANT).time).toBe('14:30:45')
})
it('flags periodFirst for locales that print the period before the time', () => {
const f = fmt({ timeZone: 'Asia/Seoul', locale: 'ko-KR' })
const parts = formatTimeParts(f.time, INSTANT)
expect(parts.periodFirst).toBe(true)
expect(parts.period).not.toBe('')
const us = fmt({ timeZone: 'America/New_York', locale: 'en-US' })
expect(formatTimeParts(us.time, INSTANT).periodFirst).toBe(false)
})
it('preserves the locale-native hour:minute separator', () => {
const f = fmt({ timeZone: 'Europe/Helsinki', locale: 'fi-FI' })
expect(formatTimeParts(f.time, INSTANT).time).toBe('16.30')
})
it('always renders Latin digits, even for non-Latin-digit locales', () => {
// ar-EG defaults to Arabic-Indic numerals; only Latin fonts are vendored, so
// the time must stay ASCII digits. AM/PM marker may still localize.
const f = fmt({ timeZone: 'Africa/Cairo', locale: 'ar-EG' })
expect(formatTimeParts(f.time, INSTANT).time).toMatch(/^[0-9:.\s]+$/)
})
})
describe('date localization', () => {
const fmt = (locale: string, timeZone: string) =>
buildFormatters({ timeZone, label: 'x' }, configFor({ locale }))
it('renders month/weekday names in the global locale', () => {
expect(formatDate(fmt('en-US', 'America/New_York').date, INSTANT)).toMatch(/Jun/)
expect(formatDate(fmt('fr-FR', 'Europe/Paris').date, INSTANT)).toMatch(/juin/)
expect(formatDate(fmt('de-DE', 'Europe/Berlin').date, INSTANT)).toMatch(/Juni/)
})
it('pins the Gregorian calendar even for ar-SA (not Hijri)', () => {
const date = formatDate(fmt('ar-SA', 'Asia/Riyadh').date, INSTANT)
expect(date).toMatch(/20|٢٠/)
expect(date).not.toMatch(/محرم/)
})
it('rolls the date across the day line by zone', () => {
// 13:30 UTC is still the 20th in Tokyo (22:30) but Auckland is already the 21st.
expect(formatDate(fmt('en-GB', 'Asia/Tokyo').date, INSTANT)).toMatch(/20/)
expect(formatDate(fmt('en-GB', 'Pacific/Auckland').date, INSTANT)).toMatch(/21/)
})
})
describe('zoned hour, offset and day period', () => {
const fmt = (timeZone: string) => buildFormatters({ timeZone, label: 'x' }, configFor())
it('reads the hour in the clock zone', () => {
expect(getZonedHour(fmt('Asia/Tokyo').hour, INSTANT)).toBe(22)
expect(getZonedHour(fmt('America/New_York').hour, INSTANT)).toBe(9)
})
it('reads the short GMT offset for the zone', () => {
expect(getOffsetLabel(fmt('Asia/Tokyo').offset, INSTANT)).toBe('GMT+9')
// New York is on EDT (-4) in June.
expect(getOffsetLabel(fmt('America/New_York').offset, INSTANT)).toBe('GMT-4')
})
it('returns an empty offset label when the formatter is unavailable', () => {
// Mirrors an engine without 'shortOffset' support, where buildFormatters
// leaves offset undefined rather than crashing the page.
expect(getOffsetLabel(undefined, INSTANT)).toBe('')
})
it('bands the hour into dawn / day / dusk / night', () => {
expect(getDayPeriod(6)).toBe('dawn')
expect(getDayPeriod(12)).toBe('day')
expect(getDayPeriod(18)).toBe('dusk')
expect(getDayPeriod(23)).toBe('night')
expect(getDayPeriod(3)).toBe('night')
})
})
describe('buildUtcFormatter', () => {
it('formats the masthead UTC reference as 24h HH:MM', () => {
expect(buildUtcFormatter().format(INSTANT)).toBe('13:30')
})
})
describe('isRtlLocale', () => {
it('detects RTL primary languages', () => {
expect(isRtlLocale('ar-SA')).toBe(true)
expect(isRtlLocale('he-IL')).toBe(true)
expect(isRtlLocale('en-GB')).toBe(false)
expect(isRtlLocale('ja-JP')).toBe(false)
})
it('is case-insensitive on the language subtag', () => {
expect(isRtlLocale('AR-SA')).toBe(true)
expect(isRtlLocale('Ar')).toBe(true)
})
})