import { describe, expect, it } from 'vitest'; import { guessTown } from '../src/places/guess.js'; import { allTowns, normalize, searchTowns, type Town } from '../src/places/places.js'; const towns = allTowns(); const first = (q: string): Town | undefined => searchTowns(q)[0]; function standardOffset(zone: string): number { const f = new Intl.DateTimeFormat('en-US', { timeZone: zone, timeZoneName: 'longOffset' }); const at = (ms: number): number => { const m = /GMT([+-])(\d\d):(\d\d)/.exec(f.formatToParts(ms).find((p) => p.type === 'timeZoneName')?.value ?? ''); return m ? (m[1] === '-' ? -1 : 1) * (Number(m[2]) + Number(m[3]) / 60) : 0; }; return Math.min(at(Date.UTC(2026, 0, 15)), at(Date.UTC(2026, 6, 15))); } describe('the town list', () => { it('holds every Natural Earth town, each with a position and a zone Intl knows', () => { expect(towns).toHaveLength(7342); const zones = new Set(towns.map((t) => t.timeZone)); for (const z of zones) expect(() => new Intl.DateTimeFormat('en', { timeZone: z })).not.toThrow(); for (const t of towns) { expect(Math.abs(t.lat)).toBeLessThanOrEqual(90); expect(Math.abs(t.lon)).toBeLessThanOrEqual(180); expect(t.name).not.toBe(''); expect(t.name).not.toContain('�'); } }); it("every town's clock fits its longitude within 3.5 hours, across the date line too", () => { const off = new Map(); const bad = towns.filter((t) => { if (!off.has(t.timeZone)) off.set(t.timeZone, standardOffset(t.timeZone)); const drift = ((((off.get(t.timeZone)! - t.lon / 15) % 24) + 36) % 24) - 12; return Math.abs(drift) > 3.5; }); expect(bad.map((t) => `${t.name} ${t.timeZone}`)).toEqual([]); }); // Natural Earth's own zones were wrong for these; the build corrects them. it.each([ ['Prague', 'Czechia', 'Europe/Prague'], // Natural Earth: America/Chicago ['Cardiff', 'United Kingdom', 'Europe/London'], // Natural Earth: Australia/Sydney ['Sydney', 'Australia', 'Australia/Sydney'], // Natural Earth: none ['Anchorage', 'United States of America', 'America/Anchorage'], // Natural Earth: America/Juneau ['Kryvyi Rih', 'Ukraine', 'Europe/Kyiv'], // never Simferopol's Moscow time ['Apia', 'Samoa', 'Pacific/Apia'], // UTC+13 at 172° W ['Pristina', 'Kosovo', 'Europe/Belgrade'], // no ISO code in Natural Earth // Natural Earth: America/Sao_Paulo, whose fixed UTC-3 once moved the capital onto a // southern zone's clock with no daylight saving (fixed 2026-11-17). ['Santiago', 'Chile', 'America/Santiago'], ['Valparaíso', 'Chile', 'America/Santiago'], ['Puerto Montt', 'Chile', 'America/Santiago'], // north of Aysén: the main clock // Natural Earth predates the regional splits (Magallanes 2016, Aysén 2025). ['Coyhaique', 'Chile', 'America/Coyhaique'], ['Puerto Aisén', 'Chile', 'America/Coyhaique'], ["Villa O'Higgins", 'Chile', 'America/Coyhaique'], // Natural Earth even files it under Argentina's Santa Cruz ['Punta Arenas', 'Chile', 'America/Punta_Arenas'], ['Puerto Williams', 'Chile', 'America/Punta_Arenas'] ])('%s, %s, keeps %s', (name, country, zone) => { const t = towns.find((x) => x.name === name && x.country === country); expect(t?.timeZone).toBe(zone); }); }); describe('searchTowns', () => { it('finds a town by its English name, its local name, or without accents', () => { expect(first('brno')?.name).toBe('Brno'); expect(first('praha')?.name).toBe('Prague'); expect(first('plzen')?.name).toBe('Plzeň'); expect(first('Plzeň')?.name).toBe('Plzeň'); expect(first('ceske bud')?.name).toBe('České Budějovice'); }); it('puts the larger of two towns with one name first, and a comma narrows by country or region', () => { const sydneys = searchTowns('sydney'); expect(sydneys[0]?.country).toBe('Australia'); expect(sydneys.some((t) => t.country === 'Canada')).toBe(true); expect(first('sydney, canada')?.country).toBe('Canada'); expect(first('sydney, nova')?.region).toBe('Nova Scotia'); }); it('an exact name beats a longer one that starts the same', () => { expect(first('york')?.name).toBe('York'); }); it('nothing typed, or nothing found, is an empty list, never an error', () => { expect(searchTowns('')).toEqual([]); expect(searchTowns(' ')).toEqual([]); expect(searchTowns('zzzzqx')).toEqual([]); }); it('never returns more than asked', () => { expect(searchTowns('san', 5)).toHaveLength(5); }); it('normalize folds case, accents and punctuation', () => { expect(normalize(' Hradec Králové! ')).toBe('hradec kralove'); expect(normalize("Nuku'alofa")).toBe('nuku alofa'); }); }); describe('guessTown', () => { it.each([ ['Europe/Prague', 'Prague'], ['America/New_York', 'New York City'], ['America/Anchorage', 'Anchorage'], ['Australia/Sydney', 'Sydney'], ['Asia/Kolkata', 'Mumbai'], ['Asia/Calcutta', 'Mumbai'], // the name browsers report ['Europe/Kiev', 'Kyiv'] // likewise ])('%s guesses %s', (zone, name) => { const t = guessTown(zone); expect(t?.name).toBe(name); expect(t?.timeZone).toBe(zone); }); it('a zone with no place, such as UTC, guesses nothing: the player chooses', () => { expect(guessTown('UTC')).toBeNull(); expect(guessTown('Etc/GMT+3')).toBeNull(); expect(guessTown('constructor')).toBeNull(); // not a key of a plain object's prototype }); it('every zone Intl knows has a guess or is a zone of no place', () => { const missing = Intl.supportedValuesOf('timeZone').filter((z) => guessTown(z) === null && !/^(Etc\/|UTC$|GMT)/.test(z)); expect(missing).toEqual([]); }); });