Files
magpie/internal/gui/agent_open_test.go
T
yetone b9e5bc6cb3 gui: Rex is a session terminal, opened through its rex CLI (#4565)
Rex (com.superlogical.rex) claims no .command file, so Launch Services
never listed it and `open -a` can't hand it a script (StringKe).
Sessions' terminal list now offers Rex when its CLI is found: a native
`rex` on PATH, or one inside a Rex*.app in /Applications or
~/Applications (Rex puts rex on PATH only in its own terminals). Perl's
Rex installs a script named rex too, so only a Mach-O or ELF program
counts. Resume, carry and an agent's Open then run
`rex run --label <title> -- /bin/sh -c '<command>; exec login shell'`:
no shell parses the label or the command on the way. With no session
to add a tab to (exit 3) it runs `rex new`; with Rex's server not
running (exit 4) the error says to open Rex. Rex is then brought
forward with `open -b`. The GUI already lists s.terminalApps, so no
JS changed. reference.md's Session terminal says it.

Parts 2 to 4 of #4565 (accounts run per account, OSC 7501 status,
folder mapping) build on #992, which isn't merged; not done here.

Ran, at this tree on origin/main e90a635a9, through mote-run:
- go test ./internal/gui -run 'TestFindRex|TestOpenInRex|
  TestDiscoverTerminals|TestTerminalOpenArgs|TestAgentOpen': ok.
  TestOpenInRex runs the real sh script with SHELL=/usr/bin/true and
  checks the command ran in its folder, and a label holding $(...) and
  quotes stays one argument.
- go vet ./...; GOOS=linux (-tags nogui) and GOOS=windows go vet and
  go build: ok (at the pre-rebase head; go vet ./... again rebased).
- Full suite, go test -tags nogui -timeout 45m ./..., at the
  pre-rebase head: every package ok but internal/gui, red on
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514, red the same on
  origin/main e90a635a9) and TestColdTurnsTaken (#4510). Rebased,
  internal/gui and internal/provider whole again: red on #4514 only.

Broke by hand, each red, then restored: withRex taken out of
discovery (TestDiscoverTerminalsOffersRex); the exit-3 fallback to
rex new turned off (TestOpenInRex: "rex: Error: no session"); the
native-program check dropped for PATH (TestFindRex took the Perl
script).

Not tried: the real Rex app or rex binary. Its installer needs a
Discord sign-in, so the CLI was driven from Rex's docs only; whether
`rex run` with no -s from outside Rex targets the shown session or
exits 3 is not confirmed. Not run on Linux or Windows (Rex's Linux
build, and the tests skip on Windows). No GUI Playwright run, as no
JS changed.
2026-10-31 19:35:16 +08:00

122 lines
3.8 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 != (runtime.GOOS == "darwin") {
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)
}
run := agentCommand(a)
if runtime.GOOS == "darwin" {
if code := post("claude"); code != http.StatusNoContent || len(opened) != 1 {
t.Fatalf("claude: %d, opened %q", code, opened)
}
run = opened[0]
} else if code := post("claude"); code == http.StatusNoContent || len(opened) != 0 {
t.Fatalf("off a Mac: %d, opened %q", code, opened)
}
// 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: no Open in terminal.
func TestAgentCommandNeedsAProgram(t *testing.T) {
sandboxHome(t)
proc.Forget()
t.Cleanup(proc.Forget)
for _, a := range []*agent.Agent{
{ID: "app", Name: "An app"},
{ID: "claude@wsl:Ubuntu", Bin: "sh", WSL: "Ubuntu", Launch: func() string { return "claude" }},
} {
if run := agentCommand(a); run != "" {
t.Errorf("%s: %q", a.ID, 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)
}
}