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{ ``, "
llmgw users promote",
} {
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}, func() guard.Config {
return 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{
"llmgw ctl config):",
"