Files
magpie/internal/agent/codex_linux_app_test.go
T
yetone 41357f3935 codex: the ChatGPT desktop app on Linux is found, and a switch reaches it
pota on Discord: magpie switches the ChatGPT (Codex) desktop app's
account on macOS and Windows but not on Linux. OpenAI ships the app for
Linux since 08-11 (preview; learn.chatgpt.com/docs/linux/linux-app): a
deb, an rpm and an Arch package, which install /usr/lib/chatgpt. Its
app-server is /usr/lib/chatgpt/resources/codex ... app-server, run with
the user's HOME and no CODEX_HOME, so it reads ~/.codex like the CLI.
isCodexAppServer took only .app/Contents/ and Windows paths, so on Linux
nothing found the app: no "still signed in as" notice after a switch,
no restart of its app-server, and no version on the Agents row.

- provider: linuxCodexApp takes an absolute .../chatgpt/resources/codex
  as the app's own app-server (findCodexApp, CodexAppStale, the switch's
  restart all read it).
- agent: restartOnLinux; codexAppOpen knows on Linux whether the app is
  open (any /usr/lib/chatgpt/ChatGPT process, not its codex alone or
  another program's ChatGPT). desktopApps["codex"].linux reads the
  version from resources/linux-package-metadata.json, HasApp/AppVersion
  see it, and codexReaders counts the bundled codex's --version as a
  reader of Codex's config. version() no longer stops at an empty store
  read before the other sources.

Real run, WSL magpie-desktop (Ubuntu 24.04), OpenAI's chatgpt_amd64.deb
26.1027.30824 (bundled codex-cli 0.166.0-alpha.5.1) under Xvfb, sandbox
HOME, fake unsigned JWT accounts alice/bob (no real credentials), magpie
web on 127.0.0.1:3591:
- POST /api/login/switch alice: 200, auth.json alice; /api/providers
  then says codexApp "bob@example.com" (the stale notice).
- POST /api/codex/daemon/restart-app: 200 in ~3s; the old app-server
  ended (app log "Codex CLI process exited classifiedAsExpected=true
  code=0"), a new one started with the sandbox HOME, the notice cleared.
- /api/agents/cli: "codex":{"app":"26.1027.30824"}.
- The same run with linuxCodexApp reverted: no notice, restart-app 400,
  the app-server untouched (pota's case).

Run at 5b1709eee: go test -tags nogui -timeout 45m ./internal/agent
./internal/provider on mote, ok; gofmt -l clean; GOOS=linux go vet
-tags nogui and GOOS=windows go vet, ok; go build ./... ok. Full
go test -tags nogui -timeout 45m ./... on mote at cae01311c (the
earlier base; nothing under internal/agent, provider or proc changed
since): every package ok except two reds that are on main and filed,
TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestNoCommandBypassesProc (#4491, now child_unix.go too). WSL
./internal/agent ./internal/provider: provider ok; agent red only in
TestGrokInstalled, TestCursorFolderFromTheEditorIsNotTheCLI and
TestOmpDetectedWithPiDir, red the same alone at origin/main 5b1709eee
in WSL (#4033, #3548); the new Linux tests pass there.

Broken by hand: linuxCodexApp returning false turns TestFindCodexApp and
TestCodexAppOwnServer red; codexAppOpen's Linux case removed and
linuxVersion returning "" turn TestCodexLinuxAppOpen and
TestCodexLinuxAppVersion red; the old store early return turns
TestCodexLinuxAppVersion red; codexReaders' linux case removed turns
TestCodexLinuxAppReadsTheConfig red in WSL.

Not tried: the rpm and Arch packages (the Arch one has the same
usr/lib/chatgpt layout; the rpm's is assumed), a real ChatGPT account
(the app's own UI asks to sign in, since the fake tokens fail refresh),
and reopening the app when no app-server comes back (Windows only;
Linux says to quit and open it again, as macOS does). A switch made from
the CLI while magpie web runs elsewhere doesn't restart the app; the
notice says to, as on macOS.
2026-10-31 16:08:28 +08:00

161 lines
5.5 KiB
Go

package agent
import (
"context"
"os"
"path/filepath"
"regexp"
"runtime"
"slices"
"testing"
"github.com/yetone/magpie/internal/proc"
)
// linuxCodexMetadata is resources/linux-package-metadata.json as OpenAI's
// chatgpt 26.1027.30824 deb installs it (seen in the WSL distro
// magpie-desktop, Ubuntu 24.04).
const linuxCodexMetadata = `{
"codexAppBrand": "chatgpt",
"codexBuildFlavor": "prod",
"version": "26.1027.30824"
}
`
// installLinuxCodexApp puts OpenAI's Linux package's files magpie reads
// under the sandbox's /usr/lib/chatgpt, and says where.
func installLinuxCodexApp(t *testing.T) string {
t.Helper()
dir := desktopApps["codex"].linuxDir()
if dir != "" {
t.Fatalf("an empty sandbox has the Linux Codex app at %s", dir)
}
app := desktopApps["codex"]
if app.linux == "" {
t.Fatal("no Linux folder for the Codex app")
}
dir = proc.SystemDirs(app.linux)[0]
res := filepath.Join(dir, "resources")
if err := os.MkdirAll(res, 0o755); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(dir) })
if err := os.WriteFile(filepath.Join(res, "linux-package-metadata.json"), []byte(linuxCodexMetadata), 0o644); err != nil {
t.Fatal(err)
}
return dir
}
// pota on Discord: magpie had no account sign-in or switch for the
// ChatGPT desktop app on Linux, which macOS and Windows have. OpenAI's
// Linux package (chatgpt, in preview since 08-11) installs the app to
// /usr/lib/chatgpt; the Agents row shows its version, from the package's
// metadata, with or without a CLI, and Codex's folder isn't a leftover
// while it is installed.
func TestCodexLinuxAppVersion(t *testing.T) {
was, wasStore := appFolders, windowsStorePackages
appFolders = func() []string { return nil }
windowsStorePackages = func() []string { return nil }
t.Cleanup(func() { appFolders, windowsStorePackages = was, wasStore })
t.Setenv("PATH", t.TempDir()) // no codex CLI
codex := &Agent{ID: "codex", Name: "Codex", Bin: "codex"}
if v := codex.AppVersion(); v != "" || codex.HasApp() {
t.Fatalf("no app installed: %q, %v", v, codex.HasApp())
}
dir := installLinuxCodexApp(t)
if v := codex.AppVersion(); v != "26.1027.30824" {
t.Fatalf("the Linux package's version: %q", v)
}
if !codex.HasApp() {
t.Fatal("the Linux app not installed")
}
if c, ok := codex.CLI(); !ok || c.App != "26.1027.30824" {
t.Fatalf("the app alone, no CLI: %+v %v", c, ok)
}
// metadata that doesn't say: installed, no version
if err := os.WriteFile(filepath.Join(dir, "resources", "linux-package-metadata.json"), []byte(`{"codexAppBrand":"chatgpt"}`), 0o644); err != nil {
t.Fatal(err)
}
if v := codex.AppVersion(); v != "" || !codex.HasApp() {
t.Fatalf("metadata without a version: %q, %v", v, codex.HasApp())
}
// Codex's twin in WSL has none of this machine's
if (&Agent{ID: "codex@wsl:Ubuntu", WSL: "Ubuntu", Bin: "codex"}).HasApp() {
t.Fatal("WSL twin")
}
}
// After a switch magpie restarts the app's app-server, and when the app
// starts none it says to quit and open it again, unless the app was quit
// meanwhile: whether it is open is known on Linux too, from any process of
// /usr/lib/chatgpt/ChatGPT (the main one and Electron's helpers, as ps
// listed them for 26.1027), never from its codex alone or another
// program's ChatGPT.
func TestCodexLinuxAppOpen(t *testing.T) {
restartSandbox(t)
var cmds []string
running = func(pat string) []runningProc {
re := regexp.MustCompile(pat)
var out []runningProc
for _, c := range cmds {
if re.MatchString(c) {
out = append(out, runningProc{cmd: c})
}
}
return out
}
if _, known := codexAppOpen(context.Background()); known {
t.Fatal("known on no system")
}
restartOnLinux = true
for _, tc := range []struct {
cmds []string
open bool
}{
{[]string{"/usr/lib/chatgpt/ChatGPT", "/usr/lib/chatgpt/ChatGPT --type=zygote --no-zygote-sandbox", codexLinuxAppServerCmd}, true},
{[]string{"/usr/lib/chatgpt/ChatGPT --type=renderer"}, true},
{nil, false},
{[]string{codexLinuxAppServerCmd, "/usr/lib/chatgpt/chrome_crashpad_handler --monitor-self", "/opt/ChatGPT/ChatGPT", "/usr/lib/chatgpt/ChatGPTx"}, false},
} {
cmds = tc.cmds
if open, known := codexAppOpen(context.Background()); open != tc.open || !known {
t.Errorf("%q: open %v, known %v; want %v", tc.cmds, open, known, tc.open)
}
}
}
const codexLinuxAppServerCmd = "/usr/lib/chatgpt/resources/codex -c features.code_mode_host=true app-server --analytics-default-enabled"
// The Linux app's codex reads Codex's config as the CLI does: it is one
// of the readers whose version decides what magpie writes there, read
// from its --version (codex-cli 0.166.0-alpha.5.1 in chatgpt 26.1027).
func TestCodexLinuxAppReadsTheConfig(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("the Linux package is read on Linux only")
}
t.Setenv("PATH", t.TempDir()) // no codex CLI
home := t.TempDir()
if got := codexReaders(place{home: home}); slices.ContainsFunc(got, func(r CodexReader) bool { return r.Kind == "app" }) {
t.Fatalf("no app installed: %+v", got)
}
dir := installLinuxCodexApp(t)
bin := filepath.Join(dir, "resources", "codex")
if err := os.WriteFile(bin, []byte("#!/bin/sh\necho codex-cli 0.166.0-alpha.5.1\n"), 0o755); err != nil {
t.Fatal(err)
}
wasRun := runVersion
runVersion = func(b string) string {
if b != bin {
t.Errorf("version read from %s, want %s", b, bin)
}
return "codex-cli 0.166.0-alpha.5.1"
}
t.Cleanup(func() { runVersion = wasRun })
got := codexReaders(place{home: home})
if !slices.Contains(got, CodexReader{Kind: "app", Version: "0.166.0-alpha.5.1"}) {
t.Fatalf("the Linux app's codex not read: %+v", got)
}
}