Files
magpie/internal/gateway/trace_caller_key_test.go
T
yetone 5fe4c48df9 gateway: a route names the gateway key it came with, by id and name
The Routing page is to show each gateway key's requests as it shows each
agent's (acezan on Discord). A route kept the key's id only for
sessionLatest, unexported, so neither the trace nor the routing history
said it. Route.CallerKey and CallerKeyName are now in its JSON: the id and
the name the key had, never the secret. A request with no key names none,
and so does every route the history kept before this.

The trace is told to this computer alone (/v1/magpie/trace is loopback
only, /api/gateway/trace the GUI's), and /v1/magpie/route sends
sessionRouteOf's projection, which carries neither field.

Run (wsl-run, magpie-desktop; mote's disk was full, its link failed with
errno 28): go test -count=1 -run
'TestTraceNamesCallerKey|TestSessionRoute|TestCallerUsage|TestRejectedRequest'
./internal/gateway ok. Broken by hand: with trace.go, unrouted.go and
session_route.go back at c2b8814e7, TestTraceNamesCallerKey fails ("the
key's route doesn't name it").
2026-11-05 14:47:18 +08:00

74 lines
2.1 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The Routing page groups requests by the gateway key they came with
// (acezan on Discord: each key's use, as each agent's): every route names
// its key by id and name, never by its secret, and one sent with no key
// names none.
func TestTraceNamesCallerKey(t *testing.T) {
fresh(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "upstream-secret", Chat: up.URL + "/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
keys, secrets := newCaller(t, "Laptop")
s := New()
h := s.Handler()
send := func(remote, secret string) {
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(chatReq))
r.RemoteAddr = remote
if secret != "" {
r.Header.Set("Authorization", "Bearer "+secret)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != 200 {
t.Fatal(remote, w.Code, w.Body.String())
}
}
send("192.168.1.9:5000", secrets[0])
send("127.0.0.1:5000", "")
b, err := json.Marshal(s.Trace(context.Background(), 0, 0))
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(b), secrets[0]) {
t.Fatal("the key's secret is in the trace")
}
var got struct {
Routes []map[string]any `json:"routes"`
}
if err := json.Unmarshal(b, &got); err != nil {
t.Fatal(err)
}
if len(got.Routes) != 2 {
t.Fatalf("routes: %s", b)
}
by := map[string]map[string]any{}
for _, r := range got.Routes {
id, _ := r["callerKey"].(string)
by[id] = r
}
if r := by[keys[0].ID]; r == nil || r["callerKeyName"] != "Laptop" {
t.Fatalf("the key's route doesn't name it (%s): %s", keys[0].ID, b)
}
if r := by[""]; r == nil || r["callerKeyName"] != nil {
t.Fatalf("the route sent with no key names one: %s", b)
}
}