Files
magpie/internal/ccswitchlink/link_linux_test.go
T
yetone 9314999a4a ccswitchlink: a ccswitch:// link that still reaches magpie goes to CC Switch, and so does the scheme (asombrado_a on X)
@asombrado_a (X, 2122995130097713493) has CC Switch and magpie both
installed, and a ccswitch://v1/import link opened magpie. magpie claims
magpie:// only since 03314b25a (v0.1.4455), but v0.1.3981 to v0.1.4454
claimed CC Switch's scheme too, and what they registered stays:
- macOS: Launch Services keeps the old Info.plist's ccswitch claim after
  the bundle is rewritten in place (seen on mote below);
- Windows: HKCU\Software\Classes\ccswitch running magpie.exe;
- Linux: magpie-ccswitch.desktop as the x-scheme-handler/ccswitch default.

The owner's call in 03314b25a stands: magpie doesn't read CC Switch's
links. A ccswitch:// link that reaches magpie anyway is now handed on:
- cli.run, before anything is read, moved or started (Windows, Linux:
  `magpie ccswitch://…`), and the Mac's URL event in gui.Run call
  ccswitchlink.Forward;
- macOS: `open -b com.ccswitch.desktop <link>`, then `lsregister -f` on
  magpie's own bundle, which reads its current Info.plist and drops the
  claim;
- Windows: the handler's program when it isn't this magpie, else
  %LOCALAPPDATA%\Programs\CC Switch\cc-switch.exe (where CC Switch's
  per-user MSI installs it); when the handler ran this magpie the key is
  rewritten as that MSI writes it ("URL:com.ccswitch.desktop protocol",
  URL Protocol, DefaultIcon, shell\open\command "exe" "%1");
- Linux: CC Switch's .desktop entry in the XDG data dirs (its first
  start's cc-switch-handler.desktop with %u first, else the package's
  "CC Switch.desktop", whose Exec has no %u, so the link is appended) is
  run, and a default that is magpie-ccswitch.desktop moves to it; magpie's
  entry is removed while it runs this magpie or a gone binary. Any other
  default is left alone.
- No CC Switch: ErrNotInstalled, nothing opened, the claim kept, so
  magpie opens such a link as it does now (it says it reads magpie://
  links only).

Existing users: nothing changes until a ccswitch:// link reaches magpie.
The first one is opened in CC Switch and the claim goes back, so later
links go to CC Switch directly.

CC Switch's facts come from farion1231/cc-switch (tauri.conf.json's
deep-link schemes ["ccswitch"], wix per-user-main.wxs, lib.rs's
register_all) and its v4.0.35 assets: macOS.zip's Info.plist
(com.ccswitch.desktop, CFBundleURLSchemes ccswitch), the portable zip's
cc-switch.exe, the .deb's "CC Switch.desktop" and /usr/bin/cc-switch.

Run (this tree on d74754970, then again where named after a rebase on
589e4ba6d, which touches none of these files):
- mote: go test -count=1 ./internal/ccswitchlink ./internal/cli
  ./internal/gui ok; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build ok; gofmt -l empty.
- wsl-run (magpie-desktop) and win-run (xiaomi-gpu): go test -count=1
  -v ./internal/ccswitchlink ok (Windows writes a test key of its own,
  Software\Classes\magpie-test-ccswitch-<pid>, removed after).
