Files
magpie/internal/backup/local_server_test.go
T
yetone 4054251865 library: an MCP server can be kept to this computer, never synced
huhuy72219405 on X: "能不能把不同设备的mcp区分一下,有些mcp属于本地的,
同步之后其他设备不应该读取到". A server in Library > MCP now has a
"This computer only" switch in its editor (Server.Local; CLI
`magpie library mcp add ... --local` and `magpie library mcp local
<name> on|off`; the TUI keeps it on edit and marks the row).

- Collect leaves local servers out (carriedServers), so neither a sync
  nor a backup file takes one to another computer. Turning a shared
  server local takes it out of what is carried, so the other computers
  drop it at their next sync; the flag itself is never carried.
- Put (sync, join, Restore, Undo, backup file) keeps every local
  server, with its agents, and clears Local on what comes in.
- Another computer's server by a local server's name goes to
  Library.Away: given to no agent, not in the hub, carried on as it
  came, so a push from here never removes it from the computers that
  have it. settleAway (every change) promotes it once the name is gone
  here, and drops it once the name is shared here again.
- The hub serves local servers like any other (it runs here);
  subagents aren't carried, so one naming a local server keeps it.

Docs: docs/subsystems/library.md, docs/subsystems/sync-backup.md.

Run at f5dd4e142 + this change; after rebasing on d3898ca8d (gui/provider only, then 2a1ef15ba, routing), vet, the five new Go tests, mcp-local-server and i18n-keys again, ok:
- mote: gofmt -l clean; go vet of library/backup/davsync/cli/tui;
  GOOS=linux and GOOS=windows go vet -tags nogui of the same; go test
  of TestLocalServerNotCarried, TestPutKeepsLocalServer,
  TestPutSameNameAsLocal (library), TestRestoreKeepsLocalServer
  (backup), TestLocalServerThroughSync (davsync, two computers through
  the fake S3) ok.
- mote GUI, Chromium and WebKit: mcp-local-server.test.cjs (new; every
  language, 1100 and 390 wide, switch saves local true/false, row tag,
  no sideways scroll), i18n-keys, mcp-editor-cancel,
  mcp-startup-timeout, every library-mcp-*, mcp-oauth-app, mcp-signin*,
  library-items, library-servers-bulk, library-subagents,
  library-pick-remove, library-env-refs, library-zed-mcp,
  library-loading: all pass (126 of 126 in the last batch).
- Full Go suite, -tags nogui -timeout 45m, on the Linux box (wsl-run;
  mote had another session's full suite running): every package ok
  but internal/agent, whose TestInstalledVersion, TestGrokInstalled,
  TestOmpDetectedWithPiDir and TestZedAppPath fail the same at
  origin/main 9b4d0bda8 on that box; all four are #4033.
- Real CLI in a sandbox HOME on mote: mcp add --local, local on/off,
  `magpie library` shows "this computer only", library.json has
  "local": true; an unknown name and a bad value are refused.

Broken by hand: putServers not keeping local servers turned
TestPutKeepsLocalServer, TestPutSameNameAsLocal,
TestRestoreKeepsLocalServer and TestLocalServerThroughSync red;
carriedServers carrying local servers turned TestLocalServerNotCarried,
TestPutSameNameAsLocal, TestRestoreKeepsLocalServer and
TestLocalServerThroughSync red.

Not tried: a sync between two real computers; the real GUI app (the
Playwright run fakes the API); the full Go suite on macOS or Windows.
2026-10-30 02:31:08 +08:00

82 lines
2.4 KiB
Go

package backup
import (
"testing"
"github.com/yetone/magpie/internal/library"
)
func libServer(t *testing.T, name string) *library.Server {
t.Helper()
v, err := library.Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Servers {
if s.Name == name {
return s.Server
}
}
return nil
}
// A server kept to this computer (huhuy72219405 on X) is in no backup
// file, and restoring one — made before it was added, or on another
// computer with a server by its name — keeps it as it is here.
func TestRestoreKeepsLocalServer(t *testing.T) {
home(t)
gh := library.Server{Name: "gh", Transport: "stdio", Command: "gh-mcp", Agents: []string{}}
if _, err := library.SaveServer("", gh); err != nil {
t.Fatal(err)
}
// the other computer's db, by the name of this one's own
if _, err := library.SaveServer("", library.Server{Name: "db", Transport: "stdio", Command: "their-db", Agents: []string{}}); err != nil {
t.Fatal(err)
}
b, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
data, _ := Seal(b, "pw")
home(t) // this computer, with a db of its own
if _, err := library.SaveServer("", library.Server{Name: "db", Transport: "stdio", Command: "/opt/my-db", Agents: []string{}, Local: true}); err != nil {
t.Fatal(err)
}
if _, err := library.SaveServer("", library.Server{Name: "mine", Transport: "stdio", Command: "/opt/mine", Agents: []string{}, Local: true}); err != nil {
t.Fatal(err)
}
own, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
if len(own.Library.MCP) != 0 {
t.Fatalf("a backup made here carries the local servers: %+v", own.Library.MCP)
}
got, err := Open(data, "pw")
if err != nil {
t.Fatal(err)
}
if r, err := Restore(got, Parts{Library: true}); err != nil || !r.Library {
t.Fatalf("restore: %+v %v", r, err)
}
if s := libServer(t, "db"); s == nil || !s.Local || s.Command != "/opt/my-db" {
t.Fatalf("db after the restore: %+v", s)
}
if s := libServer(t, "mine"); s == nil || !s.Local {
t.Fatalf("mine after the restore: %+v", s)
}
if libServer(t, "gh") == nil {
t.Fatal("the backup's gh didn't come in")
}
// this computer's own backup, restored: still there
sealed, _ := Seal(own, "pw")
got, _ = Open(sealed, "pw")
if _, err := Restore(got, Parts{Library: true}); err != nil {
t.Fatal(err)
}
if s := libServer(t, "mine"); s == nil || !s.Local || libServer(t, "gh") != nil {
t.Fatalf("after this computer's own backup: mine %+v", s)
}
}