package main import ( "strings" "testing" ) func TestAllKeysExistInBothLocales(t *testing.T) { enMessages := messages["en"] esMessages := messages["es"] for key := range enMessages { if _, ok := esMessages[key]; !ok { t.Errorf("key %q exists in 'en' but not in 'es'", key) } } for key := range esMessages { if _, ok := enMessages[key]; !ok { t.Errorf("key %q exists in 'es' but not in 'en'", key) } } } func TestFormatStringParity(t *testing.T) { enMessages := messages["en"] esMessages := messages["es"] for key, enFmt := range enMessages { esFmt, ok := esMessages[key] if !ok { continue } enCount := strings.Count(enFmt, "%s") + strings.Count(enFmt, "%d") esCount := strings.Count(esFmt, "%s") + strings.Count(esFmt, "%d") if enCount != esCount { t.Errorf("key %q: 'en' has %d format verbs, 'es' has %d", key, enCount, esCount) } } } func TestFallbackToEnglish(t *testing.T) { result := T("fr", MsgCommandEmpty) expected := messages["en"][MsgCommandEmpty] if result != expected { t.Errorf("expected fallback to English %q, got %q", expected, result) } } func TestLocaleNormalization(t *testing.T) { tests := []struct { locale string expected string }{ {"es-ES", "es"}, {"es-MX", "es"}, {"EN-US", "en"}, {"en", "en"}, {"", "en"}, } for _, tt := range tests { got := normalizeLocale(tt.locale) if got != tt.expected { t.Errorf("normalizeLocale(%q) = %q, want %q", tt.locale, got, tt.expected) } } } func TestTWithArgs(t *testing.T) { result := T("en", MsgNoBooksFound, "test query") expected := `No books found for "test query".` if result != expected { t.Errorf("expected %q, got %q", expected, result) } } func TestTWithSpanish(t *testing.T) { result := T("es", MsgNoBooksFound, "test query") expected := `No se encontraron libros para "test query".` if result != expected { t.Errorf("expected %q, got %q", expected, result) } } func TestTUnknownKeyReturnsKey(t *testing.T) { result := T("en", "nonexistent.key") if result != "nonexistent.key" { t.Errorf("expected key returned as-is, got %q", result) } } func TestTNoArgs(t *testing.T) { result := T("en", MsgCommandEmpty) expected := "Empty command received." if result != expected { t.Errorf("expected %q, got %q", expected, result) } }