Files
magpie/internal/gui/agent_open_test.go
T
yetone ead0bf0059 agents, sessions: open in a terminal on Windows and Linux too, and copy any CLI agent's launch line
lileo0668 on Discord: clicking an agent should start it in a terminal,
and a history session should resume there. Opening a terminal was the
Mac app's only (Open in terminal on an Agents row, and Sessions' Open in
the selected terminal and Continue with another agent), and Copy launch
command was only for agy, Cursor (local) and Claude Science.

- terminal_open.go: openTerminal off a Mac.
  - Windows: Windows PowerShell (the system default), PowerShell 7 and
    Windows Terminal. The line, PowerShell's as Sessions already copies
    it, goes as -EncodedCommand: no script file, so no execution policy,
    no file encoding and no quoting in between. A shell gets its own
    console through cmd's start (magpie's children are CREATE_NO_WINDOW);
    wt is started directly, on pwsh or else powershell.
  - Linux: $TERMINAL, then xdg-terminal-exec, then x-terminal-emulator,
    then the first of GNOME Terminal, Konsole, Xfce Terminal, kitty,
    Alacritty, Ghostty, foot, WezTerm, Terminator and XTerm, each with its
    own words before the command. The line runs from a script of its own
    in sh, then the user's shell, as the Mac's .command file does.
  - A terminal that quits failing within a second says so, with what it
    printed ("cannot open display").
- Settings' Session terminal row lists the terminals found on Windows and
  Linux too; no row when none is found.
- Every agent row with a program now carries `command`, the line the
  server builds (Launch, or the program quoted for this system's shell;
  an agent in a WSL distro is started through wsl.exe on Windows). The
  row menu (right-click or the grip) has Copy launch command after Open in
  terminal, on the web build and in the panel too.

Run:
- mote (macOS 27, go1.26.8): go vet of internal/gui and internal/settings
  for darwin, for linux with -tags nogui, and for windows; GOOS=windows
  and GOOS=linux (nogui) builds of the binary; go test ./internal/gui
  ./internal/settings ./internal/agent. Everything passes except
  TestMainWindowCorners/macOS_26_SDK, which is #3949.
