Files
magpie/internal/gateway/middleware_test.go
T
yetone cf1498f506 tests: the middleware tests start fresh, so a rest another test left on "a" no longer skips it (#3273)
TestMiddlewareOnProviderPerTry (e44b9aef3) set XDG_CONFIG_HOME and
XDG_CACHE_HOME itself instead of calling the package's fresh(t), so it
kept the process-wide rest map. In CI run 39773996813 (240f3eb4c,
macos-latest, Race shard 2 of 5) TestReplyNamesMember ran a few tests
before it, and its last subtest's 529 left provider "a" resting: group
G went straight to b and "a" was never asked (sent map["b":"asked"]).
The test's own 500 rests "a" the same way, so -count=2 failed alone.

Both middleware tests now call fresh(t), which clears rests, affinity,
routing memory and the rest as the package's other tests do.
TestMiddlewareThroughGateway had the same hand-made setup (provider
"fake", used by 71 other tests). The upstream handlers' writes to
`sent` are under a mutex now; the race detector hadn't reported them.

The class: every Test that saves a group and neither calls fresh(t),
noRests or a helper that does was listed (11). The others have one
member, ids no other test rests, or reset in a helper two levels down
(jevved -> ruled -> fresh).

Reproduced by injection, at ef6f3749d (old test):
  go test -race -run 'TestReplyNamesMember$|TestMiddlewareOnProviderPerTry$' ./internal/gateway/
    FAIL: middleware_test.go:152: sent map["b":"asked"] (twice)
  go test -race -count=3 -run 'TestMiddlewareOnProviderPerTry$' -> runs 2 and 3 FAIL
With this change, at ef6f3749d + this diff:
  the same pair: ok
  go test -race -count=20 -shuffle=on -run 'TestMiddleware|TestReplyNamesMember$' ./internal/gateway/: ok (202s)
  gofmt -l clean; go vet ./internal/gateway/ for darwin, linux (-tags nogui), windows: ok
  go test -race -timeout 45m ./internal/gateway/ (at ef6f3749d + this diff): ok (2287s)
  go test -timeout 45m ./... at d9a54a90c (this diff rebased on 21f7f9df2),
    no other full suite on the machine: exit 0, 69 packages ok, no FAIL
    (internal/gateway ok 853s); log kept whole and grepped for FAIL/panic
  Pushed rebased on 3a213e1dc, which changes routing.css and a GUI test
  only (no Go file) since d9a54a90c's run.

Not tried: the shuffle seed of the -count=20 run wasn't recorded (no -v).
No other package was changed.
2026-10-22 21:45:17 +08:00

164 lines
6.2 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// A gateway middleware sees an agent's request before routing and its
// reply's events in the agent's own API, and can turn a request away.
func TestMiddlewareThroughGateway(t *testing.T) {
const key = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
var got []byte
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, _ = io.ReadAll(r.Body)
chunk := func(s string) string {
b, _ := json.Marshal(map[string]any{"id": "c1", "model": "m1", "choices": []any{map[string]any{"index": 0, "delta": map[string]any{"content": s}}}})
return "data: " + string(b)
}
w.Header().Set("Content-Type", "text/event-stream")
// the reply says the key back, which redaction gave it as a placeholder
var sent struct {
Messages []struct{ Content any } `json:"messages"`
}
json.Unmarshal(got, &sent)
last, _ := json.Marshal(sent.Messages[len(sent.Messages)-1].Content)
io.WriteString(w, sse(chunk("hello "), chunk(string(last)), `data: {"id":"c1","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`, "data: [DONE]"))
}))
defer up.Close()
fresh(t) // nothing another test left on "fake" (#3273)
// the plugins' list written below reads as changed, and what that
// tells the plugins' hooks ends with the test
t.Cleanup(plugin.Settle)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
t.Fatal(err)
}
mw := filepath.Join(t.TempDir(), "alias.middleware.js")
os.WriteFile(mw, []byte(`
export function onRequest(body, ctx) {
if (body.model === "blocked") return ctx.reject(402, "no budget left");
ctx.state.asked = body.model;
if (body.model === "fast") body.model = "fake/m1";
return body;
}
export const events = ["content_block_delta"];
export function onEvent(ev, ctx) {
if (ev.delta && ev.delta.type === "text_delta") ev.delta.text = ev.delta.text.toUpperCase() + "[" + ctx.protocol + "]";
return ev;
}`), 0o644)
b, _ := json.Marshal(plugin.List{Plugins: []plugin.Entry{{Spec: mw}}})
os.MkdirAll(settings.Dir(), 0o755)
os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644)
middleware.Reload()
defer middleware.Reload()
code, body := post(t, "/v1/messages", `{"model":"fast","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"key `+key+`"}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
if strings.Contains(string(got), key) {
t.Fatalf("the middleware's body went out unredacted: %s", got)
}
var text strings.Builder
for _, e := range events(body) {
if d, ok := e["delta"].(map[string]any); ok {
s, _ := d["text"].(string)
text.WriteString(s)
}
}
// the middleware saw the key as the agent will, not the placeholder
if want := "HELLO [anthropic]" + strings.ToUpper(`"key `+key+`"`) + "[anthropic]"; text.String() != want {
t.Fatalf("text %q, want %q", text.String(), want)
}
code, body = post(t, "/v1/messages", `{"model":"blocked","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`)
if code != 402 || !strings.Contains(body, "no budget left") || !strings.Contains(body, `"type":"error"`) {
t.Fatalf("reject: %d %s", code, body)
}
st := middleware.States()[mw]
if st.Failures != 0 || st.Calls < 4 {
t.Fatalf("state %+v", st)
}
}
// onProvider changes the body of one try only: a group's first member is
// sent what the middleware made of it for that member, and the member
// tried next gets the body onRequest left, not the first try's.
func TestMiddlewareOnProviderPerTry(t *testing.T) {
// no rest, affinity or turn another test left on "a" sends the group
// past it (#3273: TestReplyNamesMember's 529 left "a" resting)
fresh(t)
var mu sync.Mutex
sent := map[string]string{}
member := func(id string, code int) {
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var b struct {
System string `json:"system"`
}
raw, _ := io.ReadAll(r.Body)
json.Unmarshal(raw, &b)
mu.Lock()
sent[id] = b.System
mu.Unlock()
if code != 200 {
http.Error(w, `{"type":"error","error":{"type":"api_error","message":"down"}}`, code)
return
}
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"id":"m","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`)
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k", Models: []string{"m1"}, Anthropic: up.URL}); err != nil {
t.Fatal(err)
}
}
t.Cleanup(plugin.Settle)
member("a", 500)
member("b", 200)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m1", "b/m1"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
mw := filepath.Join(t.TempDir(), "per-try.middleware.js")
os.WriteFile(mw, []byte(`
export function onRequest(body) { body.system = "asked"; return body; }
export function onProvider(body, ctx) {
const p = ctx.provider;
if (p.id === "a") body.system += " for " + [p.id, p.name, p.model, p.account || "key"].join("/");
return body;
}`), 0o644)
b, _ := json.Marshal(plugin.List{Plugins: []plugin.Entry{{Spec: mw}}})
os.MkdirAll(settings.Dir(), 0o755)
os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644)
middleware.Reload()
defer middleware.Reload()
code, body := post(t, "/v1/messages", `{"model":"group/g","max_tokens":10,"system":"agent's","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
mu.Lock()
defer mu.Unlock()
if sent["a"] != "asked for a/A/m1/key" || sent["b"] != "asked" {
t.Fatalf("sent %q", sent)
}
if st := middleware.States()[mw]; st.Error != "" || st.Last != "" || !slices.Contains(st.Hooks, "onProvider") {
t.Fatalf("state %+v", st)
}
}