Files
magpie/internal/library/desktop3p_test.go
T
yetone 3a3d161fa0 agent: Claude Desktop uninstalled on Windows is no longer listed (alien_jambob on X)
alien_jambob (Windows 11, 0.1.3603) uninstalled Claude Desktop and magpie
still listed it under 未设置. The uninstall leaves %APPDATA%\Claude behind
with its claude_desktop_config.json (their Everything search shows it),
and Desktop counted as here whenever any of its folders was.

- On Windows Desktop is listed only while its program is installed
  (desktopInstalled): Squirrel's %LOCALAPPDATA%\AnthropicClaude\claude.exe,
  the MSIX's package folder (desktopdir.Installed, as before), or its
  package in the AppX registry (Claude_… or the Store's
  AnthropicPBC.Claude_…, main package only). A Desktop still on magpie
  stays listed, so it can be turned off, as Alma's fix (1b2ad557f) keeps.
- The library's own Claude Desktop entry (apps(), the one that gets MCP
  servers and skills) had the same folder-only check: it is now
  agent.DesktopApp, with the same rule on Windows. Something magpie gave
  it is still listed by held() to be taken back.
- macOS and Linux keep folder detection (unchanged).

The path the row shows, ~\AppData\Local\Claude\claude_desktop_config.json,
is right: it is the file magpie writes deploymentMode into on Windows
(desktopdir's Mode, as CC Switch does) when Desktop is turned on, so it
doesn't exist on a Desktop never connected. Left as is.

Run: go test -run 'Desktop|MSIX' ./internal/agent ./internal/library on
Windows (win-run, xiaomi-gpu): library ok; agent's only reds are six
that fail the same on origin/main 065ca02c2 on that box, all filed
(#4115 CarriesTheLanguage/OpenLody/DesktopExec, #5068 and #5476 sealed
key, #5069 DesktopHandler). Whole internal/agent and internal/library on
mote (darwin): ok. go vet for windows, linux (nogui) and darwin; gofmt.
Whole internal/agent and internal/library on Linux (wsl-run): library
ok, agent red only on TestGrokInstalled (WSL binfmt interop, #3548).
After rebasing on 40cd70d54: vet for three OSes and -run Desktop|MSIX on
Linux, ok.
e2e on Windows: magpie ls in a sandbox USERPROFILE/APPDATA/LOCALAPPDATA
with only Roaming\Claude\claude_desktop_config.json: origin/main lists
Claude Desktop, this change doesn't; with AnthropicClaude\claude.exe
added both list it.

Broken by hand: with the Windows branch taken out (folders count again),
TestClaudeDesktopUninstalledLeftover goes red on "only the leftover",
"a resource package alone" and "another app's package".

Not tried: a real MSIX Desktop uninstalled (the box has the Squirrel
install, which is the user's own); macOS's leftover ~/Library/Application
Support/Claude after Claude.app is trashed is not changed here
(#5979).
2026-11-10 22:56:39 +08:00

150 lines
5.4 KiB
Go

package library
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
)
// desktopFiles are Claude Desktop's two files in the sandbox: the one it
// reads signed in with Anthropic, and Claude-3p's, read in its 3p mode.
func desktopFiles(t *testing.T, h string) (string, string) {
t.Helper()
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
d, _ := os.UserConfigDir()
p := filepath.Join(d, "Claude", "claude_desktop_config.json")
p3 := filepath.Join(d, "Claude-3p", "claude_desktop_config.json")
if runtime.GOOS == "windows" {
p3 = filepath.Join(h, "AppData", "Local", "Claude-3p", "claude_desktop_config.json")
// on Windows Desktop is here when its program is
// (agent.DesktopApp), not its folders
write(t, filepath.Join(h, "AppData", "Local", "AnthropicClaude", "claude.exe"), "")
}
if !strings.HasPrefix(p, h) {
t.Skip("config dir outside the sandbox: " + p)
}
return p, p3
}
func desktopServers(t *testing.T, p string) map[string]map[string]any {
t.Helper()
var doc struct {
MCPServers map[string]map[string]any `json:"mcpServers"`
}
if s := read(t, p); s != "" {
if err := json.Unmarshal([]byte(s), &doc); err != nil {
t.Fatalf("%s: %v", p, err)
}
}
return doc.MCPServers
}
// #297: Claude Desktop in its 3p mode (on magpie's gateway) reads its MCP
// servers from Claude-3p/claude_desktop_config.json, so a server the
// library gives it goes there as well as into Claude/'s, and goes from
// both; the user's own servers and keys in either stay.
func TestClaudeDesktop3pMCP(t *testing.T) {
h := sandbox(t)
p, p3 := desktopFiles(t, h)
write(t, p, `{"deploymentMode": "3p", "mcpServers": {"mine": {"command": "x"}}}`)
write(t, p3, `{"deploymentMode": "3p", "preferences": {"a": true}, "mcpServers": {"mine3p": {"command": "y"}}}`)
tg := targetByID("claude-desktop")
if tg == nil || tg.MCP == nil || tg.MCP.Path != p || !slices.Equal(tg.MCP.Also, []string{p3}) {
t.Fatalf("claude-desktop target: %+v", tg.MCP)
}
// the user's own server in Claude-3p is one to bring in
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
if !slices.ContainsFunc(v.FoundServers, func(f Found) bool { return f.Server.Name == "mine3p" }) {
t.Errorf("mine3p not found: %+v", v.FoundServers)
}
ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "fs"}, Agents: []string{"claude-desktop"}}))
for _, f := range []string{p, p3} {
if fs := desktopServers(t, f)["fs"]; fs["command"] != "npx" {
t.Errorf("%s: fs is %v", f, fs)
}
}
if s := desktopServers(t, p3); s["mine3p"] == nil || s["mine"] != nil {
t.Errorf("Claude-3p's servers: %v", s)
}
if s := read(t, p3); !strings.Contains(s, `"deploymentMode": "3p"`) || !strings.Contains(s, `"preferences"`) {
t.Errorf("Claude-3p's file: %s", s)
}
// a key the user added in Claude-3p's entry stays when it is written again
var doc map[string]any
json.Unmarshal([]byte(read(t, p3)), &doc)
doc["mcpServers"].(map[string]any)["fs"].(map[string]any)["note"] = "mine"
b, _ := json.Marshal(doc)
write(t, p3, string(b))
ok(t)(SaveServer("fs", Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "fs2"}, Agents: []string{"claude-desktop"}}))
if fs := desktopServers(t, p3)["fs"]; fs["note"] != "mine" || !slices.Equal(strList(fs["args"]), []string{"-y", "fs2"}) {
t.Errorf("fs in Claude-3p: %v", fs)
}
// taken out of the library, it leaves neither file
ok(t)(RemoveServer("fs"))
for _, f := range []string{p, p3} {
if s := desktopServers(t, f); s["fs"] != nil {
t.Errorf("%s still has fs: %v", f, s)
}
}
if desktopServers(t, p)["mine"] == nil || desktopServers(t, p3)["mine3p"] == nil {
t.Error("the user's own servers went")
}
}
// A server magpie gave Desktop before, only in Claude/'s file, reaches
// Claude-3p's on the next sync; one there only goes when it is removed.
func TestClaudeDesktop3pSync(t *testing.T) {
h := sandbox(t)
p, p3 := desktopFiles(t, h)
write(t, p, `{}`)
ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Agents: []string{"claude-desktop"}}))
if _, err := os.Stat(filepath.Dir(p3)); err == nil {
t.Fatal("Claude-3p made for a Desktop never in 3p mode")
}
// Desktop switched to 3p since (magpie's gateway made the folder)
write(t, p3, `{"deploymentMode": "3p"}`)
ok(t)(Sync())
if fs := desktopServers(t, p3)["fs"]; fs["command"] != "npx" {
t.Fatalf("fs not synced into Claude-3p: %s", read(t, p3))
}
// gone from Claude/'s file by hand: removing it still clears Claude-3p's
write(t, p, `{}`)
ok(t)(RemoveServer("fs"))
if s := desktopServers(t, p3); s["fs"] != nil {
t.Errorf("Claude-3p still has fs: %s", read(t, p3))
}
}
// Desktop only ever run in 3p mode has no Claude folder: its servers go
// into Claude-3p's file alone, and no Claude folder is made.
func TestClaudeDesktop3pOnly(t *testing.T) {
h := sandbox(t)
p, p3 := desktopFiles(t, h)
write(t, p3, `{"deploymentMode": "3p"}`)
tg := targetByID("claude-desktop")
if tg == nil || tg.MCP == nil || tg.MCP.Path != p3 || len(tg.MCP.Also) != 0 {
t.Fatalf("claude-desktop target: %+v", tg)
}
ok(t)(SaveServer("", Server{Name: "fs", Transport: "stdio", Command: "npx", Agents: []string{"claude-desktop"}}))
if fs := desktopServers(t, p3)["fs"]; fs["command"] != "npx" {
t.Errorf("Claude-3p: %s", read(t, p3))
}
if _, err := os.Stat(filepath.Dir(p)); err == nil {
t.Error("Claude folder made")
}
}