Files
magpie/internal/gui/scheme_linux_test.go
T
yetone 889758605f import: open CC Switch's ccswitch:// provider links (coder_shaun on X)
A relay's one-click ccswitch://v1/import?resource=provider&app=…&name=…
&endpoint=…&apiKey=… link now opens magpie's own import confirmation. The
format is read from CC Switch's source (src-tauri/src/deeplink/parser.rs and
provider.rs at 3c76d3b): host v1, path /import, resource=provider only,
app, name, endpoint, apiKey, apiFormat, model/haiku/sonnet/opusModel,
homepage, iconUrl, and the base64 config (claude env, codex auth + config.toml
provider table, gemini env) filling what the URL leaves out. The link is
translated into a magpie://import's parameters and goes through the same
checks (https only, key shape, id rules, balance host) and the same
confirmation; usage scripts, configUrl, enabled and notes are never taken, a
codex env_key is refused.

Registration, so as not to take CC Switch's links: Linux writes a separate
magpie-ccswitch.desktop and sets the default only when `xdg-mime query
default` is empty or already it; Windows writes HKCU ccswitch only when
HKCR has no live handler of another program; uninstall and install.sh remove
them only while they are ours. macOS declares the scheme in Info.plist
(static), so with CC Switch installed too the system picks.

Run: provider, gui (macOS mote; Linux gtk3 on WSL for the scheme tests),
uninstall, cli, update packages; vet darwin, windows, linux nogui. The test
parses CC Switch's own 11 provider examples (testdata/ccswitch_links.txt,
copied from its repo) and refuses the other resources. Broken by hand: claude's
default endpoint kind (4 reds) and the Linux "another app has it" check
(TestRegisterSchemeLeavesCCSwitchItsLinks red).
Not tried: Windows registry (vet only), a real CC Switch installed beside
magpie, a browser click on a real relay's button.

Rebased on 610ae1f54 and run again there (mica): provider, gui, uninstall,
cli, update ok; GUI import-link-balance, import-link-gemini,
import-collision, import-opencode 80/80 in chromium and webkit; vet darwin,
linux nogui, windows ok; gofmt clean. Reviewed against CC Switch db7a0e7
(no deeplink change since 3c76d3b). Broken by hand again: the config's key
no longer filling an empty apiKey (TestCCSwitchOwnExamples, 3 reds).
Pushed on 48afb27b0, which adds only the library plugins GUI files
(client.js, library.css, library-plugins.test.cjs) to that base.
Not tried either: macOS with CC Switch installed too, where Launch Services
picks which app opens ccswitch:// links.
2026-11-13 11:46:50 +08:00

255 lines
8.8 KiB
Go

