Files
llmgw/internal/web/i18n_people_test.go
T
Ilker ManapandClaude Opus 5.5 11231d8c2b The portal in English and Turkish: a catalogue per language, a switcher at the foot of every page, landing.language, Turkish numbers and dates
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>
2026-10-05 00:12:53 +02:00

166 lines
6.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package web
import (
"context"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// peopleInTurkish sends a request to an admin page as a signed-in admin who chose
// Turkish with the switcher, and returns the page with its white space
// folded, so a sentence can be looked for across the template's lines.
func peopleInTurkish(t *testing.T, mux *http.ServeMux, method, path string, form url.Values) string {
t.Helper()
var r *http.Request
if form != nil {
r = httptest.NewRequest(method, path, strings.NewReader(form.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
r = httptest.NewRequest(method, path, nil)
}
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 %s: status %d: %s", method, path, w.Code, w.Body.String())
}
return strings.Join(strings.Fields(w.Body.String()), " ")
}
func TestThePeoplePageSpeaksTurkishWhenAsked(t *testing.T) {
q := oneTeam()
rpm := 60
q.limits["org:org-1"] = store.Limits{Scope: store.ScopeOrg, ScopeID: "org-1", RPM: &rpm}
u := &fakeScopeSeries{byGroup: map[store.Grouping][]store.UsageSeries{
store.GroupByOrg: {{Grouping: store.GroupByOrg, ID: "org-1", Name: "Scania", Requests: 1234,
Buckets: []store.Bucket{{Start: time.Now().Add(-time.Hour), Requests: 1234}}}},
}}
body := peopleInTurkish(t, peoplePage(t, q, u), http.MethodGet, "/admin/users?sel=org:org-1", nil)
for _, want := range []string{
`<html lang="tr">`,
"<title>Kişiler · llmgw</title>",
`aria-current="page">Kuruluşlar ve kişiler`,
"<h2>Kuruluşlar, ekipler ve projeler</h2>",
`<span class="q-kind">kuruluş</span>`,
"<dt>İstek/dk</dt><dd>60</dd>",
"Kullanım, son 7 gün",
"1,2 bin istek", // the count written the Turkish way
"Kuruluş ekle",
"Tüm hesaplar",
"Scania için kota", // the title handed to the quota form
"<code>llmgw users promote</code>",
} {
if !strings.Contains(body, want) {
t.Errorf("the Turkish people page lacks %q", want)
}
}
for _, english := range []string{"Organizations, teams and projects", "All accounts", "requests", "Quota for"} {
if strings.Contains(body, english) {
t.Errorf("the Turkish people page still says %q", english)
}
}
}
func TestAChangeToTheTreeIsReportedInTheReadersLanguageAndAuditedInEnglish(t *testing.T) {
s := &fakeAdmin{}
am := &auditMem{}
mux := tenancyPageWith(t, s, func(p *AccountPages) { p.SetAudit(am) })
body := peopleInTurkish(t, mux, http.MethodPost, "/admin/tenancy/org", url.Values{"csrf": {"tok"}, "name": {"Scania"}})
if !strings.Contains(body, "Scania kuruluşu oluşturuldu") {
t.Error("the page did not say in Turkish that the organization was created")
}
s.err = store.ErrDuplicateName
body = peopleInTurkish(t, mux, http.MethodPost, "/admin/tenancy/org", url.Values{"csrf": {"tok"}, "name": {"Scania"}})
if !strings.Contains(body, "burada bu adla zaten bir tane var") {
t.Error("a refusal was not said in Turkish")
}
// The audit log is a machine record, read and searched in one language.
if len(am.got) != 2 {
t.Fatalf("recorded %d", len(am.got))
}
if !strings.Contains(am.got[0].Detail, "organization Scania created") {
t.Errorf("the audit record is not in English: %s", am.got[0].Detail)
}
if !strings.Contains(am.got[1].Detail, "there is already one of those here") {
t.Errorf("the refusal's audit record is not in English: %s", am.got[1].Detail)
}
}
func TestAnAccountChangeNamesThePersonInTurkish(t *testing.T) {
mux := tenancyPage(t, &fakeAdmin{}, true)
body := peopleInTurkish(t, mux, http.MethodPost, "/admin/users/u2/leave",
url.Values{"csrf": {"tok"}, "scope": {"project:proj-9"}})
if !strings.Contains(body, "u2: proj-9 içinden çıkarıldı; orada oluşturduğu 1 anahtar iptal edildi") {
t.Errorf("the change was not said in Turkish: %s", body)
}
body = peopleInTurkish(t, mux, http.MethodPost, "/admin/users/u1/suspend", url.Values{"csrf": {"tok"}})
if !strings.Contains(body, "kendi hesabınızı askıya alamazsınız") {
t.Error("refusing to suspend oneself was not said in Turkish")
}
}
// guardSource is the guardrails page's store, with one of everything.
type guardSource struct{ at time.Time }
func (g guardSource) GuardEvents(context.Context, time.Time, int) ([]store.GuardEvent, error) {
return []store.GuardEvent{{At: g.at, RequestID: "req_1", ProjectID: "proj-1", Model: "qwen",
Status: 400, Action: "blocked", Policy: "base", Category: "iban", GuardMs: 1234}}, nil
}
func (g guardSource) GuardCounts(context.Context, time.Time) ([]store.GuardCount, error) {
return []store.GuardCount{{ProjectID: "", Action: "redacted", Policy: "base", Category: "email", Count: 1500}}, nil
}
func (g guardSource) GuardAttachments(context.Context) ([]guard.Attachment, error) {
return []guard.Attachment{{Scope: "project", ScopeID: "proj-1", Policy: "finance"}}, nil
}
func (g guardSource) AllProjects(context.Context) ([]store.ProjectPath, error) {
return []store.ProjectPath{{ID: "proj-1", Path: "Acme/fin/payroll"}}, nil
}
func TestTheGuardrailsPageSpeaksTurkishWhenAsked(t *testing.T) {
a := fakeAccounts{user: store.User{ID: "u1", Email: "a@example.com", IsAdmin: true}}
p := NewAccountPages(a, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil)))
mux := http.NewServeMux()
at := time.Date(2026, 10, 4, 9, 5, 7, 0, time.UTC)
p.RegisterAdminGuard(mux, guardSource{at: at}, guard.Config{
Default: []string{"base"}, Policies: map[string]guard.Policy{"base": {}, "finance": {}}})
body := peopleInTurkish(t, mux, http.MethodGet, "/admin/guard", nil)
for _, want := range []string{
"<title>Koruma kuralları · llmgw</title>",
"<h2>Politikalar</h2>",
"<code>server.yaml</code> içinde tanımlı:",
"<td>proje</td><td>Acme/fin/payroll</td>", // the scope's kind, in Turkish
"<td>kişisel</td><td>maskelendi</td>",
`<td class="num">1.500</td>`, // a count written the Turkish way
"<td>04.10.2026 09:05:07</td>", // and a day
"<td>engellendi</td>",
"<h2>Son olaylar</h2>",
} {
if !strings.Contains(body, want) {
t.Errorf("the Turkish guardrails page lacks %q", want)
}
}
for _, english := range []string{"Policies", "What they did", "personal", "blocked"} {
if strings.Contains(body, english) {
t.Errorf("the Turkish guardrails page still says %q", english)
}
}
}