spb/cancerindex
Public
TypeScript 97.2%
SQL 1.5%
CSS 0.6%
JavaScript 0.5%
1import { describe, expect, it } from 'vitest';2import { COUNTRY_CODES, UNMAPPED_COUNTRY_NAMES, countryToIso3, iso3ToName, isKnownUnmapped, lookupCountry, normalizeCountryName } from '../src/country-codes.js';3import dbCountries from '../src/fixtures/trial-countries.json' with { type: 'json' };45describe('countryToIso3', () => {6 it('maps the ClinicalTrials.gov v2 short names', () => {7 expect(countryToIso3('United States')).toBe('USA');8 expect(countryToIso3('South Korea')).toBe('KOR');9 expect(countryToIso3('Turkey (Türkiye)')).toBe('TUR');10 expect(countryToIso3('Russia')).toBe('RUS');11 expect(countryToIso3('Vietnam')).toBe('VNM');12 expect(countryToIso3('Reunion')).toBe('REU');13 expect(countryToIso3('Palestinian Territories')).toBe('PSE');14 expect(countryToIso3('The Bahamas')).toBe('BHS');15 expect(countryToIso3('Burma')).toBe('MMR');16 expect(countryToIso3('Côte d’Ivoire')).toBe('CIV'); // curly apostrophe as stored in the DB17 });18 it('maps the legacy ISO-style long names', () => {19 expect(countryToIso3('Korea, Republic of')).toBe('KOR');20 expect(countryToIso3('Russian Federation')).toBe('RUS');21 expect(countryToIso3('Iran, Islamic Republic of')).toBe('IRN');22 expect(countryToIso3('Viet Nam')).toBe('VNM');23 expect(countryToIso3('Réunion')).toBe('REU');24 expect(countryToIso3('Palestinian Territory, occupied')).toBe('PSE');25 expect(countryToIso3('Türkiye')).toBe('TUR');26 expect(countryToIso3('Czechia')).toBe('CZE');27 expect(countryToIso3('Hong Kong')).toBe('HKG');28 expect(countryToIso3('Macao')).toBe('MAC');29 expect(countryToIso3('Taiwan')).toBe('TWN');30 });31 it('is case/whitespace insensitive and accepts ISO3 itself', () => {32 expect(countryToIso3(' united states ')).toBe('USA');33 expect(countryToIso3('usa')).toBe('USA');34 expect(countryToIso3('FRA')).toBe('FRA');35 });36 it('returns null for dissolved states, empty and unknown strings', () => {37 expect(countryToIso3('Former Serbia and Montenegro')).toBeNull();38 expect(countryToIso3('Former Yugoslavia')).toBeNull();39 expect(countryToIso3('Netherlands Antilles')).toBeNull();40 expect(countryToIso3('')).toBeNull();41 expect(countryToIso3(null)).toBeNull();42 expect(countryToIso3('Atlantis')).toBeNull();43 expect(isKnownUnmapped('Serbia and Montenegro')).toBe(true);44 expect(isKnownUnmapped('Atlantis')).toBe(false);45 });46 it('exposes display names', () => {47 expect(iso3ToName('KOR')).toBe('South Korea');48 expect(iso3ToName('ZZZ')).toBe('ZZZ');49 expect(lookupCountry('Viet Nam')).toEqual({ iso3: 'VNM', name: 'Vietnam', aliases: ['Viet Nam'] });50 expect(normalizeCountryName(" Côte d’Ivoire ")).toBe("côte d'ivoire");51 });52});5354describe('COUNTRY_CODES table', () => {55 it('has unique ISO3 codes and unique normalized names/aliases', () => {56 const codes = COUNTRY_CODES.map((c) => c.iso3);57 expect(new Set(codes).size).toBe(codes.length);58 for (const c of codes) expect(c).toMatch(/^[A-Z]{3}$/);59 const names = COUNTRY_CODES.flatMap((c) => [c.name, ...(c.aliases ?? [])]).map(normalizeCountryName);60 const dupes = names.filter((n, i) => names.indexOf(n) !== i);61 expect(dupes).toEqual([]);62 });63 it('resolves every distinct country currently in trial_locations, or lists it as UNMAPPED', () => {64 const missing = (dbCountries as string[]).filter((n) => countryToIso3(n) === null && !isKnownUnmapped(n));65 expect(missing).toEqual([]);66 // Sanity: the fixture is the real export (≈ 180 names), not an empty file.67 expect((dbCountries as string[]).length).toBeGreaterThan(150);68 // Explicitly unmapped names never resolve (a mapping added later must remove them from UNMAPPED).69 for (const u of UNMAPPED_COUNTRY_NAMES) expect(countryToIso3(u)).toBeNull();70 });71});72