- Full suite on mica (go test -count=1 -timeout 45m ./..., on
  d74754970): 101 packages ok; reds, all filed and none in a package this
  touches: internal/dirent's watch tests (#7031; red alone on a fresh
  origin/main 589e4ba6d too), TestGatewayModeListsNoOwnSessions (#7064)
  and TestWSLStoppedDistroKept (#7071), both ok alone on mica.
- After the rebase on 589e4ba6d: go vet ./..., GOOS=linux go vet -tags
  nogui ./..., GOOS=windows go vet ./..., go build, gofmt -l on mote ok;
  ./internal/ccswitchlink and ./internal/gui ok on mote; ./internal/cli
  ok on mica. On mote three cli tests reach the 127.0.0.1:3425 an ssh
  port forward holds there (#6223).
- Real, macOS (mote, in ~/runs, cleaned up with lsregister -u): a test
  bundle registered with the old ccswitch claim kept it 5s after its
  Info.plist was rewritten without it; the built Forward then opened
  the real CC Switch v4.0.35 app and the dump showed the claim gone.
  Without CC Switch registered it returned ErrNotInstalled.
- Real, Linux (magpie-desktop, sandbox HOME and XDG dirs): with
  magpie-ccswitch.desktop as the real xdg-mime default, Forward ran
  cc-switch-handler.desktop's Exec with the link and the default was
  cc-switch-handler.desktop after.
- Broken by hand: no lsregister call (darwin), no xdg-mime default
  (linux) and no handBack (windows) each turned their test red.
- GUI suite: not run; no page, app.js or i18n change.

Not tried: a real CC Switch install on Windows (the box's real HKCU
ccswitch key is left alone; the test uses its own key), and a real
CC Switch .deb or AppImage on Linux (its entry was faked with the
package's own bytes).
2026-11-18 20:07:09 +08:00

175 lines
5.8 KiB
Go

package ccswitchlink
import (
"errors"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
const link = "ccswitch://v1/import?resource=provider&app=claude&name=Relay&apiKey=sk-x"
// fakeTools puts update-desktop-database and an xdg-mime that keeps the
// default it is given and answers `query default` with it.
func fakeTools(t *testing.T, root, def string) string {
t.Helper()
bin := filepath.Join(root, "bin")
if err := os.MkdirAll(bin, 0o755); err != nil {
t.Fatal(err)
}
state := filepath.Join(root, "default")
if def != "" {
write(t, state, def+"\n")
}
t.Setenv("CCSWITCH_TEST_DEFAULT", state)
t.Setenv("PATH", bin)
testenv.Program(t, filepath.Join(bin, "update-desktop-database"), "#!/bin/sh\nexit 0\n")
testenv.Program(t, filepath.Join(bin, "xdg-mime"), "#!/bin/sh\n"+
`[ "$1" = query ] && { [ -f "$CCSWITCH_TEST_DEFAULT" ] && read -r v < "$CCSWITCH_TEST_DEFAULT" && echo "$v"; exit 0; }`+"\n"+
`[ "$1" = default ] && [ "$3" = x-scheme-handler/ccswitch ] && echo "$2" > "$CCSWITCH_TEST_DEFAULT"`+"\n"+
"exit 0\n")
return state
}
func write(t *testing.T, path, s string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
}
// pkgEntry is what CC Switch v4.0.35's .deb installs, byte for byte;
// handlerEntry is the kind the app writes on its first start.
const (
pkgEntry = "[Desktop Entry]\nCategories=\nComment=All-in-One Assistant for Claude Code, Codex & Gemini CLI\nExec=cc-switch\nStartupWMClass=cc-switch\nIcon=cc-switch\nName=CC Switch\nTerminal=false\nType=Application\nMimeType=x-scheme-handler/ccswitch"
handlerEntry = "[Desktop Entry]\nType=Application\nName=CC Switch\nExec=\"/opt/CC Switch/cc-switch\" %u\nMimeType=x-scheme-handler/ccswitch\nNoDisplay=true\n"
)
type setup struct {
data, def string
started *[][]string
}
func linuxSetup(t *testing.T, def string, handler, pkg, magpie bool) setup {
t.Helper()
root := t.TempDir()
s := setup{data: filepath.Join(root, "data")}
sys := filepath.Join(root, "sys")
t.Setenv("XDG_DATA_HOME", s.data)
t.Setenv("XDG_DATA_DIRS", sys)
s.def = fakeTools(t, root, def)
self := filepath.Join(root, "magpie")
write(t, self, "")
e, st := executable, start
t.Cleanup(func() { executable, start = e, st })
executable = func() (string, error) { return self, nil }
var started [][]string
s.started = &started
start = func(argv []string) error { started = append(started, argv); return nil }
if handler {
write(t, filepath.Join(s.data, "applications", "cc-switch-handler.desktop"), handlerEntry)
}
if pkg {
write(t, filepath.Join(sys, "applications", "CC Switch.desktop"), pkgEntry)
}
if magpie {
// as magpie v0.1.3981 to v0.1.4454 wrote it
write(t, filepath.Join(s.data, "applications", magpieEntry), "[Desktop Entry]\nType=Application\nName=magpie (ccswitch:// links)\nExec="+self+" %u\nNoDisplay=true\nMimeType=x-scheme-handler/ccswitch;\nTerminal=false\n")
}
return s
}
func (s setup) defaultIs() string {
b, _ := os.ReadFile(s.def)
return strings.TrimSpace(string(b))
}
func (s setup) magpieEntry() bool {
_, err := os.Stat(filepath.Join(s.data, "applications", magpieEntry))
return err == nil
}
// The default an older magpie took goes to CC Switch's handler, which gets
// the link, and magpie's entry goes.
func TestForwardHandsTheDefaultBack(t *testing.T) {
s := linuxSetup(t, magpieEntry, true, true, true)
if err := Forward(link); err != nil {
t.Fatal(err)
}
if len(*s.started) != 1 || !slices.Equal((*s.started)[0], []string{"/opt/CC Switch/cc-switch", link}) {
t.Fatalf("started %q", *s.started)
}
if got := s.defaultIs(); got != "cc-switch-handler.desktop" {
t.Fatalf("default %q", got)
}
if s.magpieEntry() {
t.Fatal("magpie's ccswitch entry stayed")
}
}
// Only the package's entry, whose Exec takes no URL: the link is its last
// argument.
func TestForwardToThePackagesEntry(t *testing.T) {
s := linuxSetup(t, magpieEntry, false, true, true)
if err := Forward(link); err != nil {
t.Fatal(err)
}
if len(*s.started) != 1 || !slices.Equal((*s.started)[0], []string{"cc-switch", link}) {
t.Fatalf("started %q", *s.started)
}
if got := s.defaultIs(); got != "CC Switch.desktop" {
t.Fatalf("default %q", got)
}
}
// A default that isn't magpie's is the user's or CC Switch's: left alone,
// and so is magpie's entry.
func TestForwardLeavesAnotherDefault(t *testing.T) {
s := linuxSetup(t, "other.desktop", true, false, true)
if err := Forward(link); err != nil {
t.Fatal(err)
}
if len(*s.started) != 1 {
t.Fatalf("started %q", *s.started)
}
if got := s.defaultIs(); got != "other.desktop" {
t.Fatalf("default %q", got)
}
if !s.magpieEntry() {
t.Fatal("magpie's entry went")
}
}
// No CC Switch: nothing is started and the claim stays.
func TestForwardWithoutCCSwitch(t *testing.T) {
s := linuxSetup(t, magpieEntry, false, false, true)
if err := Forward(link); !errors.Is(err, ErrNotInstalled) {
t.Fatalf("err %v", err)
}
if len(*s.started) != 0 || s.defaultIs() != magpieEntry || !s.magpieEntry() {
t.Fatalf("started %q, default %q, entry %v", *s.started, s.defaultIs(), s.magpieEntry())
}
}
func TestExpand(t *testing.T) {
for exec, want := range map[string][]string{
`cc-switch`: {"cc-switch", link},
`/usr/bin/cc-switch %u`: {"/usr/bin/cc-switch", link},
`"/opt/CC Switch/cc-switch" %U`: {"/opt/CC Switch/cc-switch", link},
`env A=1 cc-switch --x %i %c %k %u`: {"env", "A=1", "cc-switch", "--x", link},
`sh -c "echo \"a\$b\\\\\" 100%%" %u`: {"sh", "-c", `echo "a$b\\" 100%`, link},
`"/home/u/Apps/CC-Switch.AppImage"`: {"/home/u/Apps/CC-Switch.AppImage", link},
} {
if got := expand(exec, link); !slices.Equal(got, want) {
t.Errorf("expand(%q) = %q, want %q", exec, got, want)
}
}
}