Files
magpie/internal/agent/desktop_handler_test.go
T
yetone 4cd01c964a agent: a Linux link handler counts only when its desktop entry and program are there (geddy)
geddy (X, 2117406492832010548) added magpie to Cindy on Omarchy: a blank
Chrome tab opened and magpie said it was handed to Cindy. On Linux,
systemLinkHandler took any answer from `xdg-mime query default
x-scheme-handler/cindy` as "found". xdg-utils 1.2.1 (Arch) checks that the
entry exists only for mimeapps.list; a stale line in mimeinfo.cache or
defaults.list comes back as a name with nothing behind it. xdg-open then
can't find the entry and falls back to $BROWSER / chromium, which is the
blank tab.

Now the name xdg-mime gives is looked up in every XDG data dir's
applications/ (with the dash-as-subfolder variants), and its Exec program
must be there (absolute and present, or on PATH). With no entry, the gone
path goes to OpenAppLink, which returns NoLinkHandlerError ("Cindy's links
(cindy://) open …/applications/cindy.desktop, which isn't there") instead
of opening anything. A read error other than not-exist is returned as
unknown. desktopExec (Lody) now shares the same parse (desktopArgs) and
the XDG dir list.

What was run:
- TestDesktopHandlerNeedsItsEntryAndProgram (new) and -run
  'Desktop|OpenAppLink|Lody|CommandProgram' on mote (macOS) and Linux
  (wsl-run, magpie-desktop, Go 1.26.8): ok.
- Real xdg-mime on Linux in a temp HOME, XDG_CURRENT_DESKTOP=Hyprland, a
  stale `x-scheme-handler/cindy=cindy.desktop;` in mimeinfo.cache and no
  entry: origin/main said found and handed the link to xdg-open; this
  change returns the error above.
- Broken by hand: returning found with no entry turns the new test red.
- gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet
  of ./internal/agent on mote: ok.
- go test ./... on Linux: every package ok except reds that fail the same
  at base c86fa2a34 on that box: 7 internal/agent install-detection
  tests (#4033; TestHowInstalled fails at base too with -count=3),
  TestCodexPoolSaysTheClientsVersion (#4807), TestCCListingKeepsItsNames
  (#4925). Root, internal/cli and internal/gui need GTK4 there, so they
  ran again with -tags nogui: all three ok.
- Rebased on 1a92a97ca (5 commits, internal/agent's Claude Desktop
  models among them): internal/agent on Linux again, only #4033's
  install-detection reds.

Not tried: a real Omarchy VM (its Hyper-V adapter had no IP, ssh was
refused) and a real Cindy install on Linux.
2026-11-03 09:50:31 +08:00

58 lines
2.3 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
)
// geddy (11-03, Omarchy): xdg-mime named cindy.desktop for cindy://,
// but no such entry was there, so xdg-open fell back to the browser and
// a blank Chrome page opened instead of Cindy. The handler is only
// found when its entry and the entry's program are there.
func TestDesktopHandlerNeedsItsEntryAndProgram(t *testing.T) {
home, sys := t.TempDir(), t.TempDir()
prog := filepath.Join(t.TempDir(), "Cindy.AppImage")
if err := os.WriteFile(prog, []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
write := func(dir, rel, exec string) {
p := filepath.Join(dir, "applications", rel)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("[Desktop Entry]\nType=Application\nName=Cindy\nExec="+exec+"\n"), 0o644); err != nil {
t.Fatal(err)
}
}
write(sys, "cindy.desktop", `"`+prog+`" %U`)
write(home, "gone.desktop", filepath.Join(t.TempDir(), "moved", "Cindy.AppImage")+" %u")
write(sys, "vendor/cindy.desktop", prog+" %u")
dirs := []string{home, sys}
exe, found, err := desktopHandler("cindy.desktop", dirs)
if err != nil || !found || exe != prog || gone(exe) {
t.Errorf("an entry there: exe %q found %v err %v, want %q", exe, found, err, prog)
}
exe, found, err = desktopHandler("vendor-cindy.desktop", dirs)
if err != nil || !found || exe != prog {
t.Errorf("an entry in a subfolder: exe %q found %v err %v, want %q", exe, found, err, prog)
}
exe, found, err = desktopHandler("missing.desktop", dirs)
if err != nil || !found || !gone(exe) {
t.Errorf("no entry: exe %q found %v err %v, want a path that isn't there", exe, found, err)
}
exe, found, err = desktopHandler("gone.desktop", dirs)
if err != nil || !found || !gone(exe) {
t.Errorf("program moved: exe %q found %v err %v, want its old path", exe, found, err)
}
// and OpenAppLink then says so instead of handing the link to the browser
old := linkHandler
t.Cleanup(func() { linkHandler = old })
linkHandler = func(string) (string, bool, error) { return desktopHandler("missing.desktop", dirs) }
opened := false
if err := OpenAppLink(CindyLink("http://127.0.0.1:3425"), func(string) error { opened = true; return nil }); err == nil || opened {
t.Errorf("no entry: err %v opened %v, want an error and nothing opened", err, opened)
}
}