Files
magpie/internal/gui/route_call_test.go
T
yetone af601541ef routing: a request opens to its request and response (Fate on Discord)
Fate asked on Discord to see what a single request on the Routing page sent
and got back, such as a Codex prediction. The bodies were captured already,
in the Gateway page's 40 recent calls and the opt-in request archive, but
nothing led there from a Routing request.

- gateway: a Call carries the Routing request it was (Call.Route = the
  trace's Route.ID), set where serve, the Codex backend and a refused title
  record it. Server.RouteCall finds it in the ring. A route id is not a
  call id. A route names its archive copy (Route.Archive =
  call.archiveName()), set only when the archive was on, so it reaches the
  saved history too.
- gui: GET /api/gateway/call takes ?route= besides ?id=. It is the same
  handler, the same no-store, the same key guard and the same 404 once the
  ring has let the call go.
- Routing page: the story's head has "Request and response". It fetches
  only when pressed, and shows the masked bodies through callBodyPanel (an
  SSE reply reads as its events). A request the ring no longer has shows
  the archive's copy through archivePanel when the route names one.
  Otherwise it says the last 40 are kept until magpie quits and how to turn
  the archive on. Nothing more is captured or kept than before.
- The head now wraps. At narrow widths its buttons ran past the story
  ("Back to live" was cut off at 440px).
- The two button words live in i18n.js: i18n-keys' "a plugin's words are
  its own" found them inside core strings.
- docs/subsystems/gui-shell.md says how a route reaches its bodies.

Tests:
- TestRoutingRequestOpensItsBodies (new): each of two routes gets its own
  bodies; route 0 and an unknown route get 404. Red without
  `call.Route = tr.ID` ("route …: 404").
- TestArchiveWholeBodies now checks that the route names the archive. Red
  without the t.Archive line (`route names archive ""`).
- routing-bodies.test.cjs (new), chromium and webkit at 440px, en, zh,
  zh-TW, ja and de, 10/10. It covers the ring's copy read only when asked,
  hiding, another request closing it, the archive's copy, the
  how-to-keep note, nothing past the story and no left border.
  - Red with the client asking ?id= and with the head's wrap removed.
- routing-request-metrics.test.cjs: with the head wrapped, its 480px
  picker clicks in WebKit landed on the footer. Playwright's WebKit places
  elements in the scrolled view hundreds of px lower than
  getBoundingClientRect, which elementFromPoint agrees with. The test now
  clicks where the page shows them. 5 runs of both engines, 20/20.

Run in a sandbox HOME at f1c91a99c plus this change:
- gofmt -l: clean. go vet -tags nogui: darwin, linux and windows ok.
- GUI: routing-*, panel-routing*, recent-call*, i18n-keys and
  header-relang in both engines: 1079 tests, 1079 pass (a first run had
  2 reds, both fixed above).
- go test ./internal/gui after the i18n move (TestPanelJoinsFullScreenSpaces
  skipped): ok.

Full suite: `go test -timeout 45m ./...` gave 65 ok, 1 FAIL. The FAIL is
internal/gui TestPanelJoinsFullScreenSpaces ("the panel shown doesn't take
the keyboard"), red 3/3 alone here and red the same on origin/main
1d227a7eb just fetched. It is filed as #2450.
2026-10-16 07:23:55 +08:00

88 lines
2.8 KiB
Go

package gui
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// A request on the Routing page opens to its own request and response
// (Fate on Discord): /api/gateway/call?route= finds the recent call by the
// route's id, each its own, and none for a route the ring doesn't have.
func TestRoutingRequestOpensItsBodies(t *testing.T) {
sandboxHome(t)
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
said := "reply-one"
if strings.Contains(string(b), "second") {
said = "reply-two"
}
io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"`+said+`"}}]}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "routed", Name: "Routed", Chat: up.URL, Key: "fixture"}); err != nil {
t.Fatal(err)
}
s := gateway.New()
was := served.Load()
served.Store(s)
t.Cleanup(func() { served.Store(was) })
mux := http.NewServeMux()
providerRoutes(mux, nil)
ask := func(content string) {
t.Helper()
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"routed/m","messages":[{"role":"user","content":"`+content+`"}]}`))
r.Header.Set("User-Agent", "codex")
s.Handler().ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatalf("gateway: %d %.200s", w.Code, w.Body)
}
}
ask("first question")
ask("second question")
routes := s.Trace(context.Background(), 0, 0).Routes
if len(routes) != 2 {
t.Fatalf("%d routes, want 2", len(routes))
}
get := func(route int64) (int, map[string]any) {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/gateway/call?route="+strconv.FormatInt(route, 10), nil))
var got map[string]any
json.Unmarshal(w.Body.Bytes(), &got)
return w.Code, got
}
for _, rt := range routes {
if !rt.Done {
t.Fatalf("route %d not done", rt.ID)
}
code, got := get(rt.ID)
if code != http.StatusOK {
t.Fatalf("route %d: %d", rt.ID, code)
}
req, _ := got["requestBody"].(string)
res, _ := got["responseBody"].(string)
second := strings.Contains(req, "second question")
if !second && !strings.Contains(req, "first question") {
t.Fatalf("route %d's request body: %.200s", rt.ID, req)
}
if want := map[bool]string{false: "reply-one", true: "reply-two"}[second]; !strings.Contains(res, want) {
t.Fatalf("route %d's response is not its own: %.200s", rt.ID, res)
}
}
for _, id := range []int64{0, routes[0].ID + routes[1].ID} {
if code, _ := get(id); code != http.StatusNotFound {
t.Fatalf("route %d the ring hasn't: %d", id, code)
}
}
}