Files
magpie/internal/gui/terminal_apps_darwin_test.go
T
yetone 7d11f7d428 gui: Rex opens sessions in a Rex session of magpie's own, with none, one or several open (#4565, StringKe)
StringKe tried b9e5bc6cb with the real Rex (server 0.0.1793131181,
Rex Beta 0.1.0) on v0.1.2391: it opened only when Rex had exactly one
session. Outside Rex, `rex run` with no -s exits 3 both for no session
and for several ("session target default is ambiguous"), and exit 3
means not found or ambiguous; magpie then ran `rex new <t> --label <t>`,
and `rex new` has no --label (exit 2). run's --label also names the
block, not the tab, and the CLI can't tell which session the app shows.

Now magpie keeps a Rex session of its own, labelled magpie. openInRex
lists sessions with `rex ls --json` and takes the one labelled exactly
magpie (else magpie in another case), by its full ID since labels can be
shared. It runs `rex run -s <id> --label=<t> --json -- /bin/sh -c ...`
and names the new tab with `rex window rename -s <id> -- <window_id> <t>`
from run's JSON. With no such session, or a 3 from run because it ended
since the list, it runs `rex new magpie --window=<t> -- /bin/sh -c ...`,
whose first tab is so named. Labels go as --flag=value and the rename's
positionals after --, so a title starting with "-" stays a title. Exit 4
still says to open Rex. rexRun now gives stdout only (for the JSON), and
rex's stderr comes back through ExitError for the message.
docs/reference.md's Session terminal paragraph says it.

Every flag was checked against the real rex CLI, not the report: the
snapshot build 0.0.1793420730-g6d0d9a (rex-darwin-arm64.zip from
blob.superlogical.com, sha256 checked against the release API) on mote,
in a sandbox HOME and XDG_RUNTIME_DIR, where rex autostarts a server of
its own. `rex new --help`, `run --help`, `window rename --help`; by hand
with 0, 1, 2 sessions and two labelled magpie/MAGPIE: no -s exits 3
"not found: no sessions" with 0 and "ambiguous" with 2; `new --label`
exits 2; `run -s <full id>` works with any number; `-s magpie` with two
so labelled exits 3 ambiguous; `rex new magpie --window=<t>` names the
first tab; rename sets the tab's label.

Tests: TestOpenInRex runs a fake rex modelled on those answers (target
by full ID or any-case label, 3 for not found and ambiguous alike, 2 for
new --label) with no session, one other, two others, magpie's among two
and two labelled magpie, opening twice each: one magpie session made,
then used, both tabs named. Also a session that ends between ls and run,
exit 4 and exit 1. TestOpenInRexReal runs the same cases against the real
rex when MAGPIE_TEST_REX names one (skipped otherwise), with a server of
its own that it kills and stops at the end.
TestDiscoverTerminalsOffersRex uses the fake with two sessions.

Ran, through mote-run at this tree rebased on origin/main f85a221c9:
- MAGPIE_TEST_REX=<real rex> go test -count=1 ./internal/gui: every test
  ok, TestOpenInRexReal included, except TestSettingsSaveKeepsWhatItDoes-
  NotSend (#4514) and TestMainWindowCorners/macOS_26_SDK (#3949), both
  red the same run alone on a detached origin/main f85a221c9.
- gofmt -l; go vet ./internal/gui; GOOS=linux -tags nogui and
  GOOS=windows go vet ./internal/gui and go build .: ok.
- No rex server or /tmp/rx* left after the tests.

Broke by hand, each red, then restored: the old openInRex (run with no
-s, then new --label): every TestOpenInRex case, the discovery test and
TestOpenInRexReal red ("unknown flag: --label"); the session lookup made
to find none: a second magpie session each time, red in the fake and
the real test; the window rename left out: tabs "Window <id>", red in
both.

Not tried: the Rex app itself (its download needs a Discord sign-in),
so whether Rex.app switches to the magpie session when a tab is added
from outside is not seen; nor whether an autostarted CLI server could
differ from the app's when the app is closed. Not the full go test ./...
(only internal/gui changed), no GUI Playwright run (no JS changed), not
run on Linux or Windows.
2026-10-31 21:38:56 +08:00

61 lines
2.0 KiB
Go

//go:build darwin && cgo
package gui
import (
"slices"
"strings"
"testing"
)
func TestDiscoverTerminals(t *testing.T) {
found, err := discoverTerminals()
if err != nil {
t.Fatal(err)
}
terminalFound := false
for _, app := range found.Apps {
if app.ID == "com.apple.TextEdit" {
t.Fatal("a text editor was offered as a session terminal")
}
if app.ID == terminalBundleID && app.Path != "" {
terminalFound = true
}
}
if !terminalFound {
t.Fatalf("Terminal.app missing from .command handlers: %+v", found.Apps)
}
}
// StringKe (#4565): Rex claims no .command file, so Launch Services never
// lists it; it is offered when its CLI is found, and opened through it.
func TestDiscoverTerminalsOffersRex(t *testing.T) {
was := rexFound
t.Cleanup(func() { rexFound = was })
rexFound = func() string { return "/opt/rex/bin/rex" }
found, err := discoverTerminals()
if err != nil {
t.Fatal(err)
}
rex := slices.IndexFunc(found.Apps, func(a terminalApp) bool { return a.ID == rexBundleID })
if rex < 0 || found.Apps[rex].Path != "/opt/rex/bin/rex" || found.Apps[rex].Name != "Rex" {
t.Fatalf("Rex not offered with its CLI found: %+v", found.Apps)
}
f := &fakeRex{t: t, sessions: []fakeRexSession{{id: "session:a1", label: "work"}, {id: "session:a2", label: "notes"}}}
f.use(t)
if err := openTerminal("cd '/tmp' && claude --resume x", "Fix the build", rexBundleID); err != nil {
t.Fatal(err)
}
if f.bin != "/opt/rex/bin/rex" || len(f.sessions) != 3 || f.sessions[2].label != "magpie" || !slices.Equal(f.sessions[2].windows, []string{"Fix the build"}) {
t.Fatalf("rex ran %q, sessions %+v: want a magpie session, its tab named for the session's title", f.ran, f.sessions)
}
ran := len(f.ran)
rexFound = func() string { return "" }
if err := openTerminal("claude", "", rexBundleID); err == nil || !strings.Contains(err.Error(), "no longer available") {
t.Fatalf("Rex chosen with no CLI found: %v, want it said to be unavailable", err)
}
if len(f.ran) != ran {
t.Fatalf("rex ran again with none found: %q", f.ran)
}
}