import { afterEach, beforeEach, describe, expect, test } from 'bun:test' import { formatPhoneDisplay, formatPrice } from '../src/lib/format' import { browserTimezone, countryOf, currencyForRegion, localeForRegion, REGION_CURRENCY, REGION_LOCALE, regionForTimezone, regionOf, TIMEZONE_REGION, } from '../src/lib/region' /* Un importe se pinta con el país de la persona: su moneda y su regional (separadores, posición del símbolo). El país sale de la zona horaria del sistema y, si no se conoce, del idioma del browser. El valor persistido no cambia: no hay conversión. */ const nav = globalThis as { navigator?: { language?: string; languages?: string[] } } function stubLanguage(language: string) { Object.defineProperty(globalThis, 'navigator', { value: { language, languages: [language] }, configurable: true, writable: true, }) } function restoreNavigator() { delete (nav as { navigator?: unknown }).navigator } /** Simula el navegador: idioma y zona horaria del sistema. */ function stubLocation(language: string, timezone: string) { stubLanguage(language) const original = Intl.DateTimeFormat // Callable sin new, como Intl.DateTimeFormat(). const fake = () => ({ resolvedOptions: () => ({ timeZone: timezone }) }) ;(Intl as { DateTimeFormat: unknown }).DateTimeFormat = fake as never return () => { ;(Intl as { DateTimeFormat: unknown }).DateTimeFormat = original } } /** Ejecuta fn con un navegador simulado y deja todo como estaba. */ function withLocation(language: string, timezone: string, fn: () => void) { const restore = stubLocation(language, timezone) try { fn() } finally { restore() } } /** Pinta el importe como lo vería un browser con esa locale. */ function priceFor(amount: number, language: string): string { stubLanguage(language) return formatPrice(amount) } describe('formatPrice — país del browser', () => { beforeEach(() => stubLanguage('es-AR')) afterEach(() => restoreNavigator()) test('el idioma del browser define moneda y regional', () => { expect(priceFor(50_000, 'es-AR')).toBe('$50.000') expect(priceFor(50_000, 'es-MX')).toBe('$50,000') expect(priceFor(50_000, 'es-PE')).toBe('S/50,000') expect(priceFor(50_000, 'pt-BR')).toBe('R$50.000') expect(priceFor(50_000, 'es-ES')).toBe('50.000\u00A0€') expect(priceFor(50_000, 'en-US')).toBe('$50,000') expect(priceFor(50_000, 'en-CA')).toBe('$50,000') }) test('la zona horaria define el país si el idioma no lo dice', () => { // Browser en inglés, usuario en Argentina: pesos y separador de miles argentino. withLocation('en-US', 'America/Argentina/Buenos_Aires', () => { expect(formatPrice(50_000)).toBe('$50.000') }) }) test('la zona horaria gana sobre el idioma del browser', () => { withLocation('es-MX', 'America/Argentina/Buenos_Aires', () => { expect(formatPrice(50_000)).toBe('$50.000') }) }) test('la zona horaria cubre otros países', () => { withLocation('es-419', 'America/Lima', () => { expect(formatPrice(50_000)).toBe('S/50,000') }) withLocation('es-419', 'America/Mexico_City', () => { expect(formatPrice(50_000)).toBe('$50,000') }) withLocation('es-419', 'Europe/Madrid', () => { expect(formatPrice(50_000)).toBe('50.000\u00A0€') }) }) test('los centavos se conservan', () => { expect(priceFor(50_000.5, 'es-AR')).toBe('$50.000,50') expect(priceFor(50_000.5, 'es-MX')).toBe('$50,000.50') }) test('sin país ni zona conocida se ve el símbolo $', () => { withLocation('es-419', 'Africa/Algiers', () => { expect(formatPrice(50_000)).toBe('$50,000') }) }) test('sin browser se ve el símbolo $ con la regional del runtime', () => { restoreNavigator() const value = formatPrice(50_000) expect(value).toContain('$') expect(value).toContain('50') expect(value).not.toContain('ARS') }) test('por defecto usa la locale del browser', () => { stubLanguage('en-US') expect(formatPrice(50_000)).toBe('$50,000') }) test('importes nulos y cero', () => { expect(formatPrice(null)).toBe('—') expect(priceFor(0, 'es-AR')).toBe('$0') }) }) describe('formatPrice — moneda y locale explícitos', () => { beforeEach(() => stubLanguage('es-AR')) afterEach(() => restoreNavigator()) test('separa y posiciona según la locale pedida', () => { expect(formatPrice(50_000, 'ARS', 'es-AR')).toBe('$50.000') expect(formatPrice(50_000, 'ARS', 'en-US')).toBe('ARS\u00A050,000') expect(formatPrice(50_000, 'ARS', 'es-ES')).toBe('50.000\u00A0ARS') }) test('muestra centavos solo cuando el importe los tiene', () => { expect(formatPrice(50_000.5, 'ARS', 'en-US')).toBe('ARS\u00A050,000.50') expect(formatPrice(50_000.5, 'ARS', 'es-AR')).toBe('$50.000,50') expect(formatPrice(50_000, 'ARS', 'en-US')).toBe('ARS\u00A050,000') }) test('ganan sobre el país del browser', () => { withLocation('en-US', 'America/Lima', () => { expect(formatPrice(50_000, 'ARS', 'es-AR')).toBe('$50.000') expect(formatPrice(50_000, 'ARS', 'en-US')).toBe('ARS\u00A050,000') }) }) }) describe('region', () => { test('el mapa de monedas usa códigos ISO válidos', () => { const known = Intl.supportedValuesOf('currency') for (const [region, currency] of Object.entries(REGION_CURRENCY)) { expect(region).toMatch(/^[A-Z]{2}$/) expect(currency).toMatch(/^[A-Z]{3}$/) expect(known).toContain(currency) } }) test('el mapa de zonas usa códigos ISO y zonas reales', () => { for (const [zone, region] of Object.entries(TIMEZONE_REGION)) { expect(region).toMatch(/^[A-Z]{2}$/) if (zone.endsWith('/')) continue expect(() => new Intl.DateTimeFormat('en-US', { timeZone: zone })).not.toThrow() } }) test('las locales por país son tags válidos y cubren a los países', () => { for (const [region, locale] of Object.entries(REGION_LOCALE)) { expect(region).toMatch(/^[A-Z]{2}$/) expect(locale).toMatch(/^[a-z]{2}-[A-Z]{2}$/) expect(() => new Intl.NumberFormat(locale)).not.toThrow() } for (const region of Object.keys(REGION_CURRENCY)) { expect(localeForRegion(region)).toBeDefined() } }) test('el mapa de zonas cubre países que tienen moneda', () => { for (const zone of Object.keys(TIMEZONE_REGION)) { const resolvable = zone.endsWith('/') ? `${zone}Test` : zone expect(currencyForRegion(regionForTimezone(resolvable))).toBeDefined() } }) test('resuelve la zona por ciudad y por prefijo', () => { expect(regionForTimezone('America/Argentina/Buenos_Aires')).toBe('AR') expect(regionForTimezone('America/Argentina/Catamarca')).toBe('AR') expect(regionForTimezone('America/Sao_Paulo')).toBe('BR') expect(regionForTimezone('America/Lima')).toBe('PE') expect(regionForTimezone('Europe/Madrid')).toBe('ES') expect(regionForTimezone('Asia/Tokyo')).toBeUndefined() expect(regionForTimezone('Africa/Algiers')).toBeUndefined() expect(regionForTimezone('UTC')).toBeUndefined() expect(regionForTimezone(undefined)).toBeUndefined() }) test('las zonas legacy que reporta el browser también resuelven', () => { // Chromium/ICU canonicaliza America/Argentina/X a America/X. expect(regionForTimezone('America/Buenos_Aires')).toBe('AR') expect(regionForTimezone('America/Catamarca')).toBe('AR') expect(regionForTimezone('America/Mendoza')).toBe('AR') }) test('resuelve el país: la zona horaria primero', () => { withLocation('en-US', 'America/Argentina/Buenos_Aires', () => { expect(countryOf('en-US')).toBe('AR') }) withLocation('es-419', 'America/Lima', () => { expect(countryOf('es-419')).toBe('PE') }) // Sin zona conocida manda el idioma. withLocation('es-MX', 'Africa/Algiers', () => { expect(countryOf('es-MX')).toBe('MX') }) }) test('regionOf usa el locale que le pasan', () => { expect(regionOf('es-MX')).toBe('MX') expect(regionOf('es-419')).toBe('419') expect(regionOf('pt-BR')).toBe('BR') expect(regionOf(undefined)).toBeUndefined() }) test('lookup de moneda y regional por región', () => { expect(currencyForRegion('PE')).toBe('PEN') expect(currencyForRegion('MX')).toBe('MXN') expect(currencyForRegion('ES')).toBe('EUR') expect(currencyForRegion('US')).toBe('USD') expect(currencyForRegion('419')).toBeUndefined() expect(currencyForRegion('SE')).toBe('SEK') expect(currencyForRegion(undefined)).toBeUndefined() expect(localeForRegion('AR')).toBe('es-AR') expect(localeForRegion('BR')).toBe('pt-BR') expect(localeForRegion('419')).toBeUndefined() expect(localeForRegion(undefined)).toBeUndefined() }) test('el runtime expone una zona horaria', () => { // bun test corre en UTC; en el browser es la zona del sistema. expect(browserTimezone()).toBeTruthy() }) }) describe('formatPhoneDisplay — región del número y país del browser', () => { beforeEach(() => stubLanguage('es-AR')) afterEach(() => restoreNavigator()) test('números argentinos conservan el formato actual', () => { expect(formatPhoneDisplay('+5492954667788', 'es-AR')).toBe('+54 9 2954 66-7788') expect(formatPhoneDisplay('5490989009988', 'es-AR')).toBe('+54 9 989 00-9988') expect(formatPhoneDisplay('0989009988', 'es-AR')).toBe('+54 989 00-9988') expect(formatPhoneDisplay('2983649550', 'es-AR')).toBe('2983 64-9550') }) test('número irreconocible se devuelve tal cual', () => { expect(formatPhoneDisplay('3209872223344', 'es-AR')).toBe('3209872223344') expect(formatPhoneDisplay('12345', 'es-AR')).toBe('12345') expect(formatPhoneDisplay(null)).toBeNull() expect(formatPhoneDisplay('')).toBeNull() }) test('números de otra región usan las reglas de esa región', () => { expect(formatPhoneDisplay('+12125551234', 'es-AR')).toBe('(212) 555-1234') expect(formatPhoneDisplay('+34612345678', 'es-AR')).toBe('+34 612 34 56 78') expect(formatPhoneDisplay('+525512345678', 'es-AR')).toBe('+52 55 1234 5678') }) test('nacional sin prefijo se agrupa según el país del browser', () => { expect(formatPhoneDisplay('2125551234', 'en-US')).toBe('(212) 555-1234') expect(formatPhoneDisplay('612345678', 'es-ES')).toBe('612 34 56 78') expect(formatPhoneDisplay('5512345678', 'es-MX')).toBe('55 1234 5678') }) test('la zona horaria define el país de los números nacionales', () => { withLocation('en-US', 'America/Argentina/Buenos_Aires', () => { expect(formatPhoneDisplay('3364609550')).toBe('3364 60-9550') }) }) test('el prefijo del número manda sobre el país del browser', () => { expect(formatPhoneDisplay('+5492954667788', 'en-US')).toBe('+54 9 2954 66-7788') }) test('región sin reglas propias cae a E.164', () => { expect(formatPhoneDisplay('+331234567890', 'es-AR')).toBe('+331234567890') expect(formatPhoneDisplay('00331234567890', 'es-AR')).toBe('+331234567890') }) test('por defecto usa el país del browser', () => { stubLanguage('en-US') expect(formatPhoneDisplay('2125551234')).toBe('(212) 555-1234') }) })