package web import ( "sync" "testing" ) func TestLoadLocalesReturnsIndependentCopies(t *testing.T) { first, err := LoadLocales() if err != nil { t.Fatalf("LoadLocales() error = %v", err) } if len(first) == 0 || len(first[0].Regions) == 0 { t.Fatal("locale fixture unexpectedly empty") } originalLanguage := first[0].LanguageName originalRegion := first[0].Regions[0].CountryName first[0].LanguageName = "mutated" first[0].Regions[0].CountryName = "mutated" second, err := LoadLocales() if err != nil { t.Fatalf("second LoadLocales() error = %v", err) } if second[0].LanguageName != originalLanguage || second[0].Regions[0].CountryName != originalRegion { t.Fatalf("caller mutation escaped into locale cache: %+v", second[0]) } } func TestLoadLocalesConcurrent(t *testing.T) { var wg sync.WaitGroup for range 32 { wg.Add(1) go func() { defer wg.Done() entries, err := LoadLocales() if err != nil || len(entries) == 0 { t.Errorf("LoadLocales() = %d entries, %v", len(entries), err) } }() } wg.Wait() } func TestGetRegionalLanguageName(t *testing.T) { // Initialize locale cache LoadLocales() tests := []struct { name string locale string expected string }{ { name: "Japanese - no region", locale: "ja", expected: "Japanese", }, { name: "Spanish - Latin America", locale: "es-419", expected: "Spanish (Latin America)", // Language name already contains region in data }, { name: "English - US", locale: "en", expected: "English", }, { name: "Russian - Belarus", locale: "ru-BY", expected: "Russian - Belarus", }, { name: "Arabic - no region", locale: "ar", expected: "Arabic", }, { name: "Chinese - Simplified", locale: "zh-CN", expected: "Chinese - China", }, { name: "Unknown locale", locale: "xx-YY", expected: "xx-YY", }, { name: "Empty locale", locale: "", expected: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetRegionalLanguageName(tt.locale) if result != tt.expected { t.Errorf("GetRegionalLanguageName(%q) = %q, want %q", tt.locale, result, tt.expected) } }) } } func TestGetRegionalNativeName(t *testing.T) { // Initialize locale cache LoadLocales() tests := []struct { name string locale string expected string }{ { name: "Japanese - no region", locale: "ja", expected: "日本語 (にほんご)", }, { name: "Spanish - Latin America", locale: "es-419", expected: "Español (América Latina)", // Language name already contains region in data }, { name: "English - US", locale: "en", expected: "English", }, { name: "Russian - Belarus", locale: "ru-BY", expected: "Русский - Беларусь", }, { name: "Arabic - no region", locale: "ar", expected: "العربية", }, { name: "Chinese - Simplified", locale: "zh-CN", expected: "中文 (Zhōngwén) - 中國 / 中国 / Zhōngguó", // Complex multi-script native name }, { name: "Unknown locale", locale: "xx-YY", expected: "xx-YY", }, { name: "Empty locale", locale: "", expected: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetRegionalNativeName(tt.locale) if result != tt.expected { t.Errorf("GetRegionalNativeName(%q) = %q, want %q", tt.locale, result, tt.expected) } }) } } func TestGetLanguageCountryCode(t *testing.T) { // Initialize locale cache LoadLocales() tests := []struct { name string locale string expected string }{ { name: "English - default to US", locale: "en", expected: "US", }, { name: "Spanish - default to ES", locale: "es", expected: "ES", }, { name: "Arabic - default to SA", locale: "ar", expected: "SA", }, { name: "Japanese - default to JP", locale: "ja", expected: "JP", }, { name: "Spanish - Latin America", locale: "es-419", expected: "419", // This is correct for es-419 (not a country) }, { name: "Russian - Belarus", locale: "ru-BY", expected: "BY", }, { name: "Chinese - Simplified", locale: "zh-CN", expected: "CN", }, { name: "Unknown locale", locale: "xx-YY", expected: "", }, { name: "Empty locale", locale: "", expected: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetLanguageCountryCode(tt.locale) if result != tt.expected { t.Errorf("GetLanguageCountryCode(%q) = %q, want %q", tt.locale, result, tt.expected) } }) } } func TestDefaultCountryCodes(t *testing.T) { // Test that default country codes are set for common languages expectedDefaults := map[string]string{ "en": "US", "es": "ES", "ar": "SA", "ja": "JP", "zh": "CN", "fr": "FR", "de": "DE", "ru": "RU", "pt": "BR", "it": "IT", "ko": "KR", } for langCode, expectedCountry := range expectedDefaults { t.Run(langCode, func(t *testing.T) { result := GetLanguageCountryCode(langCode) if result != expectedCountry { t.Errorf("GetLanguageCountryCode(%q) = %q, want %q", langCode, result, expectedCountry) } }) } }