// The URL-prefix i18n helpers — the load-bearing pure functions that drive the // [locale] mirror routing + the language switcher. `useUrlLocale` (the one hook // in this module) reads useLocation from @voltro/web; the rest are pure string // functions we can exercise directly. import { describe, expect, test } from 'vitest' import { SUPPORTED_LOCALES, DEFAULT_LOCALE, getCatalog, localeFromPathname, withLocalePrefix, stripLocalePrefix, } from './locale' describe('locale constants', () => { test('supports en + de with en as the default', () => { expect(SUPPORTED_LOCALES).toEqual(['en', 'de']) expect(DEFAULT_LOCALE).toBe('en') }) }) describe('getCatalog', () => { test('returns the matching catalog for a supported locale', () => { expect(getCatalog('de')['nav.language']).toBe('Sprache') expect(getCatalog('en')['nav.language']).toBe('Language') }) test('falls back to the default catalog for an unsupported locale', () => { expect(getCatalog('zz')['nav.language']).toBe('Language') }) }) describe('localeFromPathname', () => { test('extracts a prefixed locale', () => { expect(localeFromPathname('/de')).toBe('de') expect(localeFromPathname('/de/about')).toBe('de') }) test('defaults to en for the bare path or an unknown prefix', () => { expect(localeFromPathname('/')).toBe('en') expect(localeFromPathname('/about')).toBe('en') expect(localeFromPathname('/fr/about')).toBe('en') }) }) describe('withLocalePrefix', () => { test('leaves default-locale paths bare', () => { expect(withLocalePrefix('/about', 'en')).toBe('/about') expect(withLocalePrefix('/', 'en')).toBe('/') }) test('prefixes non-default locales, mapping / to /', () => { expect(withLocalePrefix('/about', 'de')).toBe('/de/about') expect(withLocalePrefix('/', 'de')).toBe('/de') }) }) describe('stripLocalePrefix', () => { test('removes a locale prefix, mapping / back to /', () => { expect(stripLocalePrefix('/de/about')).toBe('/about') expect(stripLocalePrefix('/de')).toBe('/') }) test('leaves an unprefixed path unchanged', () => { expect(stripLocalePrefix('/about')).toBe('/about') }) test('round-trips with withLocalePrefix', () => { expect(stripLocalePrefix(withLocalePrefix('/about', 'de'))).toBe('/about') }) })