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>
157 lines
6.2 KiB
Go
157 lines
6.2 KiB
Go
package web
|
||
|
||
import (
|
||
"log/slog"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"net/url"
|
||
"strings"
|
||
"testing"
|
||
|
||
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
|
||
"gitea.mynodes.xyz/ilker/llmgw/internal/control/quota"
|
||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||
)
|
||
|
||
// inTurkish is a signed-in request from someone who chose Türkçe with the
|
||
// switcher; form, when given, makes it a POST.
|
||
func inTurkish(t *testing.T, mux *http.ServeMux, path string, form url.Values) string {
|
||
t.Helper()
|
||
r := httptest.NewRequest("GET", path, nil)
|
||
if form != nil {
|
||
r = httptest.NewRequest("POST", path, strings.NewReader(form.Encode()))
|
||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
}
|
||
r.AddCookie(&http.Cookie{Name: SessionCookie, Value: "good"})
|
||
r.AddCookie(&http.Cookie{Name: csrfCookie, Value: "tok"})
|
||
r.AddCookie(&http.Cookie{Name: langCookie, Value: "tr"})
|
||
w := httptest.NewRecorder()
|
||
mux.ServeHTTP(w, r)
|
||
if w.Code != http.StatusOK {
|
||
t.Fatalf("%s answered %d", path, w.Code)
|
||
}
|
||
return w.Body.String()
|
||
}
|
||
|
||
func TestTheLimitsTabSpeaksTurkishWhenAsked(t *testing.T) {
|
||
s := oneTeam()
|
||
rpm := 60
|
||
s.limits["org:org-1"] = store.Limits{Scope: store.ScopeOrg, ScopeID: "org-1", RPM: &rpm}
|
||
s.limits["team:team-1"] = store.Limits{
|
||
Scope: store.ScopeTeam, ScopeID: "team-1",
|
||
Budget: i64(1_234_500_000), BudgetPeriod: store.PeriodMonth,
|
||
}
|
||
big := 120
|
||
s.limits["project:proj-1"] = store.Limits{Scope: store.ScopeProject, ScopeID: "proj-1", RPM: &big}
|
||
s.usage = map[store.Scope][]store.ScopeUsage{
|
||
store.ScopeTeam: {{ID: "team-1", CostMicros: 617_250_000}},
|
||
}
|
||
body := inTurkish(t, quotasPage(t, s, true), "/admin/quotas", nil)
|
||
|
||
for _, want := range []string{
|
||
"Sınırlar nasıl birleşir", "İstek/dk", ">ekip<", ">değiştir</a>", ">üye başına</a>",
|
||
// A budget is written with Turkish separators and a Turkish period,
|
||
// and so is what was spent against it.
|
||
"1.234,50", "/ay", "617,25", "%50",
|
||
// The note under a limit an ancestor holds lower, built in Go.
|
||
"Scania bunu dakikada 60 istek ile sınırlıyor",
|
||
} {
|
||
if !strings.Contains(body, want) {
|
||
t.Errorf("the limits tab in Turkish lacks %q", want)
|
||
}
|
||
}
|
||
for _, unwanted := range []string{"How limits combine", "caps this at", "1234.50"} {
|
||
if strings.Contains(body, unwanted) {
|
||
t.Errorf("the limits tab in Turkish still says %q", unwanted)
|
||
}
|
||
}
|
||
// The form's values stay as the server reads them back, whatever the page
|
||
// speaks: a Turkish decimal comma would not parse.
|
||
body = inTurkish(t, quotasPage(t, s, true), "/admin/quotas?edit=team:team-1", nil)
|
||
if !strings.Contains(body, `name="budget" value="1234.50"`) {
|
||
t.Error("the budget's form value should be written the C way")
|
||
}
|
||
}
|
||
|
||
func TestARefusedLimitIsExplainedInTurkish(t *testing.T) {
|
||
s := oneTeam()
|
||
body := inTurkish(t, quotasPage(t, s, true), "/admin/quotas", url.Values{
|
||
"csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "rpm": {"lots"},
|
||
})
|
||
if !strings.Contains(body, "dakikadaki istek sayısı tam sayı olmalı") {
|
||
t.Errorf("the refusal is not in Turkish: %s", body)
|
||
}
|
||
body = inTurkish(t, quotasPage(t, s, true), "/admin/quotas", url.Values{
|
||
"csrf": {"tok"}, "scope": {"team"}, "id": {"team-1"}, "rpm": {"60"},
|
||
})
|
||
if !strings.Contains(body, "ekip team-1: istek/dk 60") {
|
||
t.Errorf("the change is not reported in Turkish: %s", body)
|
||
}
|
||
}
|
||
|
||
func TestTheCapacitySectionSpeaksTurkishWhenAsked(t *testing.T) {
|
||
g, l := 8, 16
|
||
fc := &fakeCapacity{views: []adminapi.QuotaView{{Scope: "team:t1", Name: "Acme / ml",
|
||
Holds: quota.Amount{GPUs: 6}, Guarantee: store.Resources{GPUs: &g},
|
||
Limit: store.Resources{GPUs: &l}}}}
|
||
a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: true}}
|
||
p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.DiscardHandler))
|
||
p.SetCapacity(fc)
|
||
mux := http.NewServeMux()
|
||
p.RegisterAdminQuotas(mux, oneTeam(), pricedYes{})
|
||
body := inTurkish(t, mux, "/admin/quotas", nil)
|
||
for _, want := range []string{"Kapasite", "6 / 8 (en çok 16)", "Güvence altındaki GPU"} {
|
||
if !strings.Contains(body, want) {
|
||
t.Errorf("the capacity section in Turkish lacks %q", want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestTheUsageReportWritesTurkishNumbers(t *testing.T) {
|
||
s := &fakeScopeSeries{byGroup: map[store.Grouping][]store.UsageSeries{
|
||
store.GroupByTeam: {hourly(store.GroupByTeam, "t1", "platform", 3)},
|
||
store.GroupByUser: {{Grouping: store.GroupByUser}},
|
||
}}
|
||
body := inTurkish(t, usagePage(t, s, pricedYes{}), "/admin/usage", nil)
|
||
// Three requests an hour rising over six hours is 63 requests of 120
|
||
// tokens each: 7560 tokens, said the Turkish way; and 0.063 spent.
|
||
for _, want := range []string{"Gruplama", "son 7 gün", ">ekip</a>", "İstekler", "Hatalar",
|
||
"7,6 bin", "0,06"} {
|
||
if !strings.Contains(body, want) {
|
||
t.Errorf("the usage report in Turkish lacks %q", want)
|
||
}
|
||
}
|
||
if strings.Contains(body, "last 7 days") || strings.Contains(body, "7.6k") {
|
||
t.Error("the usage report in Turkish still has English in it")
|
||
}
|
||
body = inTurkish(t, usagePage(t, s, pricedNo{}), "/admin/usage?by=user", nil)
|
||
for _, want := range []string{"sahibi olmayan anahtarlar", "Hiçbir modelin fiyatı yok"} {
|
||
if !strings.Contains(body, want) {
|
||
t.Errorf("the usage report by person in Turkish lacks %q", want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestTheOrganizationPageSpeaksTurkishWhenAsked(t *testing.T) {
|
||
body := inTurkish(t, orgPage(t, store.User{ID: "u1", Email: "boss@acme"}), "/org/org-a", nil)
|
||
for _, want := range []string{"2 kişi", "Kullanım", "Ekipler, projeler ve sınırlar", "Üyeler",
|
||
">yönetici</b>", "kuruluşun tamamı", "son 7 gün", `lang="tr"`} {
|
||
if !strings.Contains(body, want) {
|
||
t.Errorf("the organization page in Turkish lacks %q", want)
|
||
}
|
||
}
|
||
if strings.Contains(body, "Teams, projects and limits") {
|
||
t.Error("the organization page in Turkish still has English headings")
|
||
}
|
||
}
|
||
|
||
func TestThePageAboutNoOrganizationsSpeaksTurkish(t *testing.T) {
|
||
p := NewAccountPages(fakeAccounts{user: store.User{ID: "a", IsAdmin: true}}, nil, nil, "v0", slog.New(slog.DiscardHandler))
|
||
mux := http.NewServeMux()
|
||
p.RegisterOrgPages(mux, noOrgs{}, nil)
|
||
body := inTurkish(t, mux, "/org", nil)
|
||
if !strings.Contains(body, "Bu kümede hiç kuruluş yok") || !strings.Contains(body, "<title>Kuruluşlar · llmgw</title>") {
|
||
t.Errorf("the page about no organizations is not in Turkish: %s", body)
|
||
}
|
||
}
|