package gui
import (
"bytes"
"image/png"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// fakeDesktopTools puts update-desktop-database and an xdg-mime that
// remembers the defaults it is given and answers `query default` with them,
// each call logged to calls (names only, or with arguments).
func fakeDesktopTools(t *testing.T, bin, calls string, args bool) {
t.Helper()
t.Setenv("MAGPIE_SCHEME_TEST_CALLS", calls)
t.Setenv("PATH", bin)
for _, name := range []string{"update-desktop-database", "xdg-mime"} {
log := `printf '%s\n' '` + name + `'`
if args {
log = `printf '%s\n' '` + name + `' "$@"`
}
script := "#!/bin/sh\n" + log + ` >> "$MAGPIE_SCHEME_TEST_CALLS"` + "\n"
if name == "xdg-mime" {
script += `f="$MAGPIE_SCHEME_TEST_CALLS.${3##*/}"` + "\n" + // PATH holds only these two, so shell builtins
`[ "$1" = query ] && { [ -f "$f" ] && read -r v < "$f" && echo "$v"; exit 0; }` + "\n" +
`[ "$1" = default ] && echo "$2" > "$f"` + "\n"
}
testenv.Program(t, filepath.Join(bin, name), script)
}
}
func TestRegisterScheme(t *testing.T) {
for _, tc := range []struct {
name string
flatpak bool
existing bool
}{
{"flatpak without entry", true, false},
{"flatpak with entry", true, true},
{"native without entry", false, false},
{"native with entry", false, true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("FLATPAK_ID", "")
if tc.flatpak {
t.Setenv("FLATPAK_ID", "ai.usemagpie.Magpie")
}
root := t.TempDir()
data := filepath.Join(root, "data")
t.Setenv("XDG_DATA_HOME", data)
bin := filepath.Join(root, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
t.Fatal(err)
}
calls := filepath.Join(root, "calls")
fakeDesktopTools(t, bin, calls, true)
dir := filepath.Join(data, "applications")
path := filepath.Join(dir, "magpie.desktop")
const old = "[Desktop Entry]\nExec=/usr/bin/magpie %u\nMimeType=x-scheme-handler/magpie;\n"
if tc.existing {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(old), 0o644); err != nil {
t.Fatal(err)
}
}
if err := registerScheme(); err != nil {
t.Fatal(err)
}
if tc.flatpak {
if tc.existing {
if got, err := os.ReadFile(path); err != nil || string(got) != old {
t.Fatalf("existing desktop entry changed: %q, %v", got, err)
}
} else if _, err := os.Stat(dir); !os.IsNotExist(err) {
t.Fatalf("applications directory was created: %v", err)
}
if _, err := os.Stat(filepath.Join(data, "icons")); !os.IsNotExist(err) {
t.Fatalf("icon was installed: %v", err)
}
if got, err := os.ReadFile(calls); !os.IsNotExist(err) {
t.Fatalf("desktop registration ran: %q, %v", got, err)
}
return
}
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(path)
if err != nil || !strings.Contains(string(got), "Exec="+exe+" %u\n") || !strings.Contains(string(got), "MimeType=x-scheme-handler/magpie;\n") {
t.Fatalf("desktop entry: %q, %v", got, err)
}
checkDesktopBinding(t, data, string(got))
// An entry for this executable is already registered on the next run.
if err := registerScheme(); err != nil {
t.Fatal(err)
}
// ccswitch:// links are claimed too, asked first, and on the second
// run only asked
udd := "update-desktop-database\n" + dir + "\n"
want := udd + "xdg-mime\ndefault\nmagpie.desktop\nx-scheme-handler/magpie\n" +
"xdg-mime\nquery\ndefault\nx-scheme-handler/ccswitch\n" +
udd + "xdg-mime\ndefault\nmagpie-ccswitch.desktop\nx-scheme-handler/ccswitch\n" +
"xdg-mime\nquery\ndefault\nx-scheme-handler/ccswitch\n"
if got, err := os.ReadFile(calls); err != nil || string(got) != want {
t.Fatalf("registration calls: %q, want %q (%v)", got, want, err)
}
})
}
}
// checkDesktopBinding: the entry names the window's class and an icon that
// is installed, so neither the dock nor the app grid draws a gear (#2626).
func checkDesktopBinding(t *testing.T, data, entry string) {
t.Helper()
if !strings.Contains(entry, "\nStartupWMClass="+programName+"\n") || !strings.Contains(entry, "\nIcon=magpie\n") {
t.Fatalf("desktop entry doesn't bind the window %q and its icon: %q", programName, entry)
}
icon, err := os.ReadFile(filepath.Join(data, "icons", "hicolor", "256x256", "apps", "magpie.png"))
if err != nil || !bytes.Equal(icon, desktopIcon) {
t.Fatalf("icon not installed: %d bytes, %v", len(icon), err)
}
if c, err := png.DecodeConfig(bytes.NewReader(icon)); err != nil || c.Width != 256 || c.Height != 256 {
t.Fatalf("icon in 256x256 is %dx%d (%v)", c.Width, c.Height, err)
}
}
// An entry an older magpie (or install.sh) wrote for this executable has no
// StartupWMClass; it is written again, once, and the icon put beside it.
func TestRegisterSchemeBindsAnOlderEntry(t *testing.T) {
t.Setenv("FLATPAK_ID", "")
root := t.TempDir()
data := filepath.Join(root, "data")
t.Setenv("XDG_DATA_HOME", data)
bin := filepath.Join(root, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
t.Fatal(err)
}
calls := filepath.Join(root, "calls")
fakeDesktopTools(t, bin, calls, false)
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
dir := filepath.Join(data, "applications")
path := filepath.Join(dir, "magpie.desktop")
older := "[Desktop Entry]\nType=Application\nName=magpie\nComment=Every agent's model. One place.\nExec=" + exe +
" %u\nIcon=magpie\nCategories=Development;Utility;\nMimeType=x-scheme-handler/magpie;\nTerminal=false\n"
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(older), 0o644); err != nil {
t.Fatal(err)
}
for range 2 {
if err := registerScheme(); err != nil {
t.Fatal(err)
}
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
checkDesktopBinding(t, data, string(got))
if c, err := os.ReadFile(calls); err != nil || string(c) != "update-desktop-database\nxdg-mime\nxdg-mime\nupdate-desktop-database\nxdg-mime\nxdg-mime\n" {
t.Fatalf("registered %q, want once (%v)", c, err)
}
}
// WebKitGTK names its data folder after the program; a release run as
// downloaded kept the pages' localStorage under its executable's name. It
// moves to programName's folder, never over one that is there already.
func TestKeepWebData(t *testing.T) {
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
old := filepath.Base(exe)
for _, tc := range []struct {
name string
webkit, there bool
moved bool
}{
{"webkit data under the executable's name", true, false, true},
{"magpie's folder there already", true, true, false},
{"a folder that isn't WebKit's", false, false, false},
} {
t.Run(tc.name, func(t *testing.T) {
data := t.TempDir()
t.Setenv("XDG_DATA_HOME", data)
from, to := filepath.Join(data, old), filepath.Join(data, programName)
sub := "other"
if tc.webkit {
sub = "localstorage"
}
if err := os.MkdirAll(filepath.Join(from, sub), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(from, sub, "file__0.localstorage"), []byte("favourites"), 0o644); err != nil {
t.Fatal(err)
}
if tc.there { // even empty, it is magpie's, which a rename would replace
if err := os.MkdirAll(to, 0o755); err != nil {
t.Fatal(err)
}
}
keepWebData()
b, err := os.ReadFile(filepath.Join(to, sub, "file__0.localstorage"))
if moved := err == nil && string(b) == "favourites"; moved != tc.moved {
t.Fatalf("moved to %s: %v, want %v (%v)", to, moved, tc.moved, err)
}
if _, err := os.Stat(from); os.IsNotExist(err) != tc.moved {
t.Fatalf("%s left behind: %v", from, err)
}
})
}
}
// CC Switch installed here keeps ccswitch:// links: magpie leaves the
// desktop's default for the scheme alone, and writes no entry for it.
func TestRegisterSchemeLeavesCCSwitchItsLinks(t *testing.T) {
t.Setenv("FLATPAK_ID", "")
root := t.TempDir()
data := filepath.Join(root, "data")
t.Setenv("XDG_DATA_HOME", data)
bin := filepath.Join(root, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
t.Fatal(err)
}
calls := filepath.Join(root, "calls")
fakeDesktopTools(t, bin, calls, true)
if err := os.WriteFile(calls+".ccswitch", []byte("cc-switch.desktop\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := registerScheme(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(data, "applications", ccSwitchEntry)); !os.IsNotExist(err) {
t.Fatalf("an entry for ccswitch:// was written: %v", err)
}
if c, _ := os.ReadFile(calls); strings.Contains(string(c), "default\nmagpie-ccswitch.desktop") {
t.Fatalf("took the default from CC Switch: %q", c)
}
// magpie's own scheme is still registered
if _, err := os.Stat(filepath.Join(data, "applications", "magpie.desktop")); err != nil {
t.Fatal(err)
}
}