package router import ( "log/slog" "net/http" "net/http/httptest" "strings" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/web" ) // The switcher is at the foot of the sign-in page, so it has to work for // somebody with neither a key nor a session — and the choice has to stick. func TestTheLanguageSwitchNeedsNoKeyAndSticks(t *testing.T) { rt, _ := chatRouter(t, "http://127.0.0.1:1") h := rt.Handler() r := httptest.NewRequest(http.MethodGet, "http://gw.example/_lang/tr", nil) r.Header.Set("Referer", "http://gw.example/login") w := httptest.NewRecorder() h.ServeHTTP(w, r) if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/login" { t.Fatalf("%d to %q: %s", w.Code, w.Header().Get("Location"), w.Body.String()) } cookies := w.Result().Cookies() if len(cookies) == 0 { t.Fatal("no cookie remembers the choice") } r = httptest.NewRequest(http.MethodGet, "http://gw.example/login", nil) for _, c := range cookies { r.AddCookie(c) } w = httptest.NewRecorder() h.ServeHTTP(w, r) if !strings.Contains(w.Body.String(), ``) { t.Errorf("the sign-in page after choosing Turkish: %d %.300s", w.Code, w.Body.String()) } } func TestAServerSetToTurkishGreetsVisitorsInTurkish(t *testing.T) { t.Cleanup(func() { _ = web.SetDefaultLanguage("") }) cfg := baseConfig() cfg.Landing.Language = "tr" rt := newTestRouter(t, cfg) w := httptest.NewRecorder() rt.Handler().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil)) if !strings.Contains(w.Body.String(), ``) { t.Errorf("the landing page with no language asked for: %.300s", w.Body.String()) } // A browser that asks for English still gets it. r := httptest.NewRequest(http.MethodGet, "/", nil) r.Header.Set("Accept-Language", "en-GB,en;q=0.9") w = httptest.NewRecorder() rt.Handler().ServeHTTP(w, r) if !strings.Contains(w.Body.String(), ``) { t.Errorf("the landing page asked for in English: %.300s", w.Body.String()) } } func TestALanguageThePortalDoesNotSpeakStopsTheServer(t *testing.T) { t.Cleanup(func() { _ = web.SetDefaultLanguage("") }) cfg := baseConfig() cfg.Landing.Language = "klingon" if _, err := New(cfg, slog.New(slog.DiscardHandler)); err == nil || !strings.Contains(err.Error(), "landing.language") { t.Errorf("got %v, want an error naming landing.language", err) } }