Every template and handler message in internal/web goes through a JSON
catalogue under internal/web/locales/<code>/, with English as the fallback.
The language is the person's choice (a cookie set by /_lang/{code}), else
Accept-Language, else landing.language. Tests refuse a key some language
lacks, a translation with other arguments than English, and template text
left in English.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package web
|
||
|
||
import (
|
||
"bytes"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
|
||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||
)
|
||
|
||
func TestTheChatPageSpeaksTurkishWhenAsked(t *testing.T) {
|
||
h := Chat(store.User{Email: "a@example.com"},
|
||
[]ChatModel{{Name: "qwen"}, {Name: "big-model", Simulated: true}}, nil, "", "v1")
|
||
r := httptest.NewRequest(http.MethodGet, "http://x/chat", nil)
|
||
r.Header.Set("Accept-Language", "tr-TR,tr;q=0.9")
|
||
w := httptest.NewRecorder()
|
||
h(w, r)
|
||
if w.Code != http.StatusOK {
|
||
t.Fatalf("status %d", w.Code)
|
||
}
|
||
page := w.Body.String()
|
||
for _, want := range []string{
|
||
`<html lang="tr">`,
|
||
"<h1>Sohbet</h1>",
|
||
">big-model (benzetim)<",
|
||
"Yeni sohbet",
|
||
`placeholder="Bir şey sorun…"`,
|
||
`<output id="temp-out">0,7</output>`,
|
||
// The script's own words come from the catalogue too.
|
||
`"Uzunluk sınırında kesildi — devam etmesini isteyin."`,
|
||
`decimal: ","`,
|
||
} {
|
||
if !strings.Contains(page, want) {
|
||
t.Errorf("the Turkish chat page does not have %q", want)
|
||
}
|
||
}
|
||
for _, not := range []string{"New conversation", "Cut off at the length limit", "Ask something"} {
|
||
if strings.Contains(page, not) {
|
||
t.Errorf("the Turkish chat page still says %q", not)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestTheChatDownloadRefusesInTheReadersLanguage(t *testing.T) {
|
||
r := httptest.NewRequest(http.MethodPost, "/chat/download", strings.NewReader(`{"content":"no code here"}`))
|
||
r.AddCookie(&http.Cookie{Name: langCookie, Value: "tr"})
|
||
w := httptest.NewRecorder()
|
||
CodeArchive(1<<20)(w, r)
|
||
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "indirilecek kod yok") {
|
||
t.Errorf("%d %q", w.Code, w.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestTheAboutPageSpeaksTurkishWhenAsked(t *testing.T) {
|
||
code, body := get(t, About("v1.2.3"), "http://llmgw.example/about",
|
||
map[string]string{"Accept-Language": "tr"})
|
||
if code != http.StatusOK {
|
||
t.Fatalf("status %d", code)
|
||
}
|
||
for _, want := range []string{
|
||
`<html lang="tr">`,
|
||
`<a href="/">Bu ağ geçidi</a>`,
|
||
"<em>Kendi GPU'larınızın</em> önünde tek bir uç nokta.",
|
||
"Sıfırdan ilk isteğe",
|
||
"Sağlama toplamları",
|
||
// The configurations are shown as they are.
|
||
"reverse_proxy",
|
||
"proxy_read_timeout",
|
||
} {
|
||
if !strings.Contains(body, want) {
|
||
t.Errorf("the Turkish about page does not have %q", want)
|
||
}
|
||
}
|
||
if strings.Contains(body, "What it does") {
|
||
t.Error("the Turkish about page still has English headings")
|
||
}
|
||
|
||
// The published website is English whatever anyone's browser says.
|
||
d := NewAboutData("v1.2.3")
|
||
d.Site = true
|
||
var b bytes.Buffer
|
||
if err := RenderAbout(&b, d); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !strings.Contains(b.String(), "What it does") || strings.Contains(b.String(), "Ne yapar") {
|
||
t.Error("the website is not in English")
|
||
}
|
||
}
|