Files
magpie/internal/gui/model_loads_test.go
T
yetone f3681b90c3 gui: the Agents page says a model's load by the picked model and in its picker (axelburks on Discord)
axelburks (Discord #feedback) could see a model's load only on its
provider's page, too deep to judge which model to pick on the Agents
page. A plugin's reported load (magpie.load) now shows there too, as the
same quiet grey badge the provider page uses:

- by the picked model in each agent's model row, before the chevron;
- on each row of that row's model picker, after the speed.

GET /api/model-loads (speeds.go) answers every fresh load (LoadFresh,
5 minutes) by "provider/model" lowercased, the ref /api/model-speeds
uses: plugin.FreshLoads copies the fresh entries under one lock, and
provider.ModelLoads keys them by magpie's provider id, the user's own
listed id too (ModelIDs), with model@size falling back to its model as
ModelLoad does. With no plugin reporting it is one lock and an empty
map. The page reads it when a model picker opens and with the Agents
page's 15s drift check, each throttled to once per 30s, and the drift
read only while a row shows a magpie model. No new poll; /api/state and
Option are unchanged. The core script stays under its budget
(1,533,802) by shortening comments in the picker and agent-row code;
no budget raised. The
route is named in the CLI's route list (read-only, U) like model-speeds.

Run at this head (rebased on origin/main b4073c06a; the rebase onto
dcc84dbc0 touches none of these files), on mote:
- go test ./internal/gui (TestModelLoadsByRef, TestModelLoadsNoneReported,
  budgets) ./internal/plugin ./internal/provider ./internal/cli: ok,
  except TestEveryRouteHasACommandOrIsNamed's POST
  /api/usage/checkin-account, red on main (#4826, e87140242).
- Full go test ./... -timeout 45m before the rebase: 84 packages ok;
  reds were TestEveryRouteHasACommandOrIsNamed (this route, now named,
  and #4826), TestCodexPoolSaysTheClientsVersion (#4807, full-package
  run only) and TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27).
- go vet for darwin, GOOS=linux -tags nogui and GOOS=windows on
  plugin, provider, gui, cli: ok. gofmt -l: clean.
- agent-model-load.test.cjs: chromium and webkit, 1000 and 440px, en,
  zh, zh-TW, ja, de: 40 of 40. Screenshots looked at in both engines.
- Whole GUI suite: not finished. Three whole-suite runs (WSL, mote) were
  cut at 1,266 and 642 tests by load and a session restart; their reds
  were all in provider-editor, Settings and Usage files. Then the files
  a new API call or the Agents row can reach (183: every agent-*, every
  model/picker/speed/load file, the five strict-fixture files, every
  file using .field, ag-open, renderAgents or /api/drift, i18n-keys),
  split mote/WSL, both engines: reds only in model-chip-steady (#4388),
  mobile-web (#3952), model-copy-id (#4621), balance-fix (#4683),
  allow-ips (#4357, output added), agent-update-all (#2988), account-eye
  (#4244), models-works-only, models-select-all-api, model-suffix-own,
  provider-detect-models, provider-models-rest, remote-magpie,
  routing-rule-models, usage-speed (WebKit on WSL, #4218/#4471). Each red
  file was run alone at this head and at origin/main b4073c06a: the same
  reds both sides, except agent-update-all's fixed 200ms wait (#2988),
  red once at the head. codex-subagent-model, red under load, passed
  alone.

Broken by hand, each red, then restored: the picker row's badge line,
fieldBtn's badge line (red at the renderAgents redraw check), the
readLoads painter, and ModelLoads' listed-id loop (TestModelLoadsByRef).

Not tried: a real plugin reporting load in the live app; the tray panel
layout; WebKitGTK in the real Linux app and the Windows app.
2026-11-03 05:21:47 +08:00

67 lines
2.1 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// /api/model-loads says how busy each plugin model is, keyed by the ref
// an agent's model picker gives the row, lowercased, so the Agents page
// shows the provider editor's figure by the model (axelburks on Discord).
// A model the user lists by another id is under that id; a stale load,
// and a model never reported, say nothing.
func TestModelLoadsByRef(t *testing.T) {
sandboxHome(t)
plugin.ForgetLoads()
t.Cleanup(plugin.ForgetLoads)
s := settings.Load()
s.ModelIDs = map[string]string{"acme/Fast": "big", "acme/grok": "grok-4.7@256k"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
now := time.Now()
plugin.NoteLoadsForTest("acme", map[string]float64{"big": 87.4, "grok-4.7": 60, "GLM-5": 130}, now)
plugin.NoteLoadsForTest("acme", map[string]float64{"old": 40}, now.Add(-plugin.LoadFresh-time.Minute))
mux := http.NewServeMux()
speedsAPI(mux)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/model-loads", nil))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
var got map[string]float64
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
for ref, want := range map[string]float64{"acme/fast": 87.4, "acme/grok": 60, "acme/glm-5": 130} {
if v, ok := got[ref]; !ok || v != want {
t.Errorf("%s = %v (%v); want %v", ref, v, ok, want)
}
}
for _, ref := range []string{"acme/old", "acme/quiet"} {
if v, ok := got[ref]; ok {
t.Errorf("%s says %v; want nothing", ref, v)
}
}
}
// With no plugin reporting, the answer is an empty object, not null: the
// page reads it as a map.
func TestModelLoadsNoneReported(t *testing.T) {
sandboxHome(t)
plugin.ForgetLoads()
mux := http.NewServeMux()
speedsAPI(mux)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/model-loads", nil))
if b := w.Body.String(); w.Code != 200 || (b != "{}" && b != "{}\n") {
t.Fatalf("%d %q; want {}", w.Code, b)
}
}