- Windows (win-run, xiaomi-gpu): go test ./internal/gui ./internal/settings.
  The new tests pass, among them TestAgentLineWindows, where real
  PowerShell runs the encoded line on a claude.cmd in a folder named
  `it's a $(New-Item made1) & New-Item made2 #` and runs nothing else.
  The six reds are filed: TestLodyImportHandoff,
  TestImportStatusUnreadableIsUnknown and TestAionUiImportRow (#4115),
  TestUpdateThatWontStart (#1827), and TestPluginAgentMissingSaysSo and
  TestGatewayHeldByAContainer (#5324).
- Linux (wsl-run, magpie-desktop): go test -tags nogui -timeout 45m
  ./internal/gui ./internal/settings: both ok (internal/gui 630s; at
  the default 10m it timed out with nothing hung)
- Real use. A throwaway test called openTerminal, not committed.
  - On xiaomi-gpu's console desktop (schtasks /IT): system default and
    wt each opened a window and ran a line with ' and 中文. pwsh isn't
    installed there and was refused by name.
  - On magpie-desktop under Xvfb: the system default
    (x-terminal-emulator, the xfce4-terminal wrapper) and xfce4-terminal
    each ran it (TERM=xterm-256color, set by the terminal). xterm isn't
    installed there and was refused. Without a display, xfce4-terminal's
    "cannot open display" came back as the error.
- GUI (mote, Playwright Chromium and WebKit): agent-open-terminal (five
  languages, 980px and 420px; Copy sends the server's line to /api/copy
  unchanged), session-terminal (Win32 and Linux platforms at 420px, and
  no row when nothing is found), settings-general, and the agent-*,
  sessions-*, session-*, gui-de, gui-ja and gui-zh-tw files with
  fold-show-grip, grip-inset, tap-click, menu-input-scroll and
  click-scroll (102 files): 1689 pass, 5 fail. The fails:
  menu-input-scroll, 4 of 4 red alone on origin/main 152c3852f too
  (#3402), and i18n-keys' "a plugin's words are its own" is
  red on origin/main too (#5332, #5327, #5426, #5482).
- gofmt -l is clean. TestCoreScriptBudget passes; no budget was raised.

Broken by hand:
- PowerShell quoting turned into POSIX quoting: TestAgentLineWindows red.
- cmd's start dropped: TestWinTerminalArgv red.
- A terminal's failure ignored: TestOpenLinuxTerminalRuns red.
- app.js and settings.js put back as they were: 32 GUI tests red
  (agent-open-terminal, session-terminal).

Not tried:
- A real GNOME, KDE, kitty, Alacritty, Ghostty, foot or WezTerm. Their
  words come from their docs.
- Omarchy's xdg-terminal-exec.
- PowerShell 7 on a real desktop (not installed on the box).
- The packaged GUI app on Windows or Linux; the real runs called
  openTerminal from a test binary.

Refs #4474.

Reported-by: lileo0668 (Discord)
2026-11-06 16:02:04 +08:00

125 lines
4.1 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/proc"
)
// Open in terminal on an Agents row (coolmantou on Discord): the page
// sends only the agent's id, and the line Terminal runs is made here from
// the agent's own program. A path that spells shell commands runs that
// program and nothing else, and an id that isn't an agent's opens nothing.
func TestAgentOpenRunsOnlyTheAgent(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the .command file is a POSIX shell's")
}
sandboxHome(t)
// the folder Claude Code's program is in, named to run commands if
// its path went into the line unquoted
dir := filepath.Join(t.TempDir(), `it's a' $(touch made1) `+"`touch made2`"+`; touch made3 #`)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
prog := filepath.Join(dir, "claude")
if err := os.WriteFile(prog, []byte("#!/bin/sh\necho ran >> ran\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", dir+string(os.PathListSeparator)+"/usr/bin:/bin")
proc.Forget()
t.Cleanup(proc.Forget)
var opened []string
was := agentTerminal
agentTerminal = func(run, label, choice string) error { opened = append(opened, run); return nil }
t.Cleanup(func() { agentTerminal = was })
h := Handler(nil, nil)
post := func(id string) int {
t.Helper()
b, _ := json.Marshal(map[string]string{"agent": id})
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", "/api/agents/open", strings.NewReader(string(b))))
return rec.Code
}
// an id that isn't an agent's, whatever it spells, opens nothing
for _, id := range []string{"claude; touch made4", "claude'$(touch made5)'", "cl"} {
if code := post(id); code == http.StatusNoContent || len(opened) != 0 {
t.Fatalf("%q: %d, opened %q", id, code, opened)
}
}
var row *agentJSON
s := state()
for i := range s.Agents {
if s.Agents[i].ID == "claude" {
row = &s.Agents[i]
}
}
if row == nil {
t.Fatalf("claude not detected on PATH %q", os.Getenv("PATH"))
}
if !row.Terminal || row.Command == "" {
t.Fatalf("the row's terminal is %v on %s", row.Terminal, runtime.GOOS)
}
a, err := agent.Find("claude")
if err != nil {
t.Fatal(err)
}
if code := post("claude"); code != http.StatusNoContent || len(opened) != 1 {
t.Fatalf("claude on %s: %d, opened %q", runtime.GOOS, code, opened)
}
run := opened[0]
if run != agentCommand(a) || run != row.Command {
t.Fatalf("opened %q, the row copies %q", run, row.Command)
}
// the line run as the .command file runs it, in a folder of its own
work := t.TempDir()
cmd := proc.Command("/bin/sh", "-c", run)
cmd.Dir = work
out, err := cmd.CombinedOutput()
for _, d := range []string{work, dir} {
if made, _ := filepath.Glob(filepath.Join(d, "made*")); len(made) != 0 {
t.Fatalf("%q ran more than the agent: %q", run, made)
}
}
if b, _ := os.ReadFile(filepath.Join(work, "ran")); err != nil || string(b) != "ran\n" {
t.Fatalf("%q didn't run the agent's program: %q (%v %s)", run, b, err, out)
}
}
// A desktop app's row has no command to open, and neither has an agent in
// a WSL distro off Windows: no Open in terminal.
func TestAgentCommandNeedsAProgram(t *testing.T) {
sandboxHome(t)
proc.Forget()
t.Cleanup(proc.Forget)
wsl := &agent.Agent{ID: "claude@wsl:Ubuntu", Bin: "claude", WSL: "Ubuntu"}
for _, goos := range []string{"darwin", "linux", "windows"} {
if run := agentLine(&agent.Agent{ID: "app", Name: "An app"}, "", goos); run != "" {
t.Errorf("app on %s: %q", goos, run)
}
if run := agentLine(wsl, "", goos); (run != "") != (goos == "windows") {
t.Errorf("wsl on %s: %q", goos, run)
}
}
// a program name a shell would read more into is not sent into a distro
if run := agentLine(&agent.Agent{ID: "x@wsl:U", Bin: "claude;rm", WSL: "U"}, "", "windows"); run != "" {
t.Errorf("wsl with a bad name: %q", run)
}
// one that starts on magpie by its environment (agy) opens on that
if run := agentCommand(&agent.Agent{ID: "agy", Bin: "agy", Launch: func() string { return "X=1 agy" }}); run != "X=1 agy" {
t.Errorf("agy: %q", run)
}
}