Files
llmgw/internal/web/admin_people_test.go
T
Ilker ManapandClaude Opus 5.5 fffed716be Make people, limits and usage one page, with a panel per branch
A team's members, quota and spend were on three pages that linked to each
other. They are three tabs of one page now, each at its old address, and
choosing anything in the tree opens its limits, capacity and use beside it,
editable in place.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 19:10:48 +02:00

88 lines
3.1 KiB
Go

package web
import (
"io"
"log/slog"
"net/http"
"net/url"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
// peoplePage mounts all three tabs, as the server does.
func peoplePage(t *testing.T, q *fakeQuotas, u *fakeScopeSeries) *http.ServeMux {
t.Helper()
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()
p.RegisterAdminUsers(mux, &fakeAdmin{orgs: q.orgs})
p.RegisterAdminQuotas(mux, q, pricedYes{})
p.RegisterAdminUsage(mux, u, pricedYes{})
return mux
}
func TestABranchChosenInTheTreeShowsItsLimitsAndUseTogether(t *testing.T) {
// People, their limits and their use were three pages: a team's members
// on one, its quota on another, its spend on a third.
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 := strings.Join(strings.Fields(
mirrorGet(t, peoplePage(t, q, u), "/admin/users?sel=org:org-1").Body.String()), " ")
for _, want := range []string{
`class="people-detail"`, "<h2>Scania</h2>",
"<dt>Requests/min</dt><dd>60</dd>", // its own limit
"1.2k requests", // its use over the week
`name="sel" value="org:org-1"`, // changing it comes back here
`aria-current="page">Organizations &amp; people`,
} {
if !strings.Contains(body, want) {
t.Errorf("the panel lacks %q", want)
}
}
if u.asked[len(u.asked)-1] != store.GroupByOrg {
t.Errorf("use was read by %v, not by organization", u.asked)
}
}
func TestAChangeMadeInAPanelComesBackToThePanel(t *testing.T) {
q := oneTeam()
body := mirrorPost(t, peoplePage(t, q, &fakeScopeSeries{}), "/admin/quotas", url.Values{
"csrf": {"tok"}, "scope": {"org"}, "id": {"org-1"}, "rpm": {"30"},
"tab": {"people"}, "sel": {"org:org-1"},
}).Body.String()
if len(q.put) != 1 || *q.put[0].RPM != 30 {
t.Fatalf("the limit was not saved: %+v", q.put)
}
if !strings.Contains(body, `class="people-detail"`) || !strings.Contains(body, "rpm 30") {
t.Error("saving from the panel did not come back to it with the result")
}
}
func TestEachOldAddressOpensItsTab(t *testing.T) {
mux := peoplePage(t, oneTeam(), &fakeScopeSeries{})
for path, tab := range map[string]string{
"/admin/users": "Organizations &amp; people", "/admin/quotas": "Limits", "/admin/usage": "Usage",
} {
body := mirrorGet(t, mux, path).Body.String()
if !strings.Contains(body, `aria-current="page">`+tab) {
t.Errorf("%s does not open the %s tab", path, tab)
}
}
}
func TestSomethingNotInTheTreeOpensNoPanel(t *testing.T) {
body := mirrorGet(t, peoplePage(t, oneTeam(), &fakeScopeSeries{}), "/admin/users?sel=org:gone").Body.String()
if strings.Contains(body, `class="people-detail"`) {
t.Error("a panel opened for an organization that does not exist")
}
}