Files
magpie/internal/desktopiso/desktopiso_test.go
T
yetone 39569d5604 claude desktop: an optional Claude Magpie app beside your own Claude (#5671)
baranwang on X asked to keep Claude Desktop's own (1P, Anthropic) sign-in
while using magpie. Turning on "Separate app" in Claude Desktop's row on
the Agents page (or `magpie claude-desktop launcher add`) moves magpie's
3P wiring into ~/Library/Application Support/Claude-Magpie-3p and adds
~/Applications/Claude Magpie.app. That launcher opens Claude.app with
--user-data-dir on that folder, so it runs side by side with the user's
own Claude.app. Its icon is Claude's icon with a magpie badge. Off is the
default, and turning it off (`launcher remove`) moves the wiring back
and removes the app. It is macOS only: Desktop's packaged app strips
CLAUDE_USER_DATA_DIR, and on Windows it hardcodes LOCALAPPDATA\Claude-3p.
The folder ends in -3p because Desktop adds -3p to any other name.
Restarting the isolated app ends only its own process (pkill on its
--user-data-dir) and opens the launcher again.

Run at 1b25cda26 (this change on origin/main be9dbce02); rebased onto
fdabddeb4 (catalog only), then go build ./..., vet and the budget and
launcher tests again: ok. All
on mote:
- gofmt -l clean; go vet ./internal/...; GOOS=linux go vet -tags nogui
  and GOOS=windows go vet on agent, cli, desktopiso, desktopdir, gui.
- go test (whole packages) desktopiso, desktopdir, settings, settingsapi,
  proc: ok. -run Isolated|TestDesktopInstance|TestAddStartsDesktopOnTheFolder|
  TestScript|TestAddLeavesTheirs|ClaudeDesktop|Restart in agent;
  TestClaudeDesktopLauncher|TestEveryRouteHasACommandOrIsNamed|Help in cli;
  TestCoreScriptBudget|TestSettingsSaveKeepsWhatItDoesNotSend|Budget|Desktop
  in gui and gateway: ok.
- An earlier full `go test ./...` at fef23255f had four reds, all from
  this change and fixed here: the route without a command (now in
  notYet), settings_keep's DesktopIsolated, os/exec instead of proc, and
  the core script budget (paid by sharing the switch helper with the
  largest-window line and cutting comments; no raise). A second full run
  of the touched packages queued ~7h on mote's suite lock behind other
  sessions and was stopped; the targeted runs above replace it.
- GUI on mote, both engines: desktop-isolated (new; en, zh, zh-TW, ja,
  de; 1100 and 640; aria-checked, posts, status text, no scroll, no
  select, absent without `isolated`), desktop-longest, click-scroll,
  plain-names, i18n-keys: ok. page-width-steady, panel-fold and
  zero-priced-free are red, the same 33 tests at origin/main be9dbce02
  (diffed): #4541/#5013, #5796, #5262. i18n-keys' "a plugin's words are
  its own" is red on main too (#5332).
- Whole GUI suite on WSL at an older head: 400 of 22778 red across
  ~100 files of other pages, in line with #5559 (478 of 21666 on main
  there). Not rerun on mote.

Real use, on mote in a sandbox HOME with Claude.app 4.23118 linked in:
`claude-desktop magpie`, then `launcher add`, moved the wiring to
Claude-Magpie-3p (deploymentMode 3p, mcpServers and locale carried).
Opening Claude Magpie.app started Claude with
--user-data-dir=.../Claude-Magpie-3p; it wrote configLibrary and
model-catalog there (3P mode) and no -3p-3p folder. A second, 1P Claude
ran beside it on its own sandbox folder. Opening the launcher again
kept one instance; the pkill pattern matched only the isolated pid; both
quit on SIGTERM. `launcher remove` put the wiring back and removed the
app.

Broken by hand: a folder without -3p (Desktop made -3p-3p), the
CLAUDE_USER_DATA_DIR env route (ignored by the packaged app), os/exec in
the quit (TestNoCommandBypassesProc red).

