Files
magpie/internal/gui/library_hub_note_test.go
T
yetone 454f9d023a library: the Tools on demand row names the agents the gateway gives every tool
The row under the MCP router said "Claude Code, Claude Desktop, Codex and
Pi find tools themselves and are given every one", but hubOnDemand gives
every tool to Droid and omp too (hubSearchesItself). The words now come
from the code: gateway.HubFindsItself (the ordered list hubSearchesItself
is built from) and gateway.HubOnDemandOver reach the library's view as
hubFindsItself (each agent by its name, its id when none is found) and
hubOnDemandOver, and the row joins them with the language's list
separator. Two keys replace the old sentence in zh, zh-TW, ja and de; the
CJK sentences follow each other with no space.

Tests:
- TestHubFindsItselfIsWhoGetsEveryTool: every named agent (and its WSL
  id) is given every tool, every agent given every tool is named, and
  other agents get theirs on demand.
- TestHubFindsItselfInTheView: /api/library carries the gateway's list
  and threshold, each agent by its name.
- library-mcp-router-tools.test.cjs: the row's text is built from the
  view's list and count (fixture: 12, six agents), names each one, never
  says 10, and has no "。 ".

Run (wsl-run, magpie-desktop; mote was at load 46-49 of 18, mica's disk
full), at this tree on 35a03fee0:
- gofmt -l internal/gateway internal/library internal/gui: clean
- go vet -tags nogui, and GOOS=windows go vet -tags nogui, of
  ./internal/gateway ./internal/library ./internal/gui: ok
- go test -count=1 ./internal/gateway -run 'Hub|MCP': ok
- go test -count=1 ./internal/gui ./internal/testenv: ok (134s)
- go test -count=1 ./internal/library: ok (183s)
- node --test i18n-keys, library-mcp-router-tools,
  library-mcp-router-note, library-mcp-hub (chromium and webkit; 1100
  and 420 wide; en, zh, zh-TW, ja, de): 70 of 70 ok

Broken by hand (a copy of this tree): dropping omp from the list while
omp still gets every tool, TestHubFindsItselfIsWhoGetsEveryTool fails
"omp is given every tool but not named"; putting the old hardcoded "10"
and four names back into the row, library-mcp-router-tools fails in
every language. That run also showed "。 " in zh, fixed here.

Not run: the full go test ./... and the whole GUI suite; the real app.
2026-11-18 22:39:19 +08:00

114 lines
4.1 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
)
// The name the Library gives this computer's MCP router (packinggoodsllc_07668 on Discord) is saved
// to this computer's settings and comes back in the page's view, with
// this computer's name for the field's placeholder; a save from the
// Settings page, which doesn't send it, leaves it as it is.
func TestHubNoteSaved(t *testing.T) {
sandboxHome(t)
h := Handler(nil, nil)
post := func(path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
return rec
}
rec := post("/api/library/servers/hub-note", `{"note":" Los Angeles "}`)
if rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
var v struct{ HubNote, HubName string }
if err := json.Unmarshal(rec.Body.Bytes(), &v); err != nil {
t.Fatal(err)
}
if v.HubNote != "Los Angeles" || settings.Load().RouterNote != "Los Angeles" {
t.Fatalf("view %+v, saved %q", v, settings.Load().RouterNote)
}
if rec := post("/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK {
t.Fatalf("settings: %d %s", rec.Code, rec.Body)
}
if got := settings.Load().RouterNote; got != "Los Angeles" {
t.Fatalf("the Settings page's save left %q", got)
}
if rec := post("/api/library/servers/hub-note", `{"note":""}`); rec.Code != http.StatusOK || settings.Load().RouterNote != "" {
t.Fatalf("cleared: %d %q", rec.Code, settings.Load().RouterNote)
}
}
// The router's switch for giving every agent its tools whole, not on
// demand (ArvinCharl on X), is this computer's setting, comes back in the
// page's view, and a save from the Settings page leaves it as it is.
func TestHubAllToolsSaved(t *testing.T) {
sandboxHome(t)
h := Handler(nil, nil)
post := func(path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
return rec
}
rec := post("/api/library/servers/hub-tools", `{"on":true}`)
if rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
var v struct{ HubAllTools bool }
if err := json.Unmarshal(rec.Body.Bytes(), &v); err != nil {
t.Fatal(err)
}
if !v.HubAllTools || !settings.Load().RouterAllTools {
t.Fatalf("view %+v, saved %v", v, settings.Load().RouterAllTools)
}
if rec := post("/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK {
t.Fatalf("settings: %d %s", rec.Code, rec.Body)
}
if !settings.Load().RouterAllTools {
t.Fatal("the Settings page's save turned it off")
}
if rec := post("/api/library/servers/hub-tools", `{"on":false}`); rec.Code != http.StatusOK || settings.Load().RouterAllTools {
t.Fatalf("off: %d %v", rec.Code, settings.Load().RouterAllTools)
}
}
// The Tools on demand row says how many tools an agent is given whole and
// which agents search their tools themselves from the page's view, which
// carries the gateway's own (gateway.HubOnDemandOver,
// gateway.HubFindsItself), each agent by its name: its words named four of
// the six (11-18).
func TestHubFindsItselfInTheView(t *testing.T) {
sandboxHome(t)
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("GET", "/api/library", nil))
if rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
var v struct {
HubFindsItself []string
HubOnDemandOver int
}
if err := json.Unmarshal(rec.Body.Bytes(), &v); err != nil {
t.Fatal(err)
}
ids := gateway.HubFindsItself()
if v.HubOnDemandOver != gateway.HubOnDemandOver || len(v.HubFindsItself) != len(ids) {
t.Fatalf("view says %d, %q; the gateway %d, %q", v.HubOnDemandOver, v.HubFindsItself, gateway.HubOnDemandOver, ids)
}
for i, id := range ids {
if a, err := agent.Find(id); err == nil && a.ID == id && v.HubFindsItself[i] != a.Name {
t.Fatalf("%s is said as %q, not %q", id, v.HubFindsItself[i], a.Name)
}
if v.HubFindsItself[i] == id && id != "omp" {
t.Fatalf("%s is said by its id", id)
}
}
}