import { createRequire } from 'node:module'; import { describe, expect, it } from 'vitest'; import { ATLAS_NAME_TO_ALPHA3, ISO_NUMERIC_TO_ALPHA3, atlasGeometryIso3 } from '@/lib/iso-numeric'; const require = createRequire(import.meta.url); const atlas = require('world-atlas/countries-110m.json') as { objects: { countries: { geometries: Array<{ id?: string; properties: { name: string } }> } } }; describe('ISO numeric → alpha-3', () => { it('has well-formed keys and values', () => { for (const [k, v] of Object.entries(ISO_NUMERIC_TO_ALPHA3)) { expect(k).toMatch(/^\d{3}$/); expect(v).toMatch(/^[A-Z]{3}$/); } const values = Object.values(ISO_NUMERIC_TO_ALPHA3); expect(new Set(values).size).toBe(values.length); }); it('resolves a few well-known codes', () => { expect(atlasGeometryIso3('840')).toBe('USA'); expect(atlasGeometryIso3(840)).toBe('USA'); expect(atlasGeometryIso3('004')).toBe('AFG'); expect(atlasGeometryIso3(4)).toBe('AFG'); expect(atlasGeometryIso3('410')).toBe('KOR'); expect(atlasGeometryIso3('158')).toBe('TWN'); expect(atlasGeometryIso3('275')).toBe('PSE'); expect(atlasGeometryIso3(undefined, 'Kosovo')).toBe('XKX'); expect(atlasGeometryIso3(undefined, 'N. Cyprus')).toBeNull(); expect(atlasGeometryIso3(undefined, 'Somaliland')).toBeNull(); expect(atlasGeometryIso3(undefined, 'Nowhere')).toBeNull(); expect(atlasGeometryIso3('999')).toBeNull(); }); it('covers every geometry in world-atlas countries-110m (id or name fallback)', () => { const geoms = atlas.objects.countries.geometries; expect(geoms.length).toBeGreaterThan(170); const unresolved = geoms.filter((g) => g.id != null && atlasGeometryIso3(g.id, g.properties.name) === null).map((g) => `${g.id} ${g.properties.name}`); expect(unresolved).toEqual([]); const idless = geoms.filter((g) => g.id == null).map((g) => g.properties.name); for (const n of idless) expect(n in ATLAS_NAME_TO_ALPHA3).toBe(true); }); });