Files
magpie/internal/library/main_test.go
T
yetone a28744c325 gateway: memory is an MCP server every agent can mount
memory_search, memory_save and memory_forget at /mcp/magpie/memory,
served and guarded as the search server is (serveMCP): another computer
reaches it only while magpie is shared, with a gateway key. A save
through it is active at once, origin agent, kept everywhere, for the
project (the git repository the agent works in, by its remote) or for
the agent alone; without a folder or a client name it says it can't
know rather than guess.

- The Library's market offers it as magpie-memory, run as
  `magpie mcp memory` (browsermcp.RunMemory): stdio relayed to the
  gateway with the folder it was started in (X-Magpie-Cwd) and the
  client name from initialize (X-Magpie-Mcp-Client).
- Behind the MCP router the hub keeps it on this computer (hereOnly)
  and passes the caller's roots and client in _meta (wantsCaller), as
  it does for magpie's own server.
- OwnMCPPath lists it among magpie's own servers.
- Judged as its sibling servers are: a gateway key's MaxConcurrency
  doesn't hold it (a tool call of a turn already held), and a read-only
  key is refused (memory_save writes). The first full gateway run was
  red on TestKeySlotEveryRoute and TestReadOnlyKeyEveryRoute until the
  route was listed.

Run (mote-run):
- go test -tags nogui ./internal/gateway: ok (881.5s)
- go test ./internal/browsermcp ./internal/cli ./internal/memory: ok
- go test ./internal/library: ok (130.9s)
- go vet, GOOS darwin, linux, windows: ok; gofmt -l clean
Broken by hand, each red, then green restored:
- the hub not sending memory its caller (wantsCaller without isMemory):
  TestHubTellsMagpieMemoryItsCaller "roots asked false"
- the relay not setting X-Magpie-Cwd: TestMemoryBridgeSendsFolderAndClient
- the gateway ignoring X-Magpie-Cwd: TestMemoryMCPSharedAcrossAgents
  (a project fact kept everywhere)
Not tried: a real Claude Code or Codex mounting magpie-memory.
2026-11-10 03:25:44 +08:00

66 lines
1.8 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, browser or memory server (Args mcp
// image, mcp browser, mcp memory): 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[2] == "memory") {
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)
}
// 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
}