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)
241 lines
8.6 KiB
Go
241 lines
8.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
"unicode/utf16"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// installed stubs look: the programs named are found at /bin/<name>.
|
|
func installed(t *testing.T, names ...string) {
|
|
t.Helper()
|
|
was := look
|
|
look = func(name string) (string, error) {
|
|
for _, n := range names {
|
|
if n == name {
|
|
return "/bin/" + name, nil
|
|
}
|
|
}
|
|
return "", errors.New("not found")
|
|
}
|
|
t.Cleanup(func() { look = was })
|
|
}
|
|
|
|
func psDecode(t *testing.T, s string) string {
|
|
t.Helper()
|
|
b, err := base64.StdEncoding.DecodeString(s)
|
|
if err != nil || len(b)%2 != 0 {
|
|
t.Fatalf("not UTF-16LE base64: %q", s)
|
|
}
|
|
u := make([]uint16, len(b)/2)
|
|
for i := range u {
|
|
u[i] = uint16(b[2*i]) | uint16(b[2*i+1])<<8
|
|
}
|
|
return string(utf16.Decode(u))
|
|
}
|
|
|
|
// lileo0668 on Discord (#4474): a session or an agent opens in PowerShell,
|
|
// pwsh or Windows Terminal. A shell gets its console from cmd's start (the
|
|
// empty title first), and the line goes encoded, whole, whatever it spells.
|
|
func TestWinTerminalArgv(t *testing.T) {
|
|
line := `Set-Location -LiteralPath 'C:\it''s; a $(x) 中文'; & 'C:\a b\claude.cmd' --resume 'x;y'`
|
|
installed(t, "wt.exe", "pwsh.exe", "powershell.exe")
|
|
for _, tc := range []struct {
|
|
choice string
|
|
head []string
|
|
}{
|
|
{"", []string{"cmd.exe", "/c", "start", "", "/bin/powershell.exe"}},
|
|
{"system", []string{"cmd.exe", "/c", "start", "", "/bin/powershell.exe"}},
|
|
{"powershell", []string{"cmd.exe", "/c", "start", "", "/bin/powershell.exe"}},
|
|
{"pwsh", []string{"cmd.exe", "/c", "start", "", "/bin/pwsh.exe"}},
|
|
{"wt", []string{"/bin/wt.exe", "new-tab", "--", "/bin/pwsh.exe"}},
|
|
} {
|
|
argv, err := winTerminalArgv(line, tc.choice)
|
|
if err != nil {
|
|
t.Fatalf("%q: %v", tc.choice, err)
|
|
}
|
|
n := len(tc.head)
|
|
if len(argv) != n+4 || !reflect.DeepEqual(argv[:n], tc.head) || !reflect.DeepEqual(argv[n:n+3], []string{"-NoExit", "-NoLogo", "-EncodedCommand"}) {
|
|
t.Fatalf("%q: %q", tc.choice, argv)
|
|
}
|
|
if got := psDecode(t, argv[n+3]); got != line {
|
|
t.Fatalf("%q: the line came back as %q", tc.choice, got)
|
|
}
|
|
}
|
|
// Windows Terminal without PowerShell 7 runs Windows PowerShell
|
|
installed(t, "wt.exe", "powershell.exe")
|
|
if argv, err := winTerminalArgv(line, "wt"); err != nil || argv[3] != "/bin/powershell.exe" {
|
|
t.Fatalf("wt, no pwsh: %q %v", argv, err)
|
|
}
|
|
for _, choice := range []string{"pwsh", "com.apple.Terminal", "xterm"} {
|
|
if _, err := winTerminalArgv(line, choice); err == nil || !strings.Contains(err.Error(), "no longer available") {
|
|
t.Fatalf("%q: %v", choice, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLinuxTerminalArgv(t *testing.T) {
|
|
t.Setenv("TERMINAL", "")
|
|
installed(t, "xfce4-terminal", "kitty", "wezterm", "x-terminal-emulator")
|
|
for choice, want := range map[string][]string{
|
|
"xfce4-terminal": {"/bin/xfce4-terminal", "-x", "sh", "/s"},
|
|
"kitty": {"/bin/kitty", "sh", "/s"},
|
|
"wezterm": {"/bin/wezterm", "start", "--", "sh", "/s"},
|
|
// the system's: Debian's alternative, with its -e
|
|
"": {"/bin/x-terminal-emulator", "-e", "sh", "/s"},
|
|
"system": {"/bin/x-terminal-emulator", "-e", "sh", "/s"},
|
|
} {
|
|
if got, err := linuxTerminalArgv(choice, "/s"); err != nil || !reflect.DeepEqual(got, want) {
|
|
t.Errorf("%q: %q %v; want %q", choice, got, err, want)
|
|
}
|
|
}
|
|
// $TERMINAL first, with its own words
|
|
t.Setenv("TERMINAL", "kitty --single-instance")
|
|
if got, _ := linuxTerminalArgv("", "/s"); !reflect.DeepEqual(got, []string{"/bin/kitty", "sh", "/s"}) {
|
|
t.Errorf("$TERMINAL: %q", got)
|
|
}
|
|
if _, err := linuxTerminalArgv("gnome-terminal", "/s"); err == nil || !strings.Contains(err.Error(), "no longer available") {
|
|
t.Errorf("a terminal gone: %v", err)
|
|
}
|
|
installed(t)
|
|
if _, err := linuxTerminalArgv("", "/s"); err == nil {
|
|
t.Error("no terminal at all opened something")
|
|
}
|
|
}
|
|
|
|
func TestDiscoverOwnTerminals(t *testing.T) {
|
|
t.Setenv("TERMINAL", "")
|
|
installed(t, "wt.exe", "powershell.exe")
|
|
got, err := discoverOwnTerminals("windows")
|
|
if err != nil || got.Default != "powershell" || len(got.Apps) != 2 || got.Apps[0].ID != "wt" || got.Apps[1].ID != "powershell" {
|
|
t.Fatalf("windows: %+v %v", got, err)
|
|
}
|
|
installed(t, "konsole", "xdg-terminal-exec")
|
|
got, err = discoverOwnTerminals("linux")
|
|
if err != nil || got.Default != "xdg-terminal-exec" || len(got.Apps) != 2 {
|
|
t.Fatalf("linux: %+v %v", got, err)
|
|
}
|
|
installed(t)
|
|
if _, err := discoverOwnTerminals("linux"); err == nil {
|
|
t.Fatal("linux with no terminal")
|
|
}
|
|
if _, err := discoverOwnTerminals("freebsd"); err == nil {
|
|
t.Fatal("freebsd")
|
|
}
|
|
}
|
|
|
|
// The Windows line for an agent's own program: PowerShell's call operator,
|
|
// the path quoted as PowerShell quotes. On Windows it is run by PowerShell
|
|
// for real, from a folder that spells commands.
|
|
func TestAgentLineWindows(t *testing.T) {
|
|
sandboxHome(t)
|
|
// no ; in it: Windows reads PATH as split there
|
|
dir := filepath.Join(t.TempDir(), `it's a $(New-Item made1) & New-Item made2 #`)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
name, body := "claude", "#!/bin/sh\n"
|
|
if runtime.GOOS == "windows" {
|
|
name, body = "claude.cmd", "@echo ran> ran\r\n"
|
|
}
|
|
prog := filepath.Join(dir, name)
|
|
if err := os.WriteFile(prog, []byte(body), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
proc.Forget()
|
|
t.Cleanup(proc.Forget)
|
|
a := &agent.Agent{ID: "claude", Bin: "claude"}
|
|
got := agentLine(a, "", "windows")
|
|
if want := "& '" + strings.ReplaceAll(a.BinPath(), "'", "''") + "'"; got != want || !strings.Contains(got, "it''s") {
|
|
t.Fatalf("got %q, want %q", got, want)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
return
|
|
}
|
|
work := t.TempDir()
|
|
// by its own path: the sandbox's PATH may leave System32 out
|
|
ps := filepath.Join(os.Getenv("SystemRoot"), "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
|
|
cmd := proc.Command(ps, "-NoLogo", "-NonInteractive", "-NoProfile", "-EncodedCommand", psEncode(got))
|
|
cmd.Dir = work
|
|
out, err := cmd.CombinedOutput()
|
|
if b, _ := os.ReadFile(filepath.Join(work, "ran")); err != nil || strings.TrimSpace(string(b)) != "ran" {
|
|
t.Fatalf("%q didn't run the program: %q (%v %s)", got, b, err, out)
|
|
}
|
|
for _, d := range []string{work, dir, filepath.Dir(dir)} {
|
|
if made, _ := filepath.Glob(filepath.Join(d, "made*")); len(made) != 0 {
|
|
t.Fatalf("ran more than the program: %q", made)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAgentLineWSL(t *testing.T) {
|
|
a := &agent.Agent{ID: "claude@wsl:Ubuntu 24", Bin: "claude", WSL: "Ubuntu 24"}
|
|
want := `wsl.exe -d 'Ubuntu 24' -e sh -lc 'exec "${SHELL:-sh}" -lic ''claude'''`
|
|
if got := agentLine(a, "", "windows"); got != want {
|
|
t.Fatalf("got %s\nwant %s", got, want)
|
|
}
|
|
}
|
|
|
|
// The script a Linux terminal runs, run for real by a stand-in terminal:
|
|
// the line runs from a path that spells commands, then the user's shell.
|
|
func TestOpenLinuxTerminalRuns(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a POSIX shell's script")
|
|
}
|
|
bin := t.TempDir()
|
|
// xterm: -e, then the program and its words
|
|
if err := os.WriteFile(filepath.Join(bin, "xterm"), []byte("#!/bin/sh\nshift\nexec \"$@\"\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
work := filepath.Join(t.TempDir(), `it's $(touch made1); touch made2 #`)
|
|
if err := os.MkdirAll(work, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", bin+string(os.PathListSeparator)+"/usr/bin:/bin")
|
|
// the user's shell, left open after the line: here one that quits
|
|
if err := os.WriteFile(filepath.Join(bin, "shell"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("SHELL", filepath.Join(bin, "shell"))
|
|
proc.Forget()
|
|
t.Cleanup(proc.Forget)
|
|
line := "cd " + shellQuote(work) + " && echo ran > ran"
|
|
if err := openLinuxTerminal(line, "xterm"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ran := filepath.Join(work, "ran")
|
|
for deadline := time.Now().Add(30 * time.Second); ; {
|
|
if b, _ := os.ReadFile(ran); string(b) == "ran\n" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the terminal never ran the line")
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
if made, _ := filepath.Glob(filepath.Join(filepath.Dir(work), "made*")); len(made) != 0 {
|
|
t.Fatalf("ran more than the line: %q", made)
|
|
}
|
|
if made, _ := filepath.Glob(filepath.Join(work, "made*")); len(made) != 0 {
|
|
t.Fatalf("ran more than the line: %q", made)
|
|
}
|
|
// a terminal that fails at once says so
|
|
if err := os.WriteFile(filepath.Join(bin, "xterm"), []byte("#!/bin/sh\necho 'cannot open display' >&2\nexit 1\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := openLinuxTerminal("true", "xterm"); err == nil || !strings.Contains(err.Error(), "cannot open display") {
|
|
t.Fatalf("a failing terminal: %v", err)
|
|
}
|
|
}
|