Files
magpie/internal/library/mcp_here_test.go
T
yetone 741d502d2a library: a synced MCP server can run its own command on this computer (jinpwng on X, #6426)
jinpwng runs one MCP server on a Mac and in WSL. Each needs its own
program path, and the only ways around it were two servers with two
names or keeping the server to one computer, which loses the sync of its
agents and note (#3836).

A command server now has an optional `shared` start. When it is set,
command, args and env are this computer's, and every reader (the agents'
files, projects, the router, the check) runs them with no change of its
own. Collect carries the shared start in their place and never the field,
so the other computers get exactly what they had. Put keeps this
computer's start, and what comes in for the server becomes the shared
start, so a note, agents or the other computer's own command change still
comes in. A server that comes in as a URL, one kept to this computer,
and the editor sending no shared start each end it.

- GUI: a saved synced command server's editor has "Use another command
  on this computer" under Command. With it on, the hint names what the
  other computers run, and "Use the synced command" goes back. Nothing
  else on the page moves; a new server and a local one have no button.
- CLI: `magpie library mcp here <name> <command args…>` and
  `… here <name> off`; the listing names a server that has one.
- TUI: an edit keeps the shared start.
- Docs: docs/subsystems/library.md, site docs (en, zh, ja).

Run (mote, at the tree pushed but before the rebase):
- go test ./internal/library/... ./internal/cli/... ./internal/tui/...
  ./internal/backup/... ./internal/gui/: ok
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean
- GUI, chromium and webkit: mcp-server-here (new: en, zh, zh-TW, ja, de at
  1100 and 390, the payload, no button on a local server, no sideways
  scroll), library-mcp-json, library-mcp-spaced-path, library-mcp-win-line,
  market-address, mcp-home-health, mcp-startup-timeout, market-have,
  mcp-editor-cancel, mcp-local-server, i18n-en-no-cjk, i18n-keys and the
  other library MCP files: 235 of 238. The reds:
  - i18n-keys "a plugin's words are its own": the same at origin/main
    3f6b1b0c9 (#6422, #6423, memory's "No folder").
  - market-have chromium "a server added by hand is marked added": a flake,
    red 1 of 3 at origin/main 3f6b1b0c9 and 1 of 3 here (#4620).

At the rebased head (on 04776ec6c, which touched library/client.js):
go test ./internal/gui/ ./internal/cli/ ok; GUI mcp-server-here,
mcp-local-server, library-group-steady, i18n-keys: 69 of 70, the red is
i18n-keys' #6422 above.

Broken by hand:
- keepStart doing nothing: TestServerHereThroughSync red (a sync took
  this computer's own command away).
- Collect carrying the server as is: red (carried /opt and "shared").

Not tried: the CLI's here command by hand (vet only; it calls the
tested ServerHere); the real app on a Mac with a real WSL computer syncing over
WebDAV; the test does the Mac-to-WSL round trip through Collect and Put.
A backup restored into an empty library gets the synced command only,
since a backup carries what sync carries.
2026-11-13 13:42:48 +08:00

98 lines
4.0 KiB
Go

package library
import (
"path/filepath"
"strings"
"testing"
)
// TestServerHereThroughSync: one server on a Mac and in WSL (#3836, #6426,
// jinpwng on X). This computer runs its own program; its note, agents and
// the other computer's command still come and go with a sync, and what
// this computer carries is the other computer's command, never its own.
func TestServerHereThroughSync(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "order-tools", Transport: "stdio", Command: "/Users/me/order-mcp", Args: []string{"--config", "mac.yaml"}, Agents: []string{"claude"}, Note: "orders"}))
mac := carry(t)
h := sandbox(t)
ok(t)(Put(&mac))
ok(t)(ServerHere("order-tools", []string{"/opt/order-mcp", "--config", "/etc/ops.yaml"}))
s := serverIn(t, "order-tools")
if s.Command != "/opt/order-mcp" || s.Shared == nil || s.Shared.Command != "/Users/me/order-mcp" {
t.Fatalf("after its own command here: %+v", s)
}
if c := read(t, filepath.Join(h, ".claude.json")); !strings.Contains(c, "/opt/order-mcp") || strings.Contains(c, "/Users/me") {
t.Fatalf("Claude Code was given %s", c)
}
b := carry(t)
if len(b.MCP) != 1 || b.MCP[0].Command != "/Users/me/order-mcp" || strings.Join(b.MCP[0].Args, " ") != "--config mac.yaml" || b.MCP[0].Shared != nil {
t.Fatalf("carried %s", mustJSON(t, b))
}
if j := mustJSON(t, b); strings.Contains(j, "/opt/") || strings.Contains(j, `"shared"`) {
t.Fatalf("this computer's own command was carried: %s", j)
}
if got, want := mustJSON(t, b), mustJSON(t, mac); got != want {
t.Fatalf("what this computer carries isn't what came in:\n%s\n%s", want, got)
}
// the Mac changes the note and its own path; this computer keeps its own
mac.MCP[0].Note, mac.MCP[0].Command = "orders, read only", "/Users/me/bin/order-mcp"
ok(t)(Put(&mac))
s = serverIn(t, "order-tools")
if s.Note != "orders, read only" || s.Command != "/opt/order-mcp" || s.Shared == nil || s.Shared.Command != "/Users/me/bin/order-mcp" {
t.Fatalf("after the Mac's change came in: %+v %+v", s, s.Shared)
}
// an edit here, as the editor sends it, keeps the synced command
n := *s
n.Agents = []string{"claude", "codex"}
ok(t)(SaveServer("order-tools", n))
if b := carry(t); b.MCP[0].Command != "/Users/me/bin/order-mcp" || len(b.MCP[0].Agents) != 2 {
t.Fatalf("carried after an edit here: %s", mustJSON(t, b))
}
if c := read(t, filepath.Join(h, ".codex/config.toml")); !strings.Contains(c, "/opt/order-mcp") {
t.Fatalf("Codex was given %s", c)
}
// back to the synced one
ok(t)(ServerHere("order-tools", nil))
if s := serverIn(t, "order-tools"); s.Command != "/Users/me/bin/order-mcp" || s.Shared != nil || strings.Join(s.Args, " ") != "--config mac.yaml" {
t.Fatalf("after going back to the synced command: %+v", s)
}
}
// TestServerHereEnds: the editor sending no synced command, the server
// kept to this computer, or one that comes in as a URL each end this
// computer's own command.
func TestServerHereEnds(t *testing.T) {
sandbox(t)
ok(t)(SaveServer("", Server{Name: "db", Transport: "stdio", Command: "their-db", Agents: []string{"claude"}}))
theirs := carry(t)
ok(t)(ServerHere("db", []string{"my-db"}))
n := *serverIn(t, "db")
n.Shared = nil
ok(t)(SaveServer("db", n))
if b := carry(t); b.MCP[0].Command != "my-db" {
t.Fatalf("the editor turned it off, yet carried %s", mustJSON(t, b))
}
ok(t)(ServerHere("db", []string{"mine"}))
ok(t)(ServerLocal("db", true))
if s := serverIn(t, "db"); s.Shared != nil || s.Command != "mine" {
t.Fatalf("kept to this computer: %+v", s)
}
ok(t)(ServerLocal("db", false))
ok(t)(ServerHere("db", []string{"mine"}))
url := theirs
url.MCP = []*Server{{Name: "db", Transport: "http", URL: "https://db.example/mcp", Agents: []string{"claude"}}}
ok(t)(Put(&url))
if s := serverIn(t, "db"); s.Shared != nil || s.URL != "https://db.example/mcp" || s.Command != "" {
t.Fatalf("after a URL came in: %+v", s)
}
if _, err := ServerHere("db", []string{"mine"}); err == nil {
t.Fatal("a URL server took a command of its own")
}
}