Files
llmgw/internal/web/i18n_quotas_test.go
T
Ilker ManapandClaude Opus 5.5 102ca9556f An organization's admin, and forms only for those who may send them
C1. A membership of an organization may be admin as well as member and
manager. An admin sees /org as a manager does and changes the
organization from it: its teams and projects, who of the approved
accounts is in them, its members' roles, and the keys spent in its
projects. Every id a form names is checked against the organization;
limits, accounts, the organization's name and the cluster stay with the
cluster's staff, and an admin cannot take their own role away.

A group from OIDC, SCIM or a directory no longer rewrites a membership
it names as a plain member at every sign-in, which took a manager's role
away (EnsureMembership).

Pages draw a form only for someone who may send it: Nav.May asks the
staff rule staffMay keeps, and a page whose forms a person may not send
gets no form token. A viewer sees the people, limits and mirror pages
without forms; an operator the mirror's.

Forms are compact: labels close to their fields, fields as wide as what
goes in them, smaller buttons, and the short forms on one line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:38:06 +01:00

157 lines
6.1 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 (
"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", "Sınırlar", "Üyeler",
">sorumlu</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, ">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)
}
}