Not tried: a screenshot of the running app (screencapture fails over
ssh); a model request through a gateway from the isolated app; Windows
and Linux (not offered there). Known: the Dock shows Claude's own icon
for the running isolated app, and Chrome pairing is off in a moved
userData folder.
2026-11-09 22:31:16 +08:00

209 lines
6.7 KiB
Go

package desktopiso
import (
"bytes"
"encoding/binary"
"errors"
"image"
"image/color"
"image/png"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
// fakeClaude is a Claude.app whose executable, Claude, writes the
// arguments it was started with to out, and whose icon is one blue PNG.
func fakeClaude(t *testing.T, root, out string) string {
t.Helper()
app := filepath.Join(root, "Claude.app")
for _, d := range []string{"MacOS", "Resources"} {
if err := os.MkdirAll(filepath.Join(app, "Contents", d), 0o755); err != nil {
t.Fatal(err)
}
}
info := `<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0"><dict>
<key>CFBundleExecutable</key>
<string>Claude</string>
<key>CFBundleIconFile</key>
<string>electron.icns</string>
<key>CFBundleIdentifier</key>
<string>com.anthropic.claudefordesktop</string>
<key>CFBundleShortVersionString</key>
<string>4.4212.0</string>
</dict></plist>
`
if err := os.WriteFile(filepath.Join(app, "Contents", "Info.plist"), []byte(info), 0o644); err != nil {
t.Fatal(err)
}
exe := "#!/bin/sh\nprintf '%s\\n' \"$@\" > '" + out + "'\n"
if err := os.WriteFile(filepath.Join(app, "Contents", "MacOS", "Claude"), []byte(exe), 0o755); err != nil {
t.Fatal(err)
}
img := image.NewNRGBA(image.Rect(0, 0, 256, 256))
for y := 0; y < 256; y++ {
for x := 0; x < 256; x++ {
img.SetNRGBA(x, y, color.NRGBA{20, 40, 200, 255})
}
}
var pb bytes.Buffer
png.Encode(&pb, img)
var icns bytes.Buffer
icns.WriteString("icns")
binary.Write(&icns, binary.BigEndian, uint32(16+pb.Len()))
icns.WriteString("ic08")
binary.Write(&icns, binary.BigEndian, uint32(8+pb.Len()))
icns.Write(pb.Bytes())
if err := os.WriteFile(filepath.Join(app, "Contents", "Resources", "electron.icns"), icns.Bytes(), 0o644); err != nil {
t.Fatal(err)
}
return app
}
func noRegister(t *testing.T) *[]string {
var got []string
old := register
register = func(p string) { got = append(got, p) }
t.Cleanup(func() { register = old })
return &got
}
// The app runs Claude's own executable on the folder, with the
// arguments it was opened with, and has Claude's icon with the badge.
func TestAddStartsDesktopOnTheFolder(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the app's executable is a shell script")
}
registered := noRegister(t)
home := t.TempDir()
out := filepath.Join(home, "args")
claudeApp := fakeClaude(t, filepath.Join(home, "Applications"), out)
dir := filepath.Join(home, "Library", "Application Support", "Claude-Magpie-3p")
p, err := Add(home, claudeApp, dir)
if err != nil {
t.Fatal(err)
}
if p != Path(home) || Here(home) != p || !Mine(p) {
t.Fatalf("app %q, here %q", p, Here(home))
}
if len(*registered) != 1 || (*registered)[0] != p {
t.Errorf("registered %v", *registered)
}
info, _ := os.ReadFile(filepath.Join(p, "Contents", "Info.plist"))
for _, want := range []string{"<string>" + BundleID + "</string>", "<string>claude-magpie</string>", "<string>Claude Magpie</string>", "<string>4.4212.0</string>"} {
if !bytes.Contains(info, []byte(want)) {
t.Errorf("Info.plist lacks %s:\n%s", want, info)
}
}
icns, err := os.ReadFile(filepath.Join(p, "Contents", "Resources", "claude-magpie.icns"))
if err != nil {
t.Fatal(err)
}
img, err := largestPNG(icns)
if err != nil || img.Bounds().Dx() != 1024 {
t.Fatalf("icon: %v", err)
}
// Claude's blue at the top left, the badge's white at the bottom right
if r, g, b, _ := img.At(100, 100).RGBA(); r>>8 > 40 || b>>8 < 180 || g>>8 > 60 {
t.Errorf("top left %v", img.At(100, 100))
}
if r, g, b, _ := img.At(736, 560).RGBA(); r>>8 < 200 || g>>8 < 200 || b>>8 < 200 {
t.Errorf("badge edge %v, want white", img.At(736, 560))
}
cmd := exec.Command(filepath.Join(p, "Contents", "MacOS", "claude-magpie"), "claude://open")
if b, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, b)
}
args, _ := os.ReadFile(out)
if want := "--user-data-dir=" + dir + "\nclaude://open\n"; string(args) != want {
t.Fatalf("Claude started with %q, want %q", args, want)
}
// made again: still one app, magpie's
if _, err := Add(home, claudeApp, dir); err != nil {
t.Fatal(err)
}
ents, _ := os.ReadDir(filepath.Dir(p))
if len(ents) != 2 {
t.Errorf("Applications holds %v", ents)
}
if got, err := Remove(home); err != nil || got != p {
t.Fatalf("remove: %q %v", got, err)
}
if _, err := os.Stat(p); !os.IsNotExist(err) {
t.Fatal("app still there")
}
if got, err := Remove(home); err != nil || got != "" {
t.Fatalf("remove again: %q %v", got, err)
}
}
// Claude.app moved since the app was made: it is looked for in the
// Applications folders.
func TestScriptFindsClaudeMoved(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell script")
}
home := t.TempDir()
t.Setenv("HOME", home)
out := filepath.Join(home, "args")
fakeClaude(t, filepath.Join(home, "Applications"), out)
dir := filepath.Join(home, "iso-3p")
s := filepath.Join(home, "run.sh")
os.WriteFile(s, []byte(Script(filepath.Join(home, "Gone", "Claude.app"), "Claude", dir)), 0o755)
if b, err := exec.Command(s).CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, b)
}
if args, _ := os.ReadFile(out); string(args) != "--user-data-dir="+dir+"\n" {
t.Fatalf("args %q", args)
}
os.RemoveAll(filepath.Join(home, "Applications"))
b, err := exec.Command(s).CombinedOutput()
if err == nil || !strings.Contains(string(b), "not installed") {
t.Fatalf("no Claude: %v %s", err, b)
}
}
// A folder name with a quote in it reaches Claude whole.
func TestScriptQuotes(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell script")
}
home := t.TempDir()
out := filepath.Join(home, "args")
app := fakeClaude(t, home, out)
dir := filepath.Join(home, "it's $HOME `x`")
s := filepath.Join(home, "run.sh")
os.WriteFile(s, []byte(Script(app, "Claude", dir)), 0o755)
if b, err := exec.Command(s).CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, b)
}
if args, _ := os.ReadFile(out); string(args) != "--user-data-dir="+dir+"\n" {
t.Fatalf("args %q", args)
}
}
// A Claude Magpie.app of the user's own is never written over or removed.
func TestAddLeavesTheirs(t *testing.T) {
noRegister(t)
home := t.TempDir()
theirs := Path(home)
os.MkdirAll(filepath.Join(theirs, "Contents"), 0o755)
os.WriteFile(filepath.Join(theirs, "Contents", "Info.plist"), []byte("<plist><dict><key>CFBundleIdentifier</key><string>me.mine</string></dict></plist>"), 0o644)
if _, err := Add(home, filepath.Join(home, "Claude.app"), "/x"); !errors.Is(err, ErrTheirs) {
t.Fatalf("add over theirs: %v", err)
}
if got, err := Remove(home); got != "" || err != nil {
t.Fatalf("remove theirs: %q %v", got, err)
}
if _, err := os.Stat(filepath.Join(theirs, "Contents", "Info.plist")); err != nil {
t.Fatal("theirs gone")
}
}