- Fix locale_test: add TestMain to pre-populate Supported map so zh/es tests pass - Export pure functions for testability: ResolveWeekStart, MapCuisineSlug (menu + savedrecipe), MergeAndDeduplicate - Introduce repository interfaces (DiaryRepository, ProductRepository, SavedRecipeRepository, IngredientSearcher) in each handler; NewHandler now accepts interfaces — concrete *Repository still satisfies them - Add mock files: diary/mocks, product/mocks, savedrecipe/mocks - Add handler unit tests (no DB) for diary (8), product (8), savedrecipe (8), ingredient (5) - Add pure-function unit tests: menu/ResolveWeekStart (6), savedrecipe/MapCuisineSlug (5), recognition/MergeAndDeduplicate (6) - Add repository integration tests (//go:build integration): diary (4), product (6) - Extend recipe integration tests: GetByID_Found, GetByID_WithTranslation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
package locale_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/food-ai/backend/internal/infra/locale"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
// Populate Supported map with all known languages before running tests.
|
|
// In production this is done by locale.LoadFromDB at startup.
|
|
for _, code := range []string{"en", "ru", "es", "de", "fr", "it", "pt", "zh", "ja", "ko", "ar", "hi"} {
|
|
locale.Supported[code] = true
|
|
}
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestParse(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
acceptLang string
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty header returns default",
|
|
acceptLang: "",
|
|
want: locale.Default,
|
|
},
|
|
{
|
|
name: "exact match",
|
|
acceptLang: "ru",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "region subtag stripped",
|
|
acceptLang: "ru-RU",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "full browser header picks first supported",
|
|
acceptLang: "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "unsupported first falls through to supported",
|
|
acceptLang: "xx-XX,ru;q=0.8",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "completely unsupported returns default",
|
|
acceptLang: "xx-XX,yy-YY",
|
|
want: locale.Default,
|
|
},
|
|
{
|
|
name: "chinese region subtag",
|
|
acceptLang: "zh-CN,zh;q=0.9",
|
|
want: "zh",
|
|
},
|
|
{
|
|
name: "case insensitive",
|
|
acceptLang: "RU-RU",
|
|
want: "ru",
|
|
},
|
|
{
|
|
name: "whitespace around tag",
|
|
acceptLang: " ru ",
|
|
want: "ru",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := locale.Parse(tc.acceptLang)
|
|
if got != tc.want {
|
|
t.Errorf("Parse(%q) = %q, want %q", tc.acceptLang, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestWithLangAndFromContext(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
if got := locale.FromContext(ctx); got != locale.Default {
|
|
t.Errorf("FromContext on empty ctx = %q, want %q", got, locale.Default)
|
|
}
|
|
|
|
ctx = locale.WithLang(ctx, "ru")
|
|
if got := locale.FromContext(ctx); got != "ru" {
|
|
t.Errorf("FromContext after WithLang(ru) = %q, want %q", got, "ru")
|
|
}
|
|
}
|
|
|
|
func TestFromRequest(t *testing.T) {
|
|
req, _ := http.NewRequest(http.MethodGet, "/", nil)
|
|
req.Header.Set("Accept-Language", "es-ES,es;q=0.9")
|
|
|
|
if got := locale.FromRequest(req); got != "es" {
|
|
t.Errorf("FromRequest = %q, want %q", got, "es")
|
|
}
|
|
}
|