Files
magpie/internal/library/main_test.go
T
yetone de74cb53f7 mcp router: each caller gets its own Magpie Browser session
The router keeps one backend per Library server for every agent, so all
agents behind it shared one `magpie mcp browser`, one browser session and
so one tab (packinggoodsllc_07668 on Discord: two agents through a remote
router fought over one tab).

- serveMCP gives a client that initializes an Mcp-Session-Id (nothing is
  kept or checked by it).
- hubSession names each tools/call's caller: _meta magpie/session from a
  router in front, else hub-<agent>-<Mcp-Session-Id>.
- The router passes it in _meta magpie/session to `magpie mcp browser` and
  to another magpie's router only (keepsSession); the bridge sends it as
  that call's X-Magpie-Browser-Session (callerSession). A call naming none
  goes as the bridge's own session, as before.

Run (mote-run): go test ./internal/browsermcp; ./internal/gateway -run
'MCP|Hub'; ./internal/library -run 'TestHubTellsMagpieBrowser|TestKeepsSession|TestHubGivesMagpieImage'. ok.
Broken by hand, each red: no meta from the library (library test), no
Mcp-Session-Id from serveMCP and ignoring _meta in hubSession (gateway test).
Not tried: a real remote router chain between two machines.
2026-11-01 16:10:16 +08:00

69 lines
1.9 KiB
Go

package library
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// The tests run in a home of their own, never the user's real files, with
// a temp dir of their own: what they leave in it is theirs, and a skill's
// download left there fails the run (#2321).
func TestMain(m *testing.M) {
// started as magpie's own image or browser server (Args mcp image,
// mcp browser): the fake server, for the hub to take it for that one
if len(os.Args) >= 3 && os.Args[1] == "mcp" && (os.Args[2] == "image" || os.Args[2] == "browser") {
os.Args = []string{os.Args[0], "-test.run=^TestFakeMCPServer$"}
}
if os.Getenv(testenv.Marker) != "" {
testenv.Main(m) // started again by a test: in its temp dir already
}
tmp, err := os.MkdirTemp("", "magpie-library-test-")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for _, k := range []string{"TMPDIR", "TMP", "TEMP"} { // TMP, TEMP: Windows'
os.Setenv(k, tmp)
}
// never the machine's own processes (mcp_reap.go)
ending = func([]*Server) {}
// never the registry (mcp_npx.go)
npxInstall = nil
// Codex given its servers by their commands, as the tests expect;
// mcp_run_test.go turns it on
runOn = false
code := testenv.Run(m)
forgetProbes()
if left := leftDownloads(tmp); len(left) > 0 && code == 0 {
fmt.Fprintf(os.Stderr, "library: the tests left %d skill downloads in the temp dir: %s\n", len(left), strings.Join(left, ", "))
code = 1
}
os.RemoveAll(tmp)
os.Exit(code)
}
// forgetProbes ends every cached probe, as its expiry would.
func forgetProbes() {
probes.Lock()
m := probes.m
probes.m = map[string]*probeEntry{}
probes.Unlock()
for _, e := range m {
e.timer.Stop()
os.RemoveAll(e.tmp)
}
}
func leftDownloads(dir string) []string {
got, _ := filepath.Glob(filepath.Join(dir, skillTemp+"*"))
for i, p := range got {
got[i] = filepath.Base(p)
}
